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\AdminBundle\Form\Type\Filter\NumberType; |
18
|
|
|
|
19
|
|
|
class NumberFilter 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) || !is_numeric($data['value'])) { |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$type = $data['type'] ?? false; |
31
|
|
|
$where = $this->getWhere($proxyQuery); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$value = $data['value']; |
34
|
|
|
|
35
|
|
|
switch ($type) { |
36
|
|
|
case NumberType::TYPE_GREATER_EQUAL: |
37
|
|
|
$where->gte()->field('a.'.$field)->literal($value); |
38
|
|
|
|
39
|
|
|
break; |
40
|
|
|
case NumberType::TYPE_GREATER_THAN: |
41
|
|
|
$where->gt()->field('a.'.$field)->literal($value); |
42
|
|
|
|
43
|
|
|
break; |
44
|
|
|
case NumberType::TYPE_LESS_EQUAL: |
45
|
|
|
$where->lte()->field('a.'.$field)->literal($value); |
46
|
|
|
|
47
|
|
|
break; |
48
|
|
|
case NumberType::TYPE_LESS_THAN: |
49
|
|
|
$where->lt()->field('a.'.$field)->literal($value); |
50
|
|
|
|
51
|
|
|
break; |
52
|
|
|
case NumberType::TYPE_EQUAL: |
53
|
|
|
default: |
54
|
|
|
$where->eq()->field('a.'.$field)->literal($value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// filter is active as we have now modified the query |
58
|
|
|
$this->active = true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function getDefaultOptions() |
65
|
|
|
{ |
66
|
|
|
return []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function getRenderSettings() |
73
|
|
|
{ |
74
|
|
|
return ['sonata_type_filter_number', [ |
75
|
|
|
'field_type' => $this->getFieldType(), |
76
|
|
|
'field_options' => $this->getFieldOptions(), |
77
|
|
|
'label' => $this->getLabel(), |
78
|
|
|
]]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.