<commit_before><?php

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

namespace Wizad\CrudBundle\Manager;

use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\EntityRepository;
use Wizad\CrudBundle\Model\FilterModel;


abstract class EntityFilterManager
{
    /**
     * @return EntityRepository
     */
    public abstract function getEntityRepository();

    /**
     * @param FilterModel $filter
     *
     * @return mixed
     */
    public function findByFilter(FilterModel $filter)
    {
        $query = $this->createQueryByFilter($filter);

        return $query->getQuery()->execute();
    }

    /**
     * @param FilterModel $filter
     *
     * @return QueryBuilder
     */
    public function createQueryByFilter(FilterModel $filter)
    {
        $qb = $this->getEntityRepository()->createQueryBuilder('entity');
        $this->applyFilter($qb, $filter);

        return $qb;
    }

    /**
     *
     * @param \Doctrine\ORM\QueryBuilder $qb
     * @param FilterModel                $filter
     *
     * @return QueryBuilder
     */
    public function applyFilter(QueryBuilder $qb, FilterModel $filter)
    {
        foreach ($filter->getFilters() as $propertyName => $property) {

            $value       = $filter->getFilterValue($propertyName);
            $queryMethod = sprintf('filter%s', ucfirst($propertyName));

            if (method_exists($this, $queryMethod)) {
                $this->$queryMethod($qb, $filter, $value);
            } else {
                if ($value !== null) {
                    if (!is_array($value)) {
                        $qb
                            ->andWhere(
                                $qb->expr()->like(sprintf('entity.%s', $propertyName), sprintf(':%s', $propertyName))
                            )
                            ->setParameter($propertyName, '%' . $value . '%');
                    } elseif (!empty($value)) {
                        $qb
                            ->andWhere(
                                $qb->expr()->in(sprintf('entity.%s', $propertyName), sprintf(':%s', $propertyName))
                            )
                            ->setParameter($propertyName, $value);
                    }
                }
            }
        }

        return $qb;
    }
}<commit_msg>add sorting support on entity filter manager<commit_after><?php

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

namespace Wizad\CrudBundle\Manager;

use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\EntityRepository;
use Wizad\CrudBundle\Model\FilterModel;


abstract class EntityFilterManager
{
    /**
     * @return EntityRepository
     */
    public abstract function getEntityRepository();

    /**
     * @param FilterModel $filter
     *
     * @return mixed
     */
    public function findByFilter(FilterModel $filter)
    {
        $query = $this->createQueryByFilter($filter);

        return $query->getQuery()->execute();
    }

    /**
     * @param FilterModel $filter
     *
     * @return QueryBuilder
     */
    public function createQueryByFilter(FilterModel $filter)
    {
        $qb = $this->getEntityRepository()->createQueryBuilder('entity');
        $this->applyFilter($qb, $filter);

        return $qb;
    }

    /**
     *
     * @param \Doctrine\ORM\QueryBuilder $qb
     * @param FilterModel                $filter
     *
     * @return QueryBuilder
     */
    public function applyFilter(QueryBuilder $qb, FilterModel $filter)
    {
        foreach ($filter->getFilters() as $propertyName => $property) {

            $value       = $filter->getFilterValue($propertyName);
            $queryMethod = sprintf('filter%s', ucfirst($propertyName));

            if (method_exists($this, $queryMethod)) {
                $this->$queryMethod($qb, $filter, $value);
            } else {
                if ($value !== null) {
                    if (!is_array($value)) {
                        $qb
                            ->andWhere(
                                $qb->expr()->like(sprintf('entity.%s', $propertyName), sprintf(':%s', $propertyName))
                            )
                            ->setParameter($propertyName, '%' . $value . '%');
                    } elseif (!empty($value)) {
                        $qb
                            ->andWhere(
                                $qb->expr()->in(sprintf('entity.%s', $propertyName), sprintf(':%s', $propertyName))
                            )
                            ->setParameter($propertyName, $value);
                    }
                }
            }
        }

        if (strlen($filter->getSort()) > 0) {
            $qb->orderBy(sprintf('entity.%s', $filter->getSort()), $filter->getSortMode());
        }

        return $qb;
    }
}
{'fraction_non_alphanumeric': 0.13330711757766417, 'fraction_numerical': 0.00019661816751867872, 'mean_word_length': 1.6773684210526316, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 50, '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': '947414', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1345, 'n_words': 276}