|
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\DoctrineMongoDBAdminBundle\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
|
|
|
* @param ProxyQueryInterface $queryBuilder |
|
23
|
|
|
* @param string $alias |
|
24
|
|
|
* @param string $field |
|
25
|
|
|
* @param string $data |
|
26
|
|
|
*/ |
|
27
|
|
|
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data) |
|
28
|
|
|
{ |
|
29
|
|
|
if (!$data || !\is_array($data) || !array_key_exists('value', $data) || !is_numeric($data['value'])) { |
|
30
|
|
|
return; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$type = $data['type'] ?? false; |
|
34
|
|
|
|
|
35
|
|
|
$operator = $this->getOperator($type); |
|
36
|
|
|
|
|
37
|
|
|
if (!$operator) { |
|
|
|
|
|
|
38
|
|
|
$operator = 'equals'; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$queryBuilder->field($field)->$operator((float) $data['value']); |
|
42
|
|
|
$this->active = true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getDefaultOptions() |
|
49
|
|
|
{ |
|
50
|
|
|
return []; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getRenderSettings() |
|
54
|
|
|
{ |
|
55
|
|
|
return [NumberType::class, [ |
|
56
|
|
|
'field_type' => $this->getFieldType(), |
|
57
|
|
|
'field_options' => $this->getFieldOptions(), |
|
58
|
|
|
'label' => $this->getLabel(), |
|
59
|
|
|
]]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $type |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
private function getOperator($type) |
|
68
|
|
|
{ |
|
69
|
|
|
$choices = [ |
|
70
|
|
|
NumberType::TYPE_EQUAL => 'equals', |
|
71
|
|
|
NumberType::TYPE_GREATER_EQUAL => 'gte', |
|
72
|
|
|
NumberType::TYPE_GREATER_THAN => 'gt', |
|
73
|
|
|
NumberType::TYPE_LESS_EQUAL => 'lte', |
|
74
|
|
|
NumberType::TYPE_LESS_THAN => 'lt', |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
return $choices[$type] ?? false; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: