1 | <?php |
||
15 | class StringFilter extends AbstractComparatorFilter |
||
16 | { |
||
17 | const TYPE_EQUAL = 'equal'; |
||
18 | const TYPE_EMPTY = 'empty'; |
||
19 | const TYPE_NOT_EMPTY = 'not_empty'; |
||
20 | const TYPE_CONTAINS = 'contains'; |
||
21 | const TYPE_NOT_CONTAINS = 'not_contains'; |
||
22 | const TYPE_STARTS_WITH = 'starts_with'; |
||
23 | const TYPE_ENDS_WITH = 'ends_with'; |
||
24 | const TYPE_IN = 'in'; |
||
25 | const TYPE_NOT_IN = 'not_in'; |
||
26 | |||
27 | private static $comparatorMap = [ |
||
28 | self::TYPE_EQUAL => Comparison::EQUALS, |
||
29 | self::TYPE_EMPTY => Comparison::NULL, |
||
30 | self::TYPE_NOT_EMPTY => Comparison::NOT_NULL, |
||
31 | self::TYPE_CONTAINS => Comparison::CONTAINS, |
||
32 | self::TYPE_NOT_CONTAINS => Comparison::NOT_CONTAINS, |
||
33 | self::TYPE_STARTS_WITH => Comparison::CONTAINS, |
||
34 | self::TYPE_ENDS_WITH => Comparison::CONTAINS, |
||
35 | self::TYPE_IN => Comparison::IN, |
||
36 | self::TYPE_NOT_IN => Comparison::NOT_IN, |
||
37 | ]; |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function buildForm(FormBuilderInterface $builder, array $options) |
||
43 | { |
||
44 | $this->addComparatorChoice($builder, $options); |
||
45 | |||
46 | $builder->add('value', TextType::class, [ |
||
47 | 'required' => false, |
||
48 | ]); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function getExpression(string $fieldName, array $data): Expression |
||
55 | { |
||
56 | $comparator = $data['comparator'] ?: self::TYPE_EQUAL; |
||
57 | |||
58 | return Query::comparison( |
||
59 | self::$comparatorMap[$comparator], |
||
60 | $fieldName, |
||
61 | $this->getValue($comparator, $data['value']) |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function configureOptions(OptionsResolver $options) |
||
69 | { |
||
70 | $options->setDefault('comparators', array_keys(self::$comparatorMap)); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function isApplicable(array $filterData): bool |
||
77 | { |
||
78 | return isset($filterData['value']); |
||
79 | } |
||
80 | |||
81 | protected function getComparatorMap(): array |
||
85 | |||
86 | private function getValue($comparator, $value) |
||
111 | } |
||
112 |