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\Filter\Filter as BaseFilter; |
18
|
|
|
use Sonata\Form\Type\BooleanType; |
19
|
|
|
|
20
|
|
|
class BooleanFilter extends BaseFilter |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data): void |
26
|
|
|
{ |
27
|
|
|
if (!$data || !\is_array($data) || !\array_key_exists('type', $data) || !\array_key_exists('value', $data)) { |
|
|
|
|
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (\is_array($data['value']) || !\in_array($data['value'], [BooleanType::TYPE_NO, BooleanType::TYPE_YES], true)) { |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$where = $this->getWhere($proxyQuery); |
|
|
|
|
36
|
|
|
$where->eq()->field('a.'.$field)->literal(BooleanType::TYPE_YES === $data['value'] ? true : false); |
37
|
|
|
|
38
|
|
|
// filter is active as we have now modified the query |
39
|
|
|
$this->active = true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function getDefaultOptions() |
46
|
|
|
{ |
47
|
|
|
return []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function getRenderSettings() |
54
|
|
|
{ |
55
|
|
|
return ['sonata_type_filter_default', [ |
56
|
|
|
'field_type' => $this->getFieldType(), |
57
|
|
|
'field_options' => $this->getFieldOptions(), |
58
|
|
|
'operator_type' => 'hidden', |
59
|
|
|
'operator_options' => [], |
60
|
|
|
'label' => $this->getLabel(), |
61
|
|
|
]]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
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.