1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Component\Grid\Filter; |
6
|
|
|
|
7
|
|
|
use Psi\Component\Grid\FilterDataInterface; |
8
|
|
|
use Psi\Component\Grid\FilterInterface; |
9
|
|
|
use Psi\Component\ObjectAgent\Capabilities; |
10
|
|
|
use Psi\Component\ObjectAgent\Query\Comparison; |
11
|
|
|
use Psi\Component\ObjectAgent\Query\Expression; |
12
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\NumberType; |
16
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
17
|
|
|
use Symfony\Component\Form\FormInterface; |
18
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
19
|
|
|
|
20
|
|
|
class NumberFilter extends AbstractComparatorFilter |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
26
|
|
|
{ |
27
|
|
|
$this->addComparatorChoice($builder, $options); |
28
|
|
|
|
29
|
|
|
$builder->add('value', NumberType::class, [ |
30
|
|
|
'required' => false, |
31
|
|
|
]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function getExpression(string $fieldName, FilterDataInterface $data): Expression |
38
|
|
|
{ |
39
|
|
|
$comparator = $data->getComparator() ?: Comparison::EQUALS; |
|
|
|
|
40
|
|
|
|
41
|
|
|
return Query::comparison( |
42
|
|
|
$comparator, |
43
|
|
|
$fieldName, |
44
|
|
|
$data->getValue() |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function configureOptions(OptionsResolver $options) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$options->setDefault('comparators', $this->getComparatorMap()); |
54
|
|
|
$options->setDefault('data_class', NumberFilterData::class); |
55
|
|
|
$options->setDefault('empty_data', function (FormInterface $form) { |
56
|
|
|
return new NumberFilterData( |
57
|
|
|
$form->get('comparator')->getData(), |
58
|
|
|
$form->get('value')->getData() |
59
|
|
|
); |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
protected function getComparatorMap(): array |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$supported = [ |
66
|
|
|
Comparison::EQUALS, |
67
|
|
|
Comparison::NOT_EQUALS, |
68
|
|
|
Comparison::GREATER_THAN, |
69
|
|
|
Comparison::GREATER_THAN_EQUAL, |
70
|
|
|
Comparison::LESS_THAN_EQUAL, |
71
|
|
|
Comparison::LESS_THAN, |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
return array_combine($supported, $supported); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: