1 | <?php |
||
23 | abstract class AbstractDateFilter extends Filter |
||
24 | { |
||
25 | /** |
||
26 | * Flag indicating that filter will have range. |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | protected $range = false; |
||
31 | |||
32 | /** |
||
33 | * Flag indicating that filter will filter by datetime instead by date. |
||
34 | * |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $time = false; |
||
38 | |||
39 | public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data) |
||
40 | { |
||
41 | //check data sanity |
||
42 | if (true !== \is_array($data)) { |
||
43 | return; |
||
44 | } |
||
45 | |||
46 | //default type for simple filter |
||
47 | $data['type'] = !isset($data['type']) || !is_numeric($data['type']) ? DateOperatorType::TYPE_EQUAL : $data['type']; |
||
48 | |||
49 | // Some types do not require a value to be set (NULL, NOT NULL). |
||
50 | if (!isset($data['value']) && $this->typeDoesRequireValue($data['type'])) { |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | switch ($data['type']) { |
||
55 | case DateOperatorType::TYPE_EQUAL: |
||
56 | $this->active = true; |
||
57 | |||
58 | $this->applyTypeIsEqual($queryBuilder, $field, $data); |
||
59 | |||
60 | return; |
||
61 | |||
62 | case DateOperatorType::TYPE_GREATER_THAN: |
||
63 | $this->active = true; |
||
64 | |||
65 | $this->applyTypeIsGreaterThan($queryBuilder, $field, $data); |
||
66 | |||
67 | return; |
||
68 | |||
69 | case DateOperatorType::TYPE_LESS_EQUAL: |
||
70 | $this->active = true; |
||
71 | |||
72 | $this->applyTypeIsLessEqual($queryBuilder, $field, $data); |
||
73 | |||
74 | return; |
||
75 | |||
76 | case DateOperatorType::TYPE_NULL: |
||
77 | case DateOperatorType::TYPE_NOT_NULL: |
||
78 | $this->active = true; |
||
79 | |||
80 | $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, null); |
||
81 | |||
82 | return; |
||
83 | |||
84 | case DateOperatorType::TYPE_GREATER_EQUAL: |
||
85 | case DateOperatorType::TYPE_LESS_THAN: |
||
86 | $this->active = true; |
||
87 | |||
88 | $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, $data['value']); |
||
89 | |||
90 | return; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | public function getDefaultOptions() |
||
95 | { |
||
96 | return ['input_type' => 'datetime']; |
||
97 | } |
||
98 | |||
99 | public function getRenderSettings() |
||
117 | |||
118 | abstract protected function applyTypeIsLessEqual(ProxyQueryInterface $queryBuilder, string $field, array $data); |
||
119 | |||
120 | abstract protected function applyTypeIsGreaterThan(ProxyQueryInterface $queryBuilder, string $field, array $data); |
||
121 | |||
122 | abstract protected function applyTypeIsEqual(ProxyQueryInterface $queryBuilder, string $field, array $data); |
||
123 | |||
124 | /** |
||
125 | * @param string $operation |
||
126 | * @param string $field |
||
127 | * @param \DateTime $datetime |
||
128 | */ |
||
129 | protected function applyType(ProxyQueryInterface $queryBuilder, $operation, $field, ?\DateTime $datetime = null): void |
||
134 | |||
135 | /** |
||
136 | * NEXT_MAJOR: Remove this method. |
||
137 | * |
||
138 | * Returns if the filter type requires a value to be set. |
||
139 | * |
||
140 | * @param int $type |
||
141 | * |
||
142 | * @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | protected function typeRequiresValue($type) |
||
158 | |||
159 | /** |
||
160 | * Resolves DataType:: constants to MongoDb operators. |
||
161 | * |
||
162 | * @param int $type |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | protected function getOperator($type) |
||
180 | |||
181 | private function typeDoesRequireValue(int $type): bool |
||
188 | } |
||
189 |
If you suppress an error, we recommend checking for the error condition explicitly: