<commit_before><?php
namespace Search\Model\Behavior;

use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Cake\Utility\Hash;
use Exception;
use Search\Manager;

class SearchBehavior extends Behavior
{

    /**
     * Search Manager instance.
     *
     * @var \Search\Manager
     */
    public $_manager = null;

    /**
     * Default config for the behavior.
     *
     * ### Options
     * - `searchConfigMethod` Method name of the method that returns the filters.
     *
     * @var array
     */
    protected $_defaultConfig = [
        'searchConfigMethod' => 'searchConfiguration',
        'implementedFinders' => [
            'search' => 'findSearch'
        ],
        'implementedMethods' => [
            'filterParams' => 'filterParams',
            'searchManager' => 'searchManager',
            'isSearch' => 'isSearch'
        ],
        'isSearch' => false
    ];

    /**
     * Callback fired from the controller.
     *
     * @param Query $query Query.
     * @param array $options The options for the find.
     *   - `search`: Array of search arguments.
     * @return \Cake\ORM\Query The Query object used in pagination.
     * @throws \Exception When missing search arguments.
     */
    public function findSearch(Query $query, array $options)
    {
        $this->config('isSearch', false);
        if (!isset($options['search'])) {
            throw new Exception(
                'Custom finder "search" expects search arguments ' .
                'to be nested under key "search" in find() options.'
            );
        }

        $filters = $this->_getAllFilters();
        $params = (array)$options['search'];
        $params = Hash::flatten($params);
        $params = array_intersect_key(Hash::filter($params), $filters);

        $isSearch = false;
        foreach ($filters as $filter) {
            $filter->args($params);
            $filter->query($query);

            if (!$filter->skip()) {
                $isSearch = true;
            }
            $filter->process();
        }
        $this->config('isSearch', $isSearch);

        return $query;
    }

    /**
     * Returns true if the findSearch call modified the query in a way
     * that at least one search filter has been applied.
     *
     * @return bool
     */
    public function isSearch()
    {
        return $this->config('isSearch');
    }

    /**
     * Returns search params nested in array with key `_search` for passing as
     * options to find method.
     *
     * @param array $params A key value list of search parameters to use for a search.
     * @return array
     * @deprecated 2.0.0 You can directly call find like
     *   `find('search', ['search' => $this->request->query])`
     */
    public function filterParams($params)
    {
        return ['search' => $params];
    }

    /**
     * Returns the search filter manager.
     *
     * @return \Search\Manager
     */
    public function searchManager()
    {
        if ($this->_manager === null) {<commit_msg>Simplify.<commit_after><?php
namespace Search\Model\Behavior;

use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Cake\Utility\Hash;
use Exception;
use Search\Manager;

class SearchBehavior extends Behavior
{

    /**
     * Search Manager instance.
     *
     * @var \Search\Manager
     */
    public $_manager = null;

    /**
     * Default config for the behavior.
     *
     * ### Options
     * - `searchConfigMethod` Method name of the method that returns the filters.
     *
     * @var array
     */
    protected $_defaultConfig = [
        'searchConfigMethod' => 'searchConfiguration',
        'implementedFinders' => [
            'search' => 'findSearch'
        ],
        'implementedMethods' => [
            'filterParams' => 'filterParams',
            'searchManager' => 'searchManager',
            'isSearch' => 'isSearch'
        ],
    ];
    
    /**
     * @var bool
     */
    protected $_isSearch = false;

    /**
     * Callback fired from the controller.
     *
     * @param Query $query Query.
     * @param array $options The options for the find.
     *   - `search`: Array of search arguments.
     * @return \Cake\ORM\Query The Query object used in pagination.
     * @throws \Exception When missing search arguments.
     */
    public function findSearch(Query $query, array $options)
    {
        $this->config('isSearch', false);
        if (!isset($options['search'])) {
            throw new Exception(
                'Custom finder "search" expects search arguments ' .
                'to be nested under key "search" in find() options.'
            );
        }

        $filters = $this->_getAllFilters();
        $params = (array)$options['search'];
        $params = Hash::flatten($params);
        $params = array_intersect_key(Hash::filter($params), $filters);

        $this->_isSearch = false;
        foreach ($filters as $filter) {
            $filter->args($params);
            $filter->query($query);

            if (!$filter->skip()) {
                $this->_isSearch = true;
            }
            $filter->process();
        }

        return $query;
    }

    /**
     * Returns true if the findSearch call modified the query in a way
     * that at least one search filter has been applied.
     *
     * @return bool
     */
    public function isSearch()
    {
        return $this->_isSearch;
    }

    /**
     * Returns search params nested in array with key `_search` for passing as
     * options to find method.
     *
     * @param array $params A key value list of search parameters to use for a search.
     * @return array
     * @deprecated 2.0.0 You can directly call find like
     *   `find('search', ['search' => $this->request->query])`
     */
    public function filterParams($params)
    {
        return ['search' => $params];
    }

    /**
     * Returns the search filter manager.
     *
     * @return \Search\Manager
     */
    public function searchManager()
    {
        if ($this->_manager === null) {
{'fraction_non_alphanumeric': 0.12560306105473298, 'fraction_numerical': 0.000998170021627017, 'mean_word_length': 1.9528487229862475, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '3937326', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1615, 'n_words': 481}