<commit_before><?php

namespace Kunstmaan\AdminListBundle\AdminList;

use Kunstmaan\AdminListBundle\AdminList\FilterType\FilterTypeInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * AdminListFilter.
 */
class FilterBuilder
{
    /**
     * @var array
     */
    private $filterDefinitions = [];

    /**
     * @var Filter[]
     */
    private $currentFilters = [];

    /**
     * @var array
     */
    private $currentParameters = [];

    /**
     * @param string              $columnName The column name
     * @param FilterTypeInterface $type       The filter type
     * @param string              $filterName The name of the filter
     * @param array               $options    Options
     *
     * @return FilterBuilder
     */
    public function add($columnName, FilterTypeInterface $type = null, $filterName = null, array $options = [])
    {
        $this->filterDefinitions[$columnName] = [
            'type' => $type,
            'options' => $options,
            'filtername' => $filterName,
        ];

        return $this;
    }

    /**
     * @param string $columnName
     *
     * @return null|mixed
     */
    public function get($columnName)
    {
        if (isset($this->filterDefinitions[$columnName])) {
            return $this->filterDefinitions[$columnName];
        }

        return null;
    }

    /**
     * @param string $columnName
     *
     * @return FilterBuilder
     */
    public function remove($columnName)
    {
        if (isset($this->filterDefinitions[$columnName])) {
            unset($this->filterDefinitions[$columnName]);
        }

        return $this;
    }

    /**
     * @param string $columnName
     *
     * @return bool
     */
    public function has($columnName)
    {
        return isset($this->filterDefinitions[$columnName]);
    }

    /**
     * @return array
     */
    public function getFilterDefinitions()
    {
        return $this->filterDefinitions;
    }

    /**
     * @param Request $request
     * @param array $defaults
     */
    public function bindRequest(Request $request, array $defaults = [])
    {
        $filterBuilderName = 'filter_'.$request->get('_route');
        $this->currentParameters = $request->query->all();
        unset($this->currentParameters['pagesize']);
        if (0 === count($this->currentParameters)) {
            if (!$request->query->has('filter')) {
                if ($request->getSession()->has($filterBuilderName)) {
                    $savedQuery = $request->getSession()->get($filterBuilderName);
                    $request->query->replace($savedQuery);
                    $this->currentParameters = $savedQuery;
                } else if ((!empty($defaults)) {
                    $request->query->replace($defaults);
                    $this->currentParameters = $defaults;
                }
            }
        } else {
            $request->getSession()->set($filterBuilderName, $this->currentParameters);
        }

        $filterColumnNames = $request->query->get('filter_columnname');
        if (isset($filterColumnNames)) {
            $uniqueIds = $request->query->get('filter_uniquefilterid');
            $index = 0;
            foreach ($filterColumnNames as $filterColumnName) {
                $uniqueId = $uniqueIds[$index];
                $filter = new Filter($filterColumnName, $this->get($filterColumnName), $uniqueId);<commit_msg>Update FilterBuilder.php<commit_after><?php

namespace Kunstmaan\AdminListBundle\AdminList;

use Kunstmaan\AdminListBundle\AdminList\FilterType\FilterTypeInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * AdminListFilter.
 */
class FilterBuilder
{
    /**
     * @var array
     */
    private $filterDefinitions = [];

    /**
     * @var Filter[]
     */
    private $currentFilters = [];

    /**
     * @var array
     */
    private $currentParameters = [];

    /**
     * @param string              $columnName The column name
     * @param FilterTypeInterface $type       The filter type
     * @param string              $filterName The name of the filter
     * @param array               $options    Options
     *
     * @return FilterBuilder
     */
    public function add($columnName, FilterTypeInterface $type = null, $filterName = null, array $options = [])
    {
        $this->filterDefinitions[$columnName] = [
            'type' => $type,
            'options' => $options,
            'filtername' => $filterName,
        ];

        return $this;
    }

    /**
     * @param string $columnName
     *
     * @return null|mixed
     */
    public function get($columnName)
    {
        if (isset($this->filterDefinitions[$columnName])) {
            return $this->filterDefinitions[$columnName];
        }

        return null;
    }

    /**
     * @param string $columnName
     *
     * @return FilterBuilder
     */
    public function remove($columnName)
    {
        if (isset($this->filterDefinitions[$columnName])) {
            unset($this->filterDefinitions[$columnName]);
        }

        return $this;
    }

    /**
     * @param string $columnName
     *
     * @return bool
     */
    public function has($columnName)
    {
        return isset($this->filterDefinitions[$columnName]);
    }

    /**
     * @return array
     */
    public function getFilterDefinitions()
    {
        return $this->filterDefinitions;
    }

    /**
     * @param Request $request
     * @param array $defaults
     */
    public function bindRequest(Request $request, array $defaults = [])
    {
        $filterBuilderName = 'filter_'.$request->get('_route');
        $this->currentParameters = $request->query->all();
        unset($this->currentParameters['pagesize']);
        if (0 === count($this->currentParameters)) {
            if (!$request->query->has('filter')) {
                if ($request->getSession()->has($filterBuilderName)) {
                    $savedQuery = $request->getSession()->get($filterBuilderName);
                    $request->query->replace($savedQuery);
                    $this->currentParameters = $savedQuery;
                } else if (!empty($defaults)) {
                    $request->query->replace($defaults);
                    $this->currentParameters = $defaults;
                }
            }
        } else {
            $request->getSession()->set($filterBuilderName, $this->currentParameters);
        }

        $filterColumnNames = $request->query->get('filter_columnname');
        if (isset($filterColumnNames)) {
            $uniqueIds = $request->query->get('filter_uniquefilterid');
            $index = 0;
            foreach ($filterColumnNames as $filterColumnName) {
                $uniqueId = $uniqueIds[$index];
                $filter = new Filter($filterColumnName, $this->get($filterColumnName), $uniqueId);
{'fraction_non_alphanumeric': 0.13733528550512444, 'fraction_numerical': 0.0005856515373352855, 'mean_word_length': 2.029268292682927, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, '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': '1214838', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1779, 'n_words': 336}