<commit_before><?php
namespace DataGrid\DataSource;

use DataGrid\CellSet;
use DataGrid\Exception;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Query\Expr;

class DoctrineSource implements DataSourceInterface, DataSourceFilterableInterface, DataSourcePaginatedInterface, DataSourceSortableInterface
{

    const QUERY_ALIAS_SEPARATOR = ':';

    private $source = null;
    private $cellssetDecorator = null;
    private $currentAlias = 't';

    private $queryResult = null;

    private $maxResults = null;
    private $page = null;

    private $iteratorPosition = 0;

    public function __construct()
    {
    }

    public function setSource($source)
    {
        if ($source instanceof EntityRepository) {
            $this->source = $source->createQueryBuilder($this->currentAlias);
        } elseif ($source instanceof QueryBuilder) {
            $this->source = $source;
            /**
             * @vr \Doctrine\ORM\Query\Expr\From $from
             */
            $from = $this->source->getDQLPart('from')[0];
            $this->setCurrentAlias($from->getAlias());
        } else {
            throw new Exception('Doctrine Adapter can handle only doctrine repository or Doctrine Query Builder');
        }
        return $this;
    }

    public function orFilterGroup($fields, $joiner='OR')
    {
        $conditions = array();
        foreach($fields as $field=>$data){
            $conditions[] = $this->compileFilterParams($field, $data['value'], $data['type']);
        }
        $this->source->orWhere('(' . implode(' ' . $joiner . ' ', $conditions) . ')');
    }

    public function andFilterGroup($fields, $joiner='OR')
    {
        $conditions = array();
        foreach($fields as $field=>$data){
            $conditions[] = $this->compileFilterParams($field, $data['value'], $data['type']);
        }
        $this->source->andWhere('(' . implode(' ' . $joiner . ' ', $conditions) . ')');
    }

    public function andFilter($field, $value, $type = self::FILTER_TYPE_EQ)
    {
        $this->source->andWhere($this->compileFilterParams($field, $value, $type));
        return $this;
    }

    public function orFilter($field, $value, $type = self::FILTER_TYPE_EQ)
    {
        $this->source->orWhere($this->compileFilterParams($field, $value, $type));
        return $this;
    }

    protected function compileFilterParams($field, $value, $type = self::FILTER_TYPE_EQ)
    {
        $dbField = !strpos($field, '.') ? $this->currentAlias . '.' . $field : $field;
        $paramName = str_replace('.', '_', $field) . '_' . uniqid();
        switch ($type) {
            case self::FILTER_TYPE_EQ:
                $condition = $dbField . ' = :' . $paramName;
                $this->source->setParameter($paramName, $value);
                break;
            case self::FILTER_YPE_CONTAINS:
                $condition = $dbField . ' LIKE :' . $paramName;
                $this->source->setParameter($paramName, '%' . $value . '%');
                break;
            default:
                throw new \RuntimeException('Filter comparison type "' . $type . '" is not supported by Doctrine grid source');
        }
        return $condition;
    }

    public function clearFilters()
    {
        $this->source->resetDQLPart('where');
        $this->source->setParameters(array());
        return $this;
    }

    public function setMaxResults($maxResults)
    {
        $this->maxResults = $maxResults;
        return $this;
    }

    public function getMaxResults()
    {
        return $this->maxResults;
    }

    public function setPage($page)
    {
        $this->page = $page;
        return $this;
    }
<commit_msg>updated filter for doctrine adapter<commit_after><?php
namespace DataGrid\DataSource;

use DataGrid\CellSet;
use DataGrid\Exception;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Query\Expr;

class DoctrineSource implements DataSourceInterface, DataSourceFilterableInterface, DataSourcePaginatedInterface, DataSourceSortableInterface
{

    const QUERY_ALIAS_SEPARATOR = ':';

    private $source = null;
    private $cellssetDecorator = null;
    private $currentAlias = 't';

    private $queryResult = null;

    private $maxResults = null;
    private $page = null;

    private $iteratorPosition = 0;

    public function __construct()
    {
    }

    public function setSource($source)
    {
        if ($source instanceof EntityRepository) {
            $this->source = $source->createQueryBuilder($this->currentAlias);
        } elseif ($source instanceof QueryBuilder) {
            $this->source = $source;
            /**
             * @vr \Doctrine\ORM\Query\Expr\From $from
             */
            $from = $this->source->getDQLPart('from')[0];
            $this->setCurrentAlias($from->getAlias());
        } else {
            throw new Exception('Doctrine Adapter can handle only doctrine repository or Doctrine Query Builder');
        }
        return $this;
    }

    public function orFilterGroup($fields, $joiner='OR')
    {
        $conditions = array();
        foreach($fields as $field=>$data){
            $conditions[] = $this->compileFilterParams($field, $data['value'], $data['type']);
        }
        $this->source->orWhere('(' . implode(' ' . $joiner . ' ', $conditions) . ')');
    }

    public function andFilterGroup($fields, $joiner='OR')
    {
        $conditions = array();
        foreach($fields as $field=>$data){
            $conditions[] = $this->compileFilterParams($field, $data['value'], $data['type']);
        }
        $this->source->andWhere('(' . implode(' ' . $joiner . ' ', $conditions) . ')');
    }

    public function andFilter($field, $value, $type = self::FILTER_TYPE_EQ)
    {
        $this->source->andWhere($this->compileFilterParams($field, $value, $type));
        return $this;
    }

    public function orFilter($field, $value, $type = self::FILTER_TYPE_EQ)
    {
        $this->source->orWhere($this->compileFilterParams($field, $value, $type));
        return $this;
    }

    protected function compileFilterParams($field, $value, $type = self::FILTER_TYPE_EQ)
    {
        $dbField = !strpos($field, '.') ? $this->currentAlias . '.' . $field : $field;
        $paramName = str_replace('.', '_', $field) . '_' . uniqid();
        switch ($type) {
            case self::FILTER_TYPE_EQ:
                $condition = $dbField . ' = :' . $paramName;
                $this->source->setParameter($paramName, $value);
                break;
            case self::FILTER_YPE_CONTAINS:
                $condition = $dbField . ' LIKE :' . $paramName;
                $this->source->setParameter($paramName, '%' . $value . '%');
                break;
            default:
                throw new \RuntimeException('Filter comparison type "' . $type . '" is not supported by Doctrine grid source');
        }
        return $condition;
    }

    public function clearFilters()
    {
        //$this->source->resetDQLPart('where');
        $this->source->setParameters(array());
        return $this;
    }

    public function setMaxResults($maxResults)
    {
        $this->maxResults = $maxResults;
        return $this;
    }

    public function getMaxResults()
    {
        return $this->maxResults;
    }

    public function setPage($page)
    {
        $this->page = $page;
        return $this;
    }

{'fraction_non_alphanumeric': 0.14690026954177898, 'fraction_numerical': 0.0005390835579514825, 'mean_word_length': 2.411954022988506, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, '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': '4037466', 'n_tokens_mistral': 2120, 'n_tokens_neox': 2075, 'n_words': 434}