<commit_before><?php

namespace Laravel\Scout\Engines;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str;
use Laravel\Scout\Builder;

class CollectionEngine extends Engine
{
    /**
     * Create a new engine instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Update the given model in the index.
     *
     * @param  \Illuminate\Database\Eloquent\Collection  $models
     * @return void
     */
    public function update($models)
    {
        //
    }

    /**
     * Remove the given model from the index.
     *
     * @param  \Illuminate\Database\Eloquent\Collection  $models
     * @return void
     */
    public function delete($models)
    {
        //
    }

    /**
     * Perform the given search on the engine.
     *
     * @param  \Laravel\Scout\Builder  $builder
     * @return mixed
     */
    public function search(Builder $builder)
    {
        $models = $this->searchModels($builder);

        return [
            'results' => $models->all(),
            'total' => count($models),
        ];
    }

    /**
     * Perform the given search on the engine.
     *
     * @param  \Laravel\Scout\Builder  $builder
     * @param  int  $perPage
     * @param  int  $page
     * @return mixed
     */
    public function paginate(Builder $builder, $perPage, $page)
    {
        $models = $this->searchModels($builder);

        return [
            'results' => $models->forPage($page, $perPage)->all(),
            'total' => count($models),
        ];
    }

    /**
     * Get the Eloquent models for the given builder.
     *
     * @param  \Laravel\Scout\Builder  $builder
     * @return \Illuminate\Database\Eloquent\Collection
     */
    protected function searchModels(Builder $builder)
    {
        $query = $builder->model->query()
                        ->when(! is_null($builder->callback), function ($query) use ($builder) {
                            call_user_func($builder->callback, $query, $builder, $builder->query);
                        })
                        ->when(! $builder->callback && count($builder->wheres) > 0, function ($query) use ($builder) {
                            foreach ($builder->wheres as $key => $value) {
                                if ($key !== '__soft_deleted') {
                                    $query->where($key, $value);
                                }
                            }
                        })
                        ->when(! $builder->callback && count($builder->whereIns) > 0, function ($query) use ($builder) {
                            foreach ($builder->whereIns as $key => $values) {
                                $query->whereIn($key, $values);
                            }
                        })
                        ->orderBy($builder->model->getKeyName(), 'desc');

        $models = $this->ensureSoftDeletesAreHandled($builder, $query)
                        ->get()
                        ->values();

        if (count($models) === 0) {
            return $models;
        }

        return $models->filter(function ($model) use ($builder) {
            if (! $model->shouldBeSearchable()) {
                return false;
            }

            if (! $builder->query) {
                return true;
            }

            $searchables = $model->toSearchableArray();

            foreach ($searchables as $value) {
                if (Str::contains(Str::lower($value), Str::lower($builder->query))) {
                    return true;
                }
            }

            return false;<commit_msg>[9.x] - Collection Engine: add support for non-scalar values (#528)<commit_after><?php

namespace Laravel\Scout\Engines;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str;
use Laravel\Scout\Builder;

class CollectionEngine extends Engine
{
    /**
     * Create a new engine instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Update the given model in the index.
     *
     * @param  \Illuminate\Database\Eloquent\Collection  $models
     * @return void
     */
    public function update($models)
    {
        //
    }

    /**
     * Remove the given model from the index.
     *
     * @param  \Illuminate\Database\Eloquent\Collection  $models
     * @return void
     */
    public function delete($models)
    {
        //
    }

    /**
     * Perform the given search on the engine.
     *
     * @param  \Laravel\Scout\Builder  $builder
     * @return mixed
     */
    public function search(Builder $builder)
    {
        $models = $this->searchModels($builder);

        return [
            'results' => $models->all(),
            'total' => count($models),
        ];
    }

    /**
     * Perform the given search on the engine.
     *
     * @param  \Laravel\Scout\Builder  $builder
     * @param  int  $perPage
     * @param  int  $page
     * @return mixed
     */
    public function paginate(Builder $builder, $perPage, $page)
    {
        $models = $this->searchModels($builder);

        return [
            'results' => $models->forPage($page, $perPage)->all(),
            'total' => count($models),
        ];
    }

    /**
     * Get the Eloquent models for the given builder.
     *
     * @param  \Laravel\Scout\Builder  $builder
     * @return \Illuminate\Database\Eloquent\Collection
     */
    protected function searchModels(Builder $builder)
    {
        $query = $builder->model->query()
                        ->when(! is_null($builder->callback), function ($query) use ($builder) {
                            call_user_func($builder->callback, $query, $builder, $builder->query);
                        })
                        ->when(! $builder->callback && count($builder->wheres) > 0, function ($query) use ($builder) {
                            foreach ($builder->wheres as $key => $value) {
                                if ($key !== '__soft_deleted') {
                                    $query->where($key, $value);
                                }
                            }
                        })
                        ->when(! $builder->callback && count($builder->whereIns) > 0, function ($query) use ($builder) {
                            foreach ($builder->whereIns as $key => $values) {
                                $query->whereIn($key, $values);
                            }
                        })
                        ->orderBy($builder->model->getKeyName(), 'desc');

        $models = $this->ensureSoftDeletesAreHandled($builder, $query)
                        ->get()
                        ->values();

        if (count($models) === 0) {
            return $models;
        }

        return $models->filter(function ($model) use ($builder) {
            if (! $model->shouldBeSearchable()) {
                return false;
            }

            if (! $builder->query) {
                return true;
            }

            $searchables = $model->toSearchableArray();

            foreach ($searchables as $value) {
                if (! is_scalar($value)) {
                    $value = json_encode($value);
                }

                if (Str::contains(Str::lower($value), Str::lower($builder->query))) {
                    return true;
                }
            }

            return false;
{'fraction_non_alphanumeric': 0.13186378035902852, 'fraction_numerical': 0.0013199577613516368, 'mean_word_length': 1.4379021879021878, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1986303', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1994, 'n_words': 435}