<commit_before><?php

/**
 * Author: <NAME> <<EMAIL>>
 * Date: 7/02/16
 * Time: 16:06.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace NilPortugues\Foundation\Infrastructure\Model\Repository\Sql;

use Doctrine\DBAL\Query\QueryBuilder;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\BaseFilter;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter as FilterInterface;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Mapping;

/**
 * Class SqlFilter.
 */
class SqlFilter
{
    const MUST_NOT = 'must_not';
    const MUST = 'must';
    const SHOULD = 'should';

    /**
     * @param QueryBuilder    $query
     * @param FilterInterface $filter
     * @param Mapping         $mapping
     *
     * @return QueryBuilder
     */
    public static function filter(QueryBuilder $query, FilterInterface $filter, Mapping $mapping)
    {
        $placeholders = [];
        $columns = $mapping->map();

        foreach ($filter->filters() as $condition => $filters) {
            $filters = self::removeEmptyFilters($filters);
            if (count($filters) > 0) {
                self::processConditions($columns, $placeholders, $query, $condition, $filters);
            }
        }

        $query->setParameters($placeholders);

        return $query;
    }

    /**
     * @param array $filters
     *
     * @return array
     */
    private static function removeEmptyFilters(array $filters)
    {
        $filters = array_filter($filters, function ($v) {
            return count($v) > 0;
        });<commit_msg>Apply filter if parameter matches a mapped value only<commit_after><?php

/**
 * Author: <NAME> <<EMAIL>>
 * Date: 7/02/16
 * Time: 16:06.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace NilPortugues\Foundation\Infrastructure\Model\Repository\Sql;

use Doctrine\DBAL\Query\QueryBuilder;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\BaseFilter;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter as FilterInterface;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Mapping;

/**
 * Class SqlFilter.
 */
class SqlFilter
{
    const MUST_NOT = 'must_not';
    const MUST = 'must';
    const SHOULD = 'should';

    /**
     * @param QueryBuilder    $query
     * @param FilterInterface $filter
     * @param Mapping         $mapping
     *
     * @return QueryBuilder
     */
    public static function filter(QueryBuilder $query, FilterInterface $filter, Mapping $mapping)
    {
        $placeholders = [];
        $columns = $mapping->map();

        foreach ($filter->filters() as $condition => $filters) {
            if (array_key_exists($condition, $columns)) {
                $filters = self::removeEmptyFilters($filters);
                if (count($filters) > 0) {
                    self::processConditions($columns, $placeholders, $query, $condition, $filters);
                }
            }
        }

        $query->setParameters($placeholders);

        return $query;
    }

    /**
     * @param array $filters
     *
     * @return array
     */
    private static function removeEmptyFilters(array $filters)
    {
        $filters = array_filter($filters, function ($v) {
            return count($v) > 0;
        });
{'fraction_non_alphanumeric': 0.12208624708624709, 'fraction_numerical': 0.00641025641025641, 'mean_word_length': 2.6677350427350426, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '368298', 'n_tokens_mistral': 987, 'n_tokens_neox': 937, 'n_words': 231}