-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomSearchFilter.php
475 lines (403 loc) · 17.1 KB
/
CustomSearchFilter.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/*
* Adaption of the api-platform Search filter to allow for setting the
* strategy in the request in the form ?fieldname[strategy]=value
*
* 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 App\Filter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryBuilderHelper;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\QueryBuilder;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
/**
* Filter the collection by given properties.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class CustomSearchFilter extends AbstractContextAwareFilter
{
/**
* @var string Exact matching
*/
const STRATEGY_EXACT = 'exact';
/**
* @var string The value must be contained in the field
*/
const STRATEGY_PARTIAL = 'partial';
/**
* @var string Finds fields that are starting with the value
*/
const STRATEGY_START = 'start';
/**
* @var string Finds fields that are ending with the value
*/
const STRATEGY_END = 'end';
/**
* @var string Finds fields that are starting with the word
*/
const STRATEGY_WORD_START = 'word_start';
protected $iriConverter;
protected $propertyAccessor;
/**
* @param RequestStack|null $requestStack No prefix to prevent autowiring of this deprecated property
*/
public function __construct(ManagerRegistry $managerRegistry, IriConverterInterface $iriConverter, $requestStack = null, PropertyAccessorInterface $propertyAccessor = null, LoggerInterface $logger = null, array $properties = null)
{
parent::__construct($managerRegistry, $requestStack, $logger, $properties);
$this->iriConverter = $iriConverter;
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
}
/**
* {@inheritdoc}
*/
public function getDescription(string $resourceClass): array
{
$description = [];
$properties = $this->properties;
if (null === $properties) {
$properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null);
}
foreach ($properties as $property => $strategy) {
if (!$this->isPropertyMapped($property, $resourceClass, true)) {
continue;
}
if ($this->isPropertyNested($property, $resourceClass)) {
$propertyParts = $this->splitPropertyParts($property, $resourceClass);
$field = $propertyParts['field'];
$metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
} else {
$field = $property;
$metadata = $this->getClassMetadata($resourceClass);
}
if ($metadata->hasField($field)) {
$typeOfField = $this->getType((string) $metadata->getTypeOfField($field));
$strategy = $this->properties[$property] ?? self::STRATEGY_EXACT;
$filterParameterNames = [$property];
if (self::STRATEGY_EXACT === $strategy) {
$filterParameterNames[] = $property.'[]';
}
foreach ($filterParameterNames as $filterParameterName) {
$description[$filterParameterName] = [
'property' => $property,
'type' => $typeOfField,
'required' => false,
'strategy' => $strategy,
'is_collection' => '[]' === substr($filterParameterName, -2),
];
}
} elseif ($metadata->hasAssociation($field)) {
$filterParameterNames = [
$property,
$property.'[]',
];
foreach ($filterParameterNames as $filterParameterName) {
$description[$filterParameterName] = [
'property' => $property,
'type' => 'string',
'required' => false,
'strategy' => self::STRATEGY_EXACT,
'is_collection' => '[]' === substr($filterParameterName, -2),
];
}
}
}
// $description['building[start]'] = [
// 'property' => 'building',
// 'type' => 'string',
// 'required' => 'false',
// 'strategy' => self::STRATEGY_START,
// 'is_collection' => false
// ];
return $description;
}
/**
* Converts a Doctrine type in PHP type.
*/
private function getType(string $doctrineType): string
{
switch ($doctrineType) {
case Type::TARRAY:
return 'array';
case Type::BIGINT:
case Type::INTEGER:
case Type::SMALLINT:
return 'int';
case Type::BOOLEAN:
return 'bool';
case Type::DATE:
case Type::TIME:
case Type::DATETIME:
case Type::DATETIMETZ:
return \DateTimeInterface::class;
case Type::FLOAT:
return 'float';
}
if (\defined(Type::class.'::DATE_IMMUTABLE')) {
switch ($doctrineType) {
case Type::DATE_IMMUTABLE:
case Type::TIME_IMMUTABLE:
case Type::DATETIME_IMMUTABLE:
case Type::DATETIMETZ_IMMUTABLE:
return \DateTimeInterface::class;
}
}
return 'string';
}
/**
* {@inheritdoc}
*/
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
if (null === $value ||
!$this->isPropertyEnabled($property, $resourceClass) ||
!$this->isPropertyMapped($property, $resourceClass, true)
) {
return;
}
$alias = $queryBuilder->getRootAliases()[0];
$field = $property;
$this->logger->info('the property in filterProperty: '. $property);
$this->logger->info('the provided value: '. print_r($value, true));
if ($this->isPropertyNested($property, $resourceClass)) {
list($alias, $field, $associations) = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass);
$metadata = $this->getNestedMetadata($resourceClass, $associations);
} else {
$metadata = $this->getClassMetadata($resourceClass);
}
//inspect the $value array for a provided search strategy.
$value = $this->testForProvidedSearchStrategy((array) $value, (string) $property);
$this->logger->info('the updated value: '. print_r($value, true));
$this->logger->info('the search strategy: '. $this->properties[$property]);
$this->logger->info('the properties array: '. print_r($this->properties, true));
$values = $this->normalizeValues((array) $value);
if (empty($values)) {
$this->logger->notice('Invalid filter ignored: Empty Values', [
'exception' => new InvalidArgumentException(sprintf('At least one value is required, multiple values should be in "%1$s[]=firstvalue&%1$s[]=secondvalue" format', $property)),
]);
return;
}
$caseSensitive = true;
$this->logger->info('does metadata have '. $field .': ' . ($metadata->hasField($field) ? 'yes' : 'no'));
if ($metadata->hasField($field)) {
if ('id' === $field) {
$values = array_map([$this, 'getIdFromValue'], $values);
}
if (!$this->hasValidValues($values, $this->getDoctrineFieldType($property, $resourceClass))) {
$this->logger->notice('Invalid filter ignored: Invalid value for the filter', [
'exception' => new InvalidArgumentException(sprintf('Values for field "%s" are not valid according to the doctrine type.', $field)),
]);
return;
}
$strategy = $this->properties[$property] ?? self::STRATEGY_EXACT;
$this->logger->info('the current stategy is: '. $strategy);
// prefixing the strategy with i makes it case insensitive
if (0 === strpos($strategy, 'i')) {
$strategy = substr($strategy, 1);
$caseSensitive = false;
}
if (1 === \count($values)) {
$this->addWhereByStrategy($strategy, $queryBuilder, $queryNameGenerator, $alias, $field, $values[0], $caseSensitive);
return;
}
if (self::STRATEGY_EXACT !== $strategy) {
$this->logger->notice('Invalid filter ignored: Invalid Strategy', [
'exception' => new InvalidArgumentException(sprintf('"%s" strategy selected for "%s" property, but only "%s" strategy supports multiple values', $strategy, $property, self::STRATEGY_EXACT)),
]);
return;
}
$wrapCase = $this->createWrapCase($caseSensitive);
$valueParameter = $queryNameGenerator->generateParameterName($field);
$queryBuilder
->andWhere(sprintf($wrapCase('%s.%s').' IN (:%s)', $alias, $field, $valueParameter))
->setParameter($valueParameter, $caseSensitive ? $values : array_map('strtolower', $values));
}
// metadata doesn't have the field, nor an association on the field
if (!$metadata->hasAssociation($field)) {
return;
}
$values = array_map([$this, 'getIdFromValue'], $values);
$this->logger->info('checking array map of values: '. print_r($values, true));
if (!$this->hasValidValues($values, $this->getDoctrineFieldType($property, $resourceClass))) {
$this->logger->notice('Invalid filter ignored: Invalid value for the provided field', [
'exception' => new InvalidArgumentException(sprintf('Values for field "%s" are not valid according to the doctrine type.', $field)),
]);
return;
}
$association = $field;
$valueParameter = $queryNameGenerator->generateParameterName($association);
if ($metadata->isCollectionValuedAssociation($association)) {
$associationAlias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $alias, $association);
$associationField = 'id';
} else {
$associationAlias = $alias;
$associationField = $field;
}
if (1 === \count($values)) {
$queryBuilder
->andWhere(sprintf('%s.%s = :%s', $associationAlias, $associationField, $valueParameter))
->setParameter($valueParameter, $values[0]);
} else {
$queryBuilder
->andWhere(sprintf('%s.%s IN (:%s)', $associationAlias, $associationField, $valueParameter))
->setParameter($valueParameter, $values);
}
}
/**
* Adds where clause according to the strategy.
*
* @throws InvalidArgumentException If strategy does not exist
*/
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $value, bool $caseSensitive)
{
$wrapCase = $this->createWrapCase($caseSensitive);
$valueParameter = $queryNameGenerator->generateParameterName($field);
switch ($strategy) {
case null:
case self::STRATEGY_EXACT:
$queryBuilder
->andWhere(sprintf($wrapCase('%s.%s').' = '.$wrapCase(':%s'), $alias, $field, $valueParameter))
->setParameter($valueParameter, $value);
break;
case self::STRATEGY_PARTIAL:
$queryBuilder
->andWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(\'%%\', :%s, \'%%\')'), $alias, $field, $valueParameter))
->setParameter($valueParameter, $value);
break;
case self::STRATEGY_START:
$queryBuilder
->andWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(:%s, \'%%\')'), $alias, $field, $valueParameter))
->setParameter($valueParameter, $value);
break;
case self::STRATEGY_END:
$queryBuilder
->andWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(\'%%\', :%s)'), $alias, $field, $valueParameter))
->setParameter($valueParameter, $value);
break;
case self::STRATEGY_WORD_START:
$queryBuilder
->andWhere(sprintf($wrapCase('%1$s.%2$s').' LIKE '.$wrapCase('CONCAT(:%3$s, \'%%\')').' OR '.$wrapCase('%1$s.%2$s').' LIKE '.$wrapCase('CONCAT(\'%% \', :%3$s, \'%%\')'), $alias, $field, $valueParameter))
->setParameter($valueParameter, $value);
break;
default:
throw new InvalidArgumentException(sprintf('strategy %s does not exist.', $strategy));
}
}
/**
* Creates a function that will wrap a Doctrine expression according to the
* specified case sensitivity.
*
* For example, "o.name" will get wrapped into "LOWER(o.name)" when $caseSensitive
* is false.
*/
protected function createWrapCase(bool $caseSensitive): \Closure
{
return function (string $expr) use ($caseSensitive): string {
if ($caseSensitive) {
return $expr;
}
return sprintf('LOWER(%s)', $expr);
};
}
/**
* Gets the ID from an IRI or a raw ID.
*/
protected function getIdFromValue(string $value)
{
try {
if ($item = $this->iriConverter->getItemFromIri($value, ['fetch_data' => false])) {
return $this->propertyAccessor->getValue($item, 'id');
}
} catch (InvalidArgumentException $e) {
// Do nothing, return the raw value
}
return $value;
}
/**
* Test the values array for a provided search method
* It will be the key for the array
* Use that term to set the search strategy for the property
* Return the updated values array for use in normalize values
*/
protected function testForProvidedSearchStrategy(array $value, $property): array
{
$this->logger->info('in testForProvidedSearchStrategy');
$this->logger->info('value: '. print_r($value, true));
$this->logger->info('property: '. $property);
$valid_strategies = [
self::STRATEGY_START,
self::STRATEGY_END,
self::STRATEGY_EXACT,
self::STRATEGY_PARTIAL,
self::STRATEGY_WORD_START
];
$updated_value = [];
foreach ($value as $k => $v) {
if (!\is_int($k)) {
//test to see what we have
if (\in_array($k, $valid_strategies)) {
//Set the strategy
$this->properties[$property] = $k;
}
//reset the Values array to be a standard number indexed array
$updated_value = $this->convertArray($v);
} else {
$updated_value = $value;
}
}
return $updated_value;
}
/**
* strip the key and return the value as its own numerically indexed array
*/
protected function convertArray($value): array
{
switch (gettype($value)) {
case ('array'):
return $value;
break;
default:
return [$value];
}
}
/**
* Normalize the values array.
*/
protected function normalizeValues(array $values): array
{
foreach ($values as $key => $value) {
if (!\is_int($key) || !\is_string($value)) {
unset($values[$key]);
}
}
return array_values($values);
}
/**
* When the field should be an integer, check that the given value is a valid one.
*
* @param Type|string $type
*/
protected function hasValidValues(array $values, $type = null): bool
{
foreach ($values as $key => $value) {
if (Type::INTEGER === $type && null !== $value && false === filter_var($value, FILTER_VALIDATE_INT)) {
return false;
}
}
return true;
}
}