-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFilterLogic.php
More file actions
355 lines (318 loc) · 15.2 KB
/
Copy pathFilterLogic.php
File metadata and controls
355 lines (318 loc) · 15.2 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
namespace Metaclass\FilterBundle\Filter;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Api\FilterCollection;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ContextAwareFilterInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Query\Expr;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use ReflectionAttribute;
use ReflectionClass;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Doctrine\ORM\Query\Expr\Join;
/**
* Combines existing API Platform ORM Filters with AND and OR.
* For usage and limitations see https://gist.github.com/metaclass-nl/790a5c8e9064f031db7d3379cc47c794
* WARNING: $innerJoinsLeft=true changes the behavior of ExistsFilter =false,
* and though it makes it more like one would expect given the semantics of its name,
* it does break backward compatibility.
*
* Copyright (c) MetaClass, Groningen, 2021. MIT License
*/
class FilterLogic extends AbstractContextAwareFilter
{
/** @var ResourceMetadataFactoryInterface */
private $resourceMetadataFactory;
/** @var ContainerInterface|FilterCollection */
private $filterLocator;
/** @var string Filter classes must match this to be applied with logic */
private $classExp;
/** @var ContextAwareFilterInterface[] */
private $filters;
/** @var bool Wheather to replace all inner joins by left joins */
private $innerJoinsLeft;
/**
* @param ResourceMetadataFactoryInterface $resourceMetadataFactory
* @param ContainerInterface|FilterCollection $filterLocator
* @param $regExp string Filter classes must match this to be applied with logic
* @param $innerJoinsLeft bool Wheather to replace all inner joins by left joins.
* This makes the standard Api Platform filters combine properly with OR,
* but also changes the behavior of ExistsFilter =false.
* {@inheritdoc}
*/
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, $filterLocator, ManagerRegistry $managerRegistry, LoggerInterface $logger = null, array $properties = null, NameConverterInterface $nameConverter = null, string $classExp='//', $innerJoinsLeft=false)
{
parent::__construct($managerRegistry, null, $logger, $properties, $nameConverter);
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->filterLocator = $filterLocator;
$this->classExp = $classExp;
$this->innerJoinsLeft = $innerJoinsLeft;
}
/** {@inheritdoc } */
public function getDescription(string $resourceClass): array
{
$description = [];
$reflectionClass = new \ReflectionClass($resourceClass);
// Does the resource have the filter? #[ApiFilter(FilterLogic::class)] ?
if (false === $this->isFilterLogicFilter($reflectionClass)) {
return $description;
}
// Does the resource have the filter with properties? #[ApiFilter(SearchFilter::class)]
$properties = $this->getPropertiesFromSearchFilter($reflectionClass);
if (null === $properties) {
return $description;
}
foreach ($properties as $key => $value) {
$description['and[or]['.$key.']'] = [
'description' => 'andOr filter',
'type' => 'string',
'required' => false,
];
// todo add all filters here and or not ...
}
return $description;
}
/**
* {@inheritdoc}
* @throws ResourceClassNotFoundException
* @throws \LogicException if assumption proves wrong
*/
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = [])
{
if (!isset($context['filters']) || !\is_array($context['filters'])) {
throw new \InvalidArgumentException('::apply without $context[filters] not supported');
}
$this->filters = $this->getFilters($resourceClass, $operationName);
if (isset($context['filters']['and']) ) {
$expressions = $this->filterProperty('and', $context['filters']['and'], $queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);
foreach($expressions as $exp) {
$queryBuilder->andWhere($exp);
};
}
if (isset($context['filters']['not']) ) {
// NOT expressions are combined by parent logic, here defaulted to AND
$expressions = $this->filterProperty('not', $context['filters']['not'], $queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);
foreach($expressions as $exp) {
$queryBuilder->andWhere(new Expr\Func('NOT', [$exp]));
};
}
#Issue 10: for security allways AND with existing criteria
if (isset($context['filters']['or'])) {
$expressions = $this->filterProperty('or', $context['filters']['or'], $queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);
$queryBuilder->andWhere(new Expr\Orx($expressions));
}
if ($this->innerJoinsLeft) {
$this->replaceInnerJoinsByLeftJoins($queryBuilder);
}
}
/**
* @return array of Doctrine\ORM\Query\Expr\* and/or string (DQL),
* each of which must be self-contained in the sense that the intended
* logic is not compromised if it is combined with the others and other
* self-contained expressions by
* Doctrine\ORM\Query\Expr\Andx or Doctrine\ORM\Query\Expr\Orx
*
* Adds parameters and joins to $queryBuilder.
* Caller of this function is responsable for adding the generated
* expressions to $queryBuilder so that the parameters in the query will
* correspond 1 to 1 with the parameters that where added by this function.
* In practice this comes down to adding EACH expression to $queryBuilder
* once and only once.
* @throws \LogicException if assumption proves wrong
*/
public function generateExpressions(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = [])
{
if (!isset($context['filters']) || !\is_array($context['filters'])) {
throw new \InvalidArgumentException('::generateExpressions without $context[filters] not supported');
}
$this->filters = $this->getFilters($resourceClass, $operationName);
return $this->doGenerate($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);
}
/**
* @throws \LogicException if assumption proves wrong
*/
protected function doGenerate($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context)
{
$oldWhere = $queryBuilder->getDQLPart('where');
// replace by marker expression
$marker = new Expr\Func('NOT', []);
$queryBuilder->add('where', $marker);
$assoc = [];
$logic = [];
foreach ($context['filters'] as $key => $value) {
if (ctype_digit((string) $key)) {
// allows the same filter to be applied several times, usually with different arguments
$subcontext = $context; //copies
$subcontext['filters'] = $value;
$this->applyFilters($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $subcontext);
// apply logic seperately
if (isset($value['and'])) {
$logic[]['and'] = $value['and'];
}if (isset($value['or'])) {
$logic[]['or'] = $value['or'];
}if (isset($value['not'])) {
$logic[]['not'] = $value['not'];
}
} elseif (in_array($key, ['and', 'or', 'not'])) {
$logic[][$key] = $value;
} else {
$assoc[$key] = $value;
}
}
// Process $assoc
$subcontext = $context; //copies
$subcontext['filters'] = $assoc;
$this->applyFilters($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $subcontext);
$newWhere = $queryBuilder->getDQLPart('where');
$queryBuilder->add('where', $oldWhere); //restores old where
// force $operator logic upon $newWhere
$expressions = $this->getAppliedExpressions($newWhere, $marker);
// Process logic
foreach ($logic as $eachLogic) {
$subExpressions = $this->filterProperty(key($eachLogic), current($eachLogic), $queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);
if (key($eachLogic) == 'not') {
// NOT expressions are combined by parent logic
foreach ($subExpressions as $subExp) {
$expressions[] = new Expr\Func('NOT', [$subExp]);
}
} else {
$expressions[] = key($eachLogic) == 'or'
? new Expr\Orx($subExpressions)
: new Expr\Andx($subExpressions);
}
}
return $expressions; // may be empty
}
/**
* @throws \LogicException if assumption proves wrong
*/
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, $context=[])
{
$subcontext = $context; //copies
$subcontext['filters'] = $value;
return $this->doGenerate($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $subcontext);
}
/** Calls ::apply on each filter in $filters */
private function applyFilters($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context)
{
foreach ($this->filters as $filter) {
$filter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);
}
}
/**
* ASSUMPTION: filters do not use QueryBuilder::where or QueryBuilder::add
* and create semantically complete expressions in the sense that expressions
* added to the QueryBundle through ::andWhere or ::orWhere do not depend
* on one another so that the intended logic is not compromised if they are
* recombined with the others by either Doctrine\ORM\Query\Expr\Andx
* or Doctrine\ORM\Query\Expr\Orx.
*
* Get expressions from $where
* andWhere and orWhere allways add their args at the end of existing or
* new logical expressions, so we started with a marker expression
* to become the deepest first part. The marker should not be returned
* @param Expr\Andx | Expr\Orx $where Result from applying filters
* @param Expr\Func $marker Marks the end of logic resulting from applying filters
* @return array of ORM Expression
* @throws \LogicException if assumption proves wrong
*/
private function getAppliedExpressions($where, $marker)
{
if ($where === $marker) {
return [];
}
if (!$where instanceof Expr\Andx && !$where instanceof Expr\Orx) {
// A filter used QueryBuilder::where or QueryBuilder::add or otherwise
throw new \LogicException("Assumpion failure, unexpected Expression: ". $where);
}
$parts = $where->getParts();
if (empty($parts)) {
// A filter used QueryBuilder::where or QueryBuilder::add or otherwise
throw new \LogicException("Assumpion failure, marker not found");
}
$firstPart = array_shift($parts);
$parts = array_merge($parts, $this->getAppliedExpressions($firstPart, $marker));
return $parts;
}
/**
* @param string $resourceClass
* @param string $operationName
* @return ContextAwareFilterInterface[] From resource except $this and OrderFilters
* @throws ResourceClassNotFoundException
*/
protected function getFilters($resourceClass, $operationName)
{
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$resourceFilters = $resourceMetadata->getCollectionOperationAttribute($operationName, 'filters', [], true);
$result = [];
foreach ($resourceFilters as $filterId) {
$filter = $this->filterLocator->has($filterId)
? $this->filterLocator->get($filterId)
: null;
if ($filter instanceof ContextAwareFilterInterface
&& !($filter instanceof OrderFilter)
&& $filter !== $this
&& preg_match($this->classExp, get_class($filter))
) {
$result[$filterId] = $filter;
}
}
return $result;
}
/**
* The filters that come standard with Api Platform create inner joins,
* but for nullable and to many references we need Left Joins for OR
* to also produce results that are not related.
* WARNING: This changes the behavior of ExistsFilter =false, consider
* using ExistFilter included in this bundle instead.
* @param QueryBuilder $queryBuilder
*/
protected function replaceInnerJoinsByLeftJoins(QueryBuilder $queryBuilder) {
$joinPart = $queryBuilder->getDQLPart('join');
$result = [];
foreach ($joinPart as $rootAlias => $joins) {
foreach ($joins as $i => $joinExp) {
if (Join::INNER_JOIN === $joinExp->getJoinType()) {
$result[$rootAlias][$i] = new Join(
Join::LEFT_JOIN,
$joinExp->getJoin(),
$joinExp->getAlias(),
$joinExp->getConditionType(),
$joinExp->getCondition(),
$joinExp->getIndexBy()
);
} else {
$result[$rootAlias][$i] = $joinExp;
}
}
}
$queryBuilder->add('join', $result);
}
private function getPropertiesFromSearchFilter(\ReflectionClass $class): ?array
{
$apiFilterAttributes = $class->getAttributes('ApiPlatform\Core\Annotation\ApiFilter', ReflectionAttribute::IS_INSTANCEOF);
foreach ($apiFilterAttributes as $filterAttribute) {
if ('ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter' === $filterAttribute->newInstance()->filterClass) {
return $filterAttribute->newInstance()->properties;
}
}
return null;
}
private function isFilterLogicFilter(\ReflectionClass $class): bool
{
$apiFilterAttributes = $class->getAttributes('ApiPlatform\Core\Annotation\ApiFilter', ReflectionAttribute::IS_INSTANCEOF);
foreach ($apiFilterAttributes as $filterAttribute) {
if ('Metaclass\FilterBundle\Filter\FilterLogic' === $filterAttribute->newInstance()->filterClass) {
return true;
}
}
return false;
}
}