1 | <?php |
||
16 | class StringFilter extends AbstractComparatorFilter |
||
17 | { |
||
18 | const TYPE_EQUAL = 'equal'; |
||
19 | const TYPE_EMPTY = 'empty'; |
||
20 | const TYPE_NOT_EMPTY = 'not_empty'; |
||
21 | const TYPE_CONTAINS = 'contains'; |
||
22 | const TYPE_NOT_CONTAINS = 'not_contains'; |
||
23 | const TYPE_STARTS_WITH = 'starts_with'; |
||
24 | const TYPE_ENDS_WITH = 'ends_with'; |
||
25 | const TYPE_IN = 'in'; |
||
26 | const TYPE_NOT_IN = 'not_in'; |
||
27 | |||
28 | private static $comparatorMap = [ |
||
29 | self::TYPE_EQUAL => Comparison::EQUALS, |
||
30 | self::TYPE_EMPTY => Comparison::NULL, |
||
31 | self::TYPE_NOT_EMPTY => Comparison::NOT_NULL, |
||
32 | self::TYPE_CONTAINS => Comparison::CONTAINS, |
||
33 | self::TYPE_NOT_CONTAINS => Comparison::NOT_CONTAINS, |
||
34 | self::TYPE_STARTS_WITH => Comparison::CONTAINS, |
||
35 | self::TYPE_ENDS_WITH => Comparison::CONTAINS, |
||
36 | self::TYPE_IN => Comparison::IN, |
||
37 | self::TYPE_NOT_IN => Comparison::NOT_IN, |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public function buildForm(FormBuilderInterface $builder, array $options) |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function getExpression(string $fieldName, FilterDataInterface $data): Expression |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function configureOptions(OptionsResolver $options) |
||
80 | |||
81 | protected function getComparatorMap(): array |
||
85 | |||
86 | private function getValue($comparator, $value) |
||
111 | } |
||
112 |
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: