1 | <?php |
||
19 | class StringFilter extends Filter |
||
20 | { |
||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data): void |
||
25 | { |
||
26 | if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || null === $data['value']) { |
||
|
|||
27 | return; |
||
28 | } |
||
29 | |||
30 | $value = trim((string) $data['value']); |
||
31 | $data['type'] = empty($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type']; |
||
32 | |||
33 | if (0 === \strlen($value)) { |
||
34 | return; |
||
35 | } |
||
36 | |||
37 | $where = $this->getWhere($proxyQuery); |
||
38 | $isComparisonLowerCase = $this->getOption('compare_case_insensitive'); |
||
39 | $value = $isComparisonLowerCase ? strtolower($value) : $value; |
||
40 | switch ($data['type']) { |
||
41 | case ChoiceType::TYPE_EQUAL: |
||
42 | if ($isComparisonLowerCase) { |
||
43 | $where->eq()->lowerCase()->field('a.'.$field)->end()->literal($value); |
||
44 | } else { |
||
45 | $where->eq()->field('a.'.$field)->literal($value); |
||
46 | } |
||
47 | |||
48 | break; |
||
49 | case ChoiceType::TYPE_NOT_CONTAINS: |
||
50 | $where->fullTextSearch('a.'.$field, '* -'.$value); |
||
51 | |||
52 | break; |
||
53 | case ChoiceType::TYPE_CONTAINS: |
||
54 | if ($isComparisonLowerCase) { |
||
55 | $where->like()->lowerCase()->field('a.'.$field)->end()->literal('%'.$value.'%'); |
||
56 | } else { |
||
57 | $where->like()->field('a.'.$field)->literal('%'.$value.'%'); |
||
58 | } |
||
59 | |||
60 | break; |
||
61 | case ChoiceType::TYPE_CONTAINS_WORDS: |
||
62 | default: |
||
63 | $where->fullTextSearch('a.'.$field, $value); |
||
64 | } |
||
65 | |||
66 | // filter is active as we have now modified the query |
||
67 | $this->active = true; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function getDefaultOptions() |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function getRenderSettings() |
||
92 | } |
||
93 |
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.