|
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\NumberType; |
|
18
|
|
|
|
|
19
|
|
|
class NumberFilter extends Filter |
|
20
|
|
|
{ |
|
21
|
|
|
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data) |
|
22
|
|
|
{ |
|
23
|
|
|
if (!$data || !\is_array($data) || !array_key_exists('value', $data) || !is_numeric($data['value'])) { |
|
|
|
|
|
|
24
|
|
|
return; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$type = $data['type'] ?? false; |
|
28
|
|
|
|
|
29
|
|
|
$operator = $this->getOperator($type); |
|
30
|
|
|
|
|
31
|
|
|
if (!$operator) { |
|
|
|
|
|
|
32
|
|
|
$operator = '='; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// c.name > '1' => c.name OPERATOR :FIELDNAME |
|
36
|
|
|
$parameterName = $this->getNewParameterName($queryBuilder); |
|
37
|
|
|
$this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName)); |
|
38
|
|
|
$queryBuilder->setParameter($parameterName, $data['value']); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getDefaultOptions() |
|
42
|
|
|
{ |
|
43
|
|
|
return []; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getRenderSettings() |
|
47
|
|
|
{ |
|
48
|
|
|
return [NumberType::class, [ |
|
49
|
|
|
'field_type' => $this->getFieldType(), |
|
50
|
|
|
'field_options' => $this->getFieldOptions(), |
|
51
|
|
|
'label' => $this->getLabel(), |
|
52
|
|
|
]]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $type |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
private function getOperator($type) |
|
61
|
|
|
{ |
|
62
|
|
|
$choices = [ |
|
63
|
|
|
NumberType::TYPE_EQUAL => '=', |
|
64
|
|
|
NumberType::TYPE_GREATER_EQUAL => '>=', |
|
65
|
|
|
NumberType::TYPE_GREATER_THAN => '>', |
|
66
|
|
|
NumberType::TYPE_LESS_EQUAL => '<=', |
|
67
|
|
|
NumberType::TYPE_LESS_THAN => '<', |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
return $choices[$type] ?? false; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
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.