-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDateFilter.php
272 lines (250 loc) · 10.3 KB
/
DateFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Doctrine\Orm\Filter;
use ApiPlatform\Doctrine\Common\Filter\DateFilterInterface;
use ApiPlatform\Doctrine\Common\Filter\DateFilterTrait;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\Operation;
use Doctrine\DBAL\Types\Type as DBALType;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
/**
* The date filter allows to filter a collection by date intervals.
*
* Syntax: `?property[<after|before|strictly_after|strictly_before>]=value`.
*
* The value can take any date format supported by the [`\DateTime` constructor](https://www.php.net/manual/en/datetime.construct.php).
*
* The `after` and `before` filters will filter including the value whereas `strictly_after` and `strictly_before` will filter excluding the value.
*
* The date filter is able to deal with date properties having `null` values. Four behaviors are available at the property level of the filter:
* - Use the default behavior of the DBMS: use `null` strategy
* - Exclude items: use `ApiPlatform\Doctrine\Orm\Filter\DateFilter::EXCLUDE_NULL` (`exclude_null`) strategy
* - Consider items as oldest: use `ApiPlatform\Doctrine\Orm\Filter\DateFilter::INCLUDE_NULL_BEFORE` (`include_null_before`) strategy
* - Consider items as youngest: use `ApiPlatform\Doctrine\Orm\Filter\DateFilter::INCLUDE_NULL_AFTER` (`include_null_after`) strategy
* - Always include items: use `ApiPlatform\Doctrine\Orm\Filter\DateFilter::INCLUDE_NULL_BEFORE_AND_AFTER` (`include_null_before_and_after`) strategy
*
* <div data-code-selector>
*
* ```php
* <?php
* // api/src/Entity/Book.php
* use ApiPlatform\Metadata\ApiFilter;
* use ApiPlatform\Metadata\ApiResource;
* use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
*
* #[ApiResource]
* #[ApiFilter(DateFilter::class, properties: ['createdAt'])]
* class Book
* {
* // ...
* }
* ```
*
* ```yaml
* # config/services.yaml
* services:
* book.date_filter:
* parent: 'api_platform.doctrine.orm.date_filter'
* arguments: [ { createdAt: ~ } ]
* tags: [ 'api_platform.filter' ]
* # The following are mandatory only if a _defaults section is defined with inverted values.
* # You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
* autowire: false
* autoconfigure: false
* public: false
*
* # api/config/api_platform/resources.yaml
* resources:
* App\Entity\Book:
* - operations:
* ApiPlatform\Metadata\GetCollection:
* filters: ['book.date_filter']
* ```
*
* ```xml
* <?xml version="1.0" encoding="UTF-8" ?>
* <!-- api/config/services.xml -->
* <?xml version="1.0" encoding="UTF-8" ?>
* <container
* xmlns="http://symfony.com/schema/dic/services"
* xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
* xsi:schemaLocation="http://symfony.com/schema/dic/services
* https://symfony.com/schema/dic/services/services-1.0.xsd">
* <services>
* <service id="book.date_filter" parent="api_platform.doctrine.orm.date_filter">
* <argument type="collection">
* <argument key="createdAt"/>
* </argument>
* <tag name="api_platform.filter"/>
* </service>
* </services>
* </container>
* <!-- api/config/api_platform/resources.xml -->
* <resources
* xmlns="https://api-platform.com/schema/metadata/resources-3.0"
* xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
* xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
* https://api-platform.com/schema/metadata/resources-3.0.xsd">
* <resource class="App\Entity\Book">
* <operations>
* <operation class="ApiPlatform\Metadata\GetCollection">
* <filters>
* <filter>book.date_filter</filter>
* </filters>
* </operation>
* </operations>
* </resource>
* </resources>
* ```
*
* </div>
*
* Given that the collection endpoint is `/books`, you can filter books by date with the following query: `/books?createdAt[after]=2018-03-19`.
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Théo FIDRY <theo.fidry@gmail.com>
*/
final class DateFilter extends AbstractFilter implements DateFilterInterface
{
use DateFilterTrait;
public const DOCTRINE_DATE_TYPES = [
Types::DATE_MUTABLE => true,
Types::DATETIME_MUTABLE => true,
Types::DATETIMETZ_MUTABLE => true,
Types::TIME_MUTABLE => true,
Types::DATE_IMMUTABLE => true,
Types::DATETIME_IMMUTABLE => true,
Types::DATETIMETZ_IMMUTABLE => true,
Types::TIME_IMMUTABLE => true,
];
/**
* {@inheritdoc}
*/
protected function filterProperty(string $property, $values, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, Operation $operation = null, array $context = []): void
{
// Expect $values to be an array having the period as keys and the date value as values
if (
!\is_array($values)
|| !$this->isPropertyEnabled($property, $resourceClass)
|| !$this->isPropertyMapped($property, $resourceClass)
|| !$this->isDateField($property, $resourceClass)
) {
return;
}
$alias = $queryBuilder->getRootAliases()[0];
$field = $property;
if ($this->isPropertyNested($property, $resourceClass)) {
[$alias, $field] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass, Join::INNER_JOIN);
}
$nullManagement = $this->properties[$property] ?? null;
$type = (string) $this->getDoctrineFieldType($property, $resourceClass);
if (self::EXCLUDE_NULL === $nullManagement) {
$queryBuilder->andWhere($queryBuilder->expr()->isNotNull(sprintf('%s.%s', $alias, $field)));
}
if (isset($values[self::PARAMETER_BEFORE])) {
$this->addWhere(
$queryBuilder,
$queryNameGenerator,
$alias,
$field,
self::PARAMETER_BEFORE,
$values[self::PARAMETER_BEFORE],
$nullManagement,
$type
);
}
if (isset($values[self::PARAMETER_STRICTLY_BEFORE])) {
$this->addWhere(
$queryBuilder,
$queryNameGenerator,
$alias,
$field,
self::PARAMETER_STRICTLY_BEFORE,
$values[self::PARAMETER_STRICTLY_BEFORE],
$nullManagement,
$type
);
}
if (isset($values[self::PARAMETER_AFTER])) {
$this->addWhere(
$queryBuilder,
$queryNameGenerator,
$alias,
$field,
self::PARAMETER_AFTER,
$values[self::PARAMETER_AFTER],
$nullManagement,
$type
);
}
if (isset($values[self::PARAMETER_STRICTLY_AFTER])) {
$this->addWhere(
$queryBuilder,
$queryNameGenerator,
$alias,
$field,
self::PARAMETER_STRICTLY_AFTER,
$values[self::PARAMETER_STRICTLY_AFTER],
$nullManagement,
$type
);
}
}
/**
* Adds the where clause according to the chosen null management.
*/
protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, string $operator, mixed $value, string $nullManagement = null, DBALType|string $type = null): void
{
$type = (string) $type;
$value = $this->normalizeValue($value, $operator);
if (null === $value) {
return;
}
try {
$value = !str_contains($type, '_immutable') ? new \DateTime($value) : new \DateTimeImmutable($value);
} catch (\Exception) {
// Silently ignore this filter if it can not be transformed to a \DateTime
$this->logger->notice('Invalid filter ignored', [
'exception' => new InvalidArgumentException(sprintf('The field "%s" has a wrong date format. Use one accepted by the \DateTime constructor', $field)),
]);
return;
}
$valueParameter = $queryNameGenerator->generateParameterName($field);
$operatorValue = [
self::PARAMETER_BEFORE => '<=',
self::PARAMETER_STRICTLY_BEFORE => '<',
self::PARAMETER_AFTER => '>=',
self::PARAMETER_STRICTLY_AFTER => '>',
];
$baseWhere = sprintf('%s.%s %s :%s', $alias, $field, $operatorValue[$operator], $valueParameter);
if (null === $nullManagement || self::EXCLUDE_NULL === $nullManagement) {
$queryBuilder->andWhere($baseWhere);
} elseif (
(self::INCLUDE_NULL_BEFORE === $nullManagement && \in_array($operator, [self::PARAMETER_BEFORE, self::PARAMETER_STRICTLY_BEFORE], true))
|| (self::INCLUDE_NULL_AFTER === $nullManagement && \in_array($operator, [self::PARAMETER_AFTER, self::PARAMETER_STRICTLY_AFTER], true))
|| (self::INCLUDE_NULL_BEFORE_AND_AFTER === $nullManagement && \in_array($operator, [self::PARAMETER_AFTER, self::PARAMETER_STRICTLY_AFTER, self::PARAMETER_BEFORE, self::PARAMETER_STRICTLY_BEFORE], true))
) {
$queryBuilder->andWhere($queryBuilder->expr()->orX(
$baseWhere,
$queryBuilder->expr()->isNull(sprintf('%s.%s', $alias, $field))
));
} else {
$queryBuilder->andWhere($queryBuilder->expr()->andX(
$baseWhere,
$queryBuilder->expr()->isNotNull(sprintf('%s.%s', $alias, $field))
));
}
$queryBuilder->setParameter($valueParameter, $value, $type);
}
}