|
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\DoctrinePHPCRAdminBundle\Filter; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
|
17
|
|
|
use Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType; |
|
18
|
|
|
|
|
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() |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
'format' => '%%%s%%', |
|
77
|
|
|
'compare_lower_case' => false, |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getRenderSettings() |
|
85
|
|
|
{ |
|
86
|
|
|
return ['Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType', [ |
|
87
|
|
|
'field_type' => $this->getFieldType(), |
|
88
|
|
|
'field_options' => $this->getFieldOptions(), |
|
89
|
|
|
'label' => $this->getLabel(), |
|
90
|
|
|
]]; |
|
91
|
|
|
} |
|
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.