<commit_before><?php

/*
 * This file is part of Flarum.
 *
 * For detailed copyright and license information, please view the
 * LICENSE file that was distributed with this source code.
 */

namespace Flarum\Tags;

use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;

class TagRepository
{
    private const TAG_RELATIONS = ['children', 'parent'];

    /**
     * Get a new query builder for the tags table.
     *
     * @return Builder
     */
    public function query()
    {
        return Tag::query();
    }

    /**
     * @param array|string $relations
     * @param User $actor
     * @return Builder
     */
    public function with($relations, User $actor): Builder
    {
        $relationsArray = is_string($relations) ? explode(',', $relations) : $relations;

        foreach (self::TAG_RELATIONS as $relation) {
            if (in_array($relation, $relationsArray, true)) {
                $relationsArray = array_diff($relationsArray, [$relation]);

                $relationsArray[$relation] = function ($query) use ($actor) {
                    $query->whereVisibleTo($actor);
                };
            }
        }

        return $this->query()->with($relationsArray);
    }

    /**
     * Find a tag by ID, optionally making sure it is visible to a certain
     * user, or throw an exception.
     *
     * @param int $id
     * @param User $actor
     * @return Tag
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    public function findOrFail($id, User $actor = null)<commit_msg>fix: `$relationsArray` can end up containing closures (#146)<commit_after><?php

/*
 * This file is part of Flarum.
 *
 * For detailed copyright and license information, please view the
 * LICENSE file that was distributed with this source code.
 */

namespace Flarum\Tags;

use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;

class TagRepository
{
    private const TAG_RELATIONS = ['children', 'parent'];

    /**
     * Get a new query builder for the tags table.
     *
     * @return Builder
     */
    public function query()
    {
        return Tag::query();
    }

    /**
     * @param array|string $relations
     * @param User $actor
     * @return Builder
     */
    public function with($relations, User $actor): Builder
    {
        $relations = is_string($relations) ? explode(',', $relations) : $relations;
        $relationsArray = [];

        foreach ($relations as $relation) {
            if (in_array($relation, self::TAG_RELATIONS, true)) {
                $relationsArray[$relation] = function ($query) use ($actor) {
                    $query->whereVisibleTo($actor);
                };
            } else {
                $relationsArray[] = $relation;
            }
        }

        return $this->query()->with($relationsArray);
    }

    /**
     * Find a tag by ID, optionally making sure it is visible to a certain
     * user, or throw an exception.
     *
     * @param int $id
     * @param User $actor
     * @return Tag
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    public function findOrFail($id, User $actor = null)
{'fraction_non_alphanumeric': 0.11842938568714376, 'fraction_numerical': 0.0009499683343888537, 'mean_word_length': 2.2837837837837838, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '197679', 'n_tokens_mistral': 909, 'n_tokens_neox': 855, 'n_words': 261}