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
|
|
View Code Duplication |
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
|
|
|
$this->addComparatorChoice($builder, $options); |
24
|
|
|
$builder->add('value', DateType::class, []); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function getExpression(string $fieldName, array $data): Expression |
31
|
|
|
{ |
32
|
|
|
$comparator = $data['comparator'] ?: Comparison::EQUALS; |
33
|
|
|
|
34
|
|
|
return Query::comparison( |
35
|
|
|
$comparator, |
36
|
|
|
$fieldName, |
37
|
|
|
$data['value'] |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function isApplicable(array $filterData): bool |
45
|
|
|
{ |
46
|
|
|
return (bool) $filterData['apply']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function configureOptions(OptionsResolver $options) |
53
|
|
|
{ |
54
|
|
|
$options->setDefault('comparators', $this->getComparatorMap()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getComparatorMap(): array |
58
|
|
|
{ |
59
|
|
|
$supported = [ |
60
|
|
|
Comparison::EQUALS, |
61
|
|
|
Comparison::NOT_EQUALS, |
62
|
|
|
Comparison::GREATER_THAN, |
63
|
|
|
Comparison::GREATER_THAN_EQUAL, |
64
|
|
|
Comparison::LESS_THAN_EQUAL, |
65
|
|
|
Comparison::LESS_THAN, |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
return array_combine($supported, $supported); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.