|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Doctrine\Orm\Filter; |
| 15 | + |
| 16 | +use ApiPlatform\Doctrine\Common\Filter\OpenApiFilterTrait; |
| 17 | +use ApiPlatform\Doctrine\Orm\NestedPropertyHelperTrait; |
| 18 | +use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
| 19 | +use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait; |
| 20 | +use ApiPlatform\Metadata\Exception\InvalidArgumentException; |
| 21 | +use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
| 22 | +use ApiPlatform\Metadata\Operation; |
| 23 | +use Doctrine\ORM\QueryBuilder; |
| 24 | + |
| 25 | +/** |
| 26 | + * Filters the collection by a word boundary prefix, matching fields that contain a word starting with the value, |
| 27 | + * using a `LIKE 'value%' OR LIKE '% value%'` clause. |
| 28 | + */ |
| 29 | +final class WordStartSearchFilter implements FilterInterface, OpenApiParameterFilterInterface |
| 30 | +{ |
| 31 | + use BackwardCompatibleFilterDescriptionTrait; |
| 32 | + use NestedPropertyHelperTrait; |
| 33 | + use OpenApiFilterTrait; |
| 34 | + |
| 35 | + public function __construct(private readonly bool $caseSensitive = false) |
| 36 | + { |
| 37 | + } |
| 38 | + |
| 39 | + public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void |
| 40 | + { |
| 41 | + $parameter = $context['parameter']; |
| 42 | + |
| 43 | + if (null === $parameter->getProperty()) { |
| 44 | + throw new InvalidArgumentException(\sprintf('The filter parameter with key "%s" must specify a property. Please provide the property explicitly.', $parameter->getKey())); |
| 45 | + } |
| 46 | + |
| 47 | + $property = $parameter->getProperty(); |
| 48 | + $alias = $queryBuilder->getRootAliases()[0]; |
| 49 | + [$alias, $property] = $this->addNestedParameterJoins($property, $alias, $queryBuilder, $queryNameGenerator, $parameter); |
| 50 | + $field = $alias.'.'.$property; |
| 51 | + $values = $parameter->getValue(); |
| 52 | + |
| 53 | + if (!is_iterable($values)) { |
| 54 | + $values = [$values]; |
| 55 | + } |
| 56 | + |
| 57 | + $expressions = []; |
| 58 | + foreach ($values as $val) { |
| 59 | + $startName = $queryNameGenerator->generateParameterName($property); |
| 60 | + $wordName = $queryNameGenerator->generateParameterName($property); |
| 61 | + |
| 62 | + $expressions[] = $queryBuilder->expr()->orX( |
| 63 | + $this->createLikeExpression($field, $startName), |
| 64 | + $this->createLikeExpression($field, $wordName), |
| 65 | + ); |
| 66 | + |
| 67 | + $queryBuilder->setParameter($startName, $this->formatStartValue($val)); |
| 68 | + $queryBuilder->setParameter($wordName, $this->formatWordValue($val)); |
| 69 | + } |
| 70 | + |
| 71 | + $queryBuilder->{$context['whereClause'] ?? 'andWhere'}( |
| 72 | + $queryBuilder->expr()->orX(...$expressions) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + private function createLikeExpression(string $field, string $parameterName): string |
| 77 | + { |
| 78 | + return $this->caseSensitive |
| 79 | + ? $field.' LIKE :'.$parameterName.' ESCAPE \'\\\'' |
| 80 | + : 'LOWER('.$field.') LIKE LOWER(:'.$parameterName.') ESCAPE \'\\\''; |
| 81 | + } |
| 82 | + |
| 83 | + private function formatStartValue(string $value): string |
| 84 | + { |
| 85 | + return addcslashes($value, '\\%_').'%'; |
| 86 | + } |
| 87 | + |
| 88 | + private function formatWordValue(string $value): string |
| 89 | + { |
| 90 | + return '% '.addcslashes($value, '\\%_').'%'; |
| 91 | + } |
| 92 | +} |
0 commit comments