<commit_before><?php

/*
 * This file is part of the Sonata package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\AdminBundle\Filter\ORM;

use Symfony\Component\Form\FormFactory;
use Doctrine\ORM\QueryBuilder;

class CallbackFilter extends Filter
{
    protected function association($queryBuilder, $value)
    {
        return array($queryBuilder->getRootAlias(), false);
    }

    public function filter($queryBuilder, $alias, $field, $value)
    {
        if (!is_callable($this->getOption('filter'))) {
            throw new \RuntimeException('Please provide a valid callback option');
        }

        call_user_func($this->getOption('filter'), $queryBuilder, $alias, $field, $value);
    }

    /**
     *    $this->filter_fields['custom'] = array(
     *        'type'           => 'callback',
     *        'filter_options' => array(
     *           'filter'  => array($this, 'getCustomFilter'),
     *           'type'    => 'type_name'
     *       )
     *    );
     *<commit_msg>Add more helpful error message to CallbackFilter<commit_after><?php

/*
 * This file is part of the Sonata package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\AdminBundle\Filter\ORM;

use Symfony\Component\Form\FormFactory;
use Doctrine\ORM\QueryBuilder;

class CallbackFilter extends Filter
{
    protected function association($queryBuilder, $value)
    {
        return array($queryBuilder->getRootAlias(), false);
    }

    public function filter($queryBuilder, $alias, $field, $value)
    {
        if (!is_callable($this->getOption('filter'))) {
            throw new \RuntimeException('Please provide a valid callback option "filter" for field "' . $this->getName() . "'");
        }

        call_user_func($this->getOption('filter'), $queryBuilder, $alias, $field, $value);
    }

    /**
     *    $this->filter_fields['custom'] = array(
     *        'type'           => 'callback',
     *        'filter_options' => array(
     *           'filter'  => array($this, 'getCustomFilter'),
     *           'type'    => 'type_name'
     *       )
     *    );
     *
{'fraction_non_alphanumeric': 0.1379752242631354, 'fraction_numerical': 0.0, 'mean_word_length': 2.625386996904025, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 26, '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': '7221942', 'n_tokens_mistral': 671, 'n_tokens_neox': 666, 'n_words': 170}