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