|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Psi\Component\Grid\Filter; |
|
6
|
|
|
|
|
7
|
|
|
use Psi\Component\ObjectAgent\Query\Comparison; |
|
8
|
|
|
use Psi\Component\ObjectAgent\Query\Expression; |
|
9
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
|
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\NumberType; |
|
11
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
13
|
|
|
|
|
14
|
|
|
class NumberFilter extends AbstractComparatorFilter |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->addComparatorChoice($builder, $options); |
|
22
|
|
|
|
|
23
|
|
|
$builder->add('value', NumberType::class, [ |
|
24
|
|
|
'required' => false, |
|
25
|
|
|
]); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getExpression(string $fieldName, array $data): Expression |
|
32
|
|
|
{ |
|
33
|
|
|
$comparator = $data['comparator'] ?: Comparison::EQUALS; |
|
34
|
|
|
|
|
35
|
|
|
return Query::comparison( |
|
36
|
|
|
$comparator, |
|
37
|
|
|
$fieldName, |
|
38
|
|
|
$data['value'] |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function configureOptions(OptionsResolver $options) |
|
46
|
|
|
{ |
|
47
|
|
|
$options->setDefault('comparators', $this->getComparatorMap()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function isApplicable(array $filterData): bool |
|
54
|
|
|
{ |
|
55
|
|
|
return isset($filterData['value']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function getComparatorMap(): array |
|
59
|
|
|
{ |
|
60
|
|
|
$supported = [ |
|
61
|
|
|
Comparison::EQUALS, |
|
62
|
|
|
Comparison::NOT_EQUALS, |
|
63
|
|
|
Comparison::GREATER_THAN, |
|
64
|
|
|
Comparison::GREATER_THAN_EQUAL, |
|
65
|
|
|
Comparison::LESS_THAN_EQUAL, |
|
66
|
|
|
Comparison::LESS_THAN, |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
return array_combine($supported, $supported); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|