1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Component\Grid\Filter; |
6
|
|
|
|
7
|
|
|
use Psi\Component\ObjectAgent\Capabilities; |
8
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
9
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
11
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
12
|
|
|
use Psi\Component\Grid\Metadata\GridMetadata; |
13
|
|
|
use Psi\Component\ObjectAgent\Query\Comparison; |
14
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
15
|
|
|
use Psi\Component\Grid\FilterInterface; |
16
|
|
|
use Psi\Component\ObjectAgent\Query\Expression; |
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
18
|
|
|
use Symfony\Component\Form\FormInterface; |
19
|
|
|
use Symfony\Component\Form\AbstractType; |
20
|
|
|
use Symfony\Component\Validator\Constraints\NotNull; |
21
|
|
|
use Psi\Component\Grid\FilterDataInterface; |
22
|
|
|
use Symfony\Component\Form\Extension\Core\Type\NumberType; |
23
|
|
|
|
24
|
|
|
class NumberFilter implements FilterInterface |
25
|
|
|
{ |
26
|
|
|
private static $validComparators = [ |
27
|
|
|
Comparison::EQUALS, |
28
|
|
|
Comparison::NOT_EQUALS, |
29
|
|
|
Comparison::GREATER_THAN, |
30
|
|
|
Comparison::GREATER_THAN_EQUAL, |
31
|
|
|
Comparison::LESS_THAN_EQUAL, |
32
|
|
|
Comparison::LESS_THAN, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function buildForm(FormBuilderInterface $builder, array $options) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$builder->add('comparator', ChoiceType::class, [ |
41
|
|
|
'choices' => $this->getChoices( |
42
|
|
|
$options['capabilities']->getSupportedComparators(), |
43
|
|
|
$options['comparators'] |
44
|
|
|
) |
45
|
|
|
]); |
46
|
|
|
$builder->add('value', NumberType::class); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function getExpression(string $fieldName, FilterDataInterface $data): Expression |
53
|
|
|
{ |
54
|
|
|
return Query::comparison( |
55
|
|
|
$data->getComparator(), |
|
|
|
|
56
|
|
|
$fieldName, |
57
|
|
|
$data->getValue() |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function configureOptions(OptionsResolver $options) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$options->setDefault('comparators', self::$validComparators); |
67
|
|
|
$options->setDefault('data_class', StringFilterData::class); |
68
|
|
|
$options->setDefault('empty_data', function (FormInterface $form) { |
69
|
|
|
return new StringFilterData( |
70
|
|
|
$form->get('comparator')->getData(), |
71
|
|
|
$form->get('value')->getData() |
72
|
|
|
); |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function getChoices(array $supportedComparators, array $enabledComparators) |
77
|
|
|
{ |
78
|
|
|
$supported = array_filter(self::$validComparators, function ($comparator) use ($supportedComparators) { |
79
|
|
|
return in_array($comparator, $supportedComparators); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
$supported = array_filter($supported, function ($comparator) use ($enabledComparators) { |
83
|
|
|
return in_array($comparator, $enabledComparators); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
return array_combine($supported, $supported); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.