<commit_before><?php

namespace Oro\Bundle\GridBundle\Datagrid;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Form;
use Sonata\AdminBundle\Filter\FilterInterface as SonataFilterInterface;
use Oro\Bundle\GridBundle\Filter\FilterInterface;
use Oro\Bundle\GridBundle\Field\FieldDescriptionCollection;
use Oro\Bundle\GridBundle\Sorter\SorterInterface;

class Datagrid implements DatagridInterface
{
    /**
     * @var ProxyQueryInterface
     */
    protected $query;

    /**
     * @var FieldDescriptionCollection
     */
    protected $columns;

    /**
     * @var PagerInterface
     */
    protected $pager;

    /**
     * @var FormBuilderInterface
     */
    protected $formBuilder;

    /**
     * Parameters applied flag
     *
     * @var bool
     */
    protected $parametersApplied = false;

    /**
     * @var ParametersInterface
     */
    protected $parameters;

    /**
     * @var array
     */
    protected $filters;

    /**
     * @var SorterInterface[]
     */
    protected $sorters = array();

    /**
     * @var Form
     */
    protected $form;

    /**
     * @param ProxyQueryInterface $query
     * @param FieldDescriptionCollection $columns
     * @param PagerInterface $pager
     * @param FormBuilderInterface $formBuilder
     * @param ParametersInterface $parameters
     */
    public function __construct(
        ProxyQueryInterface $query,
        FieldDescriptionCollection $columns,
        PagerInterface $pager,
        FormBuilderInterface $formBuilder,
        ParametersInterface $parameters
    ) {
        // TODO Empty $parameters is not acceptible, maybe create default value instead (null object pattern?)
        $this->query       = $query;
        $this->columns     = $columns;
        $this->pager       = $pager;
        $this->formBuilder = $formBuilder;
        $this->parameters  = $parameters;
    }

    /**
     * @param SonataFilterInterface $filter
     * @return void
     */
    public function addFilter(SonataFilterInterface $filter)
    {
        $this->filters[$filter->getName()] = $filter;
    }

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

    /**
     * @param string $name
     *
     * @return SonataFilterInterface
     */
    public function getFilter($name)
    {
        return $this->hasFilter($name) ? $this->filters[$name] : null;
    }

    /**
     * @param string $name
     *
     * @return bool
     */
    public function hasFilter($name)
    {
        return isset($this->filters[$name]);
    }

    /**
     * @param string $name
     */
    public function removeFilter($name)
    {
        unset($this->filters[$name]);
    }

    /**
     * @return boolean
     */
    public function hasActiveFilters()
    {
        /** @var $filter FilterInterface */
        foreach ($this->filters as $name => $filter) {
            // TODO Add method to interface
            if ($filter->isActive()) {
                return true;
            }
        }

        return false;
    }

    /**
     * @param SorterInterface $sorter
     * @return void
     */
    public function addSorter(SorterInterface $sorter)
    {
        $this->sorters[$sorter->getName()] = $sorter;
    }

    /**
     * @return PagerInterface
     */
    public function getPager()
    {
        return $this->pager;
    }

    protected function applyParameters()
    {
        if ($this->parametersApplied) {
            return;
        }

        $this->applyFilters();
        $this->applySorters();
        $this->applyPager();

        $this->parametersApplied = true;
    }

    protected function applyFilters()
    {
        // TODO
    }

    /**
     * Add sorters on grid and apply requested sorting
     */
    protected function applySorters()
    {
        // we should retain an order in which sorters were added

        $sortBy = $this->parameters->get('_sort_by');
        $sortOrder = $this->parameters->get('_sort_order');

        $requestedSorters = array_combine(
            is_array($sortBy) ? $sortBy : array($sortBy),
            is_array($sortOrder) ? $sortOrder : array($sortOrder)
        );

        foreach($requestedSorters as $fieldName => $direction) {
            if (isset($this->sorters[$fieldName])) {
                $this->sorters[$fieldName]->apply($this->query, $direction);
            }
        }

        // TODO: add sorters in form builder
    }

    protected function applyPager()
    {<commit_msg>BAP-111: Implement basic filter functionality - implemented basic filters in DemoGridBundle<commit_after><?php

namespace Oro\Bundle\GridBundle\Datagrid;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Form;
use Sonata\AdminBundle\Filter\FilterInterface as SonataFilterInterface;
use Oro\Bundle\GridBundle\Filter\FilterInterface;
use Oro\Bundle\GridBundle\Field\FieldDescriptionCollection;
use Oro\Bundle\GridBundle\Sorter\SorterInterface;

class Datagrid implements DatagridInterface
{
    /**
     * @var ProxyQueryInterface
     */
    protected $query;

    /**
     * @var FieldDescriptionCollection
     */
    protected $columns;

    /**
     * @var PagerInterface
     */
    protected $pager;

    /**
     * @var FormBuilderInterface
     */
    protected $formBuilder;

    /**
     * Parameters applied flag
     *
     * @var bool
     */
    protected $parametersApplied = false;

    /**
     * @var ParametersInterface
     */
    protected $parameters;

    /**
     * @var array
     */
    protected $filters;

    /**
     * @var SorterInterface[]
     */
    protected $sorters = array();

    /**
     * @var Form
     */
    protected $form;

    /**
     * @param ProxyQueryInterface $query
     * @param FieldDescriptionCollection $columns
     * @param PagerInterface $pager
     * @param FormBuilderInterface $formBuilder
     * @param ParametersInterface $parameters
     */
    public function __construct(
        ProxyQueryInterface $query,
        FieldDescriptionCollection $columns,
        PagerInterface $pager,
        FormBuilderInterface $formBuilder,
        ParametersInterface $parameters
    ) {
        // TODO Empty $parameters is not acceptible, maybe create default value instead (null object pattern?)
        $this->query       = $query;
        $this->columns     = $columns;
        $this->pager       = $pager;
        $this->formBuilder = $formBuilder;
        $this->parameters  = $parameters;
    }

    /**
     * @param SonataFilterInterface $filter
     * @return void
     */
    public function addFilter(SonataFilterInterface $filter)
    {
        $this->filters[$filter->getName()] = $filter;
    }

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

    /**
     * @param string $name
     *
     * @return SonataFilterInterface
     */
    public function getFilter($name)
    {
        return $this->hasFilter($name) ? $this->filters[$name] : null;
    }

    /**
     * @param string $name
     *
     * @return bool
     */
    public function hasFilter($name)
    {
        return isset($this->filters[$name]);
    }

    /**
     * @param string $name
     */
    public function removeFilter($name)
    {
        unset($this->filters[$name]);
    }

    /**
     * @return boolean
     */
    public function hasActiveFilters()
    {
        /** @var $filter FilterInterface */
        foreach ($this->filters as $name => $filter) {
            // TODO Add method to interface
            if ($filter->isActive()) {
                return true;
            }
        }

        return false;
    }

    /**
     * @param SorterInterface $sorter
     * @return void
     */
    public function addSorter(SorterInterface $sorter)
    {
        $this->sorters[$sorter->getName()] = $sorter;
    }

    /**
     * @return PagerInterface
     */
    public function getPager()
    {
        return $this->pager;
    }

    protected function applyParameters()
    {
        if ($this->parametersApplied) {
            return;
        }

        $this->applyFilters();
        $this->applySorters();
        $this->applyPager();

        $this->parametersApplied = true;
    }

    /**
     * Apply filter data to ProxyQuery and add form fields
     */
    protected function applyFilters()
    {
        /** @var $filter Oro\Bundle\GridBundle\Filter\FilterInterface */
        foreach ($this->getFilters() as $name => $filter) {
            $filter->apply($this->query, array('value' => $this->parameters->get($name)));

            list($type, $options) = $filter->getRenderSettings();
            $this->formBuilder->add($filter->getFormName(), $type, $options);
        }
    }

    /**
     * Add sorters on grid and apply requested sorting
     */
    protected function applySorters()
    {
        // we should retain an order in which sorters were added

        $sortBy = $this->parameters->get('_sort_by');
        $sortOrder = $this->parameters->get('_sort_order');

        $requestedSorters = array_combine(
            is_array($sortBy) ? $sortBy : array($sortBy),
            is_array($sortOrder) ? $sortOrder : array($sortOrder)
        );

        foreach ($requestedSorters as $fieldName => $direction) {
            if (isset($this->sorters[$fieldName])) {
                $this->sorters[$fieldName]->apply($this->query, $direction);
            }
        }

        // TODO: add sorters in form builder
    }

    protected function applyPager()
    {
{'fraction_non_alphanumeric': 0.12323168814838101, 'fraction_numerical': 0.00031436655139893113, 'mean_word_length': 2.064868336544637, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 78, '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': '5750433', 'n_tokens_mistral': 2689, 'n_tokens_neox': 2631, 'n_words': 571}