Completed
Push — master ( d1f538...a2a57f )
by
unknown
02:04 queued 19s
created

ChoiceFilter::filterWithMultipleValues()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.7537
c 0
b 0
f 0
cc 6
nc 6
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineORMAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
18
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
19
use Sonata\Form\Type\EqualType;
20
21
class ChoiceFilter extends Filter
22
{
23
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
24
    {
25
        if (!$data || !\is_array($data) || !\array_key_exists('type', $data) || !\array_key_exists('value', $data)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
26
            return;
27
        }
28
29
        if (\is_array($data['value'])) {
30
            $this->filterWithMultipleValues($queryBuilder, $alias, $field, $data);
31
        } else {
32
            $this->filterWithSingleValue($queryBuilder, $alias, $field, $data);
33
        }
34
    }
35
36
    public function getDefaultOptions()
37
    {
38
        return [
39
            'operator_type' => EqualType::class,
40
            'operator_options' => [],
41
        ];
42
    }
43
44
    public function getRenderSettings()
45
    {
46
        return [DefaultType::class, [
47
            'operator_type' => $this->getOption('operator_type'),
48
            'operator_options' => $this->getOption('operator_options'),
49
            'field_type' => $this->getFieldType(),
50
            'field_options' => $this->getFieldOptions(),
51
            'label' => $this->getLabel(),
52
        ]];
53
    }
54
55
    private function filterWithMultipleValues(ProxyQueryInterface $queryBuilder, string $alias, string $field, array $data = []): void
56
    {
57
        if (0 === \count($data['value'])) {
58
            return;
59
        }
60
61
        if (\in_array('all', $data['value'], true)) {
62
            return;
63
        }
64
65
        $isNullSelected = \in_array(null, $data['value'], true);
66
        $data['value'] = array_filter($data['value'], static function ($value): bool {
67
            return null !== $value;
68
        });
69
70
        // Have to pass IN array value as parameter. See: http://www.doctrine-project.org/jira/browse/DDC-3759
71
        $completeField = sprintf('%s.%s', $alias, $field);
72
        $parameterName = $this->getNewParameterName($queryBuilder);
73
        if (ChoiceType::TYPE_NOT_CONTAINS === $data['type']) {
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...Type::TYPE_NOT_CONTAINS has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_NOT_CONTAINS instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
74
            $andConditions = [$queryBuilder->expr()->isNotNull($completeField)];
75
            if (0 !== \count($data['value'])) {
76
                $andConditions[] = $queryBuilder->expr()->notIn($completeField, ':'.$parameterName);
77
                $queryBuilder->setParameter($parameterName, $data['value']);
78
            }
79
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->andX()->addMultiple($andConditions));
80
        } else {
81
            $orConditions = [$queryBuilder->expr()->in($completeField, ':'.$parameterName)];
82
            if ($isNullSelected) {
83
                $orConditions[] = $queryBuilder->expr()->isNull($completeField);
84
            }
85
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->orX()->addMultiple($orConditions));
86
            $queryBuilder->setParameter($parameterName, $data['value']);
87
        }
88
    }
89
90
    private function filterWithSingleValue(ProxyQueryInterface $queryBuilder, string $alias, string $field, array $data = []): void
91
    {
92
        if ('' === $data['value'] || false === $data['value'] || 'all' === $data['value']) {
93
            return;
94
        }
95
96
        $parameterName = $this->getNewParameterName($queryBuilder);
97
98
        if (ChoiceType::TYPE_NOT_CONTAINS === $data['type']) {
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...Type::TYPE_NOT_CONTAINS has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_NOT_CONTAINS instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
99
            if (null === $data['value']) {
100
                $this->applyWhere($queryBuilder, $queryBuilder->expr()->isNotNull(sprintf('%s.%s', $alias, $field)));
101
            } else {
102
                $this->applyWhere($queryBuilder, sprintf('%s.%s != :%s', $alias, $field, $parameterName));
103
                $queryBuilder->setParameter($parameterName, $data['value']);
104
            }
105
        } else {
106
            if (null === $data['value']) {
107
                $this->applyWhere($queryBuilder, $queryBuilder->expr()->isNull(sprintf('%s.%s', $alias, $field)));
108
            } else {
109
                $this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $parameterName));
110
                $queryBuilder->setParameter($parameterName, $data['value']);
111
            }
112
        }
113
    }
114
}
115