|
13 | 13 |
|
14 | 14 | namespace ApiPlatform\Doctrine\Orm\Filter; |
15 | 15 |
|
| 16 | +use ApiPlatform\Doctrine\Common\Filter\LoggerAwareInterface; |
| 17 | +use ApiPlatform\Doctrine\Common\Filter\LoggerAwareTrait; |
| 18 | +use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface; |
| 19 | +use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareTrait; |
| 20 | +use ApiPlatform\Doctrine\Common\PropertyHelperTrait; |
| 21 | +use ApiPlatform\Doctrine\Orm\PropertyHelperTrait as OrmPropertyHelperTrait; |
16 | 22 | use ApiPlatform\Doctrine\Orm\Util\QueryBuilderHelper; |
17 | 23 | use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
18 | 24 | use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait; |
19 | | -use ApiPlatform\Metadata\Exception\InvalidArgumentException; |
| 25 | +use ApiPlatform\Metadata\JsonSchemaFilterInterface; |
20 | 26 | use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
21 | 27 | use ApiPlatform\Metadata\Operation; |
22 | 28 | use ApiPlatform\Metadata\Parameter; |
|
32 | 38 | /** |
33 | 39 | * @internal |
34 | 40 | */ |
35 | | -class AbstractUuidFilter extends AbstractFilter implements OpenApiParameterFilterInterface |
| 41 | +class AbstractUuidFilter implements FilterInterface, ManagerRegistryAwareInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface, LoggerAwareInterface |
36 | 42 | { |
37 | 43 | use BackwardCompatibleFilterDescriptionTrait; |
| 44 | + use LoggerAwareTrait; |
| 45 | + use ManagerRegistryAwareTrait; |
| 46 | + use OrmPropertyHelperTrait; |
| 47 | + use PropertyHelperTrait; |
38 | 48 |
|
39 | 49 | private const UUID_SCHEMA = [ |
40 | 50 | 'type' => 'string', |
41 | 51 | 'format' => 'uuid', |
42 | 52 | ]; |
43 | 53 |
|
44 | | - protected function filterProperty(string $property, mixed $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void |
| 54 | + public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void |
45 | 55 | { |
46 | | - if ( |
47 | | - null === $value |
48 | | - || !$this->isPropertyEnabled($property, $resourceClass) |
49 | | - || !$this->isPropertyMapped($property, $resourceClass, true) |
50 | | - ) { |
| 56 | + $parameter = $context['parameter'] ?? null; |
| 57 | + if (!$parameter) { |
51 | 58 | return; |
52 | 59 | } |
53 | 60 |
|
| 61 | + $this->filterProperty($parameter->getProperty(), $parameter->getValue(), $queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context); |
| 62 | + } |
| 63 | + |
| 64 | + protected function filterProperty(string $property, mixed $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void |
| 65 | + { |
54 | 66 | $alias = $queryBuilder->getRootAliases()[0]; |
55 | 67 | $field = $property; |
56 | 68 |
|
57 | | - $values = $this->normalizeValues((array) $value, $property); |
58 | | - if (null === $values) { |
59 | | - return; |
60 | | - } |
61 | | - |
| 69 | + $values = (array) $value; |
62 | 70 | $associations = []; |
63 | 71 | if ($this->isPropertyNested($property, $resourceClass)) { |
64 | 72 | [$alias, $field, $associations] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass, Join::INNER_JOIN); |
@@ -185,25 +193,8 @@ public function getOpenApiParameters(Parameter $parameter): array |
185 | 193 | ]; |
186 | 194 | } |
187 | 195 |
|
188 | | - /** |
189 | | - * Normalize the values array. |
190 | | - */ |
191 | | - protected function normalizeValues(array $values, string $property): ?array |
| 196 | + public function getSchema(Parameter $parameter): array |
192 | 197 | { |
193 | | - foreach ($values as $key => $value) { |
194 | | - if (!\is_string($value)) { |
195 | | - unset($values[$key]); |
196 | | - } |
197 | | - } |
198 | | - |
199 | | - if (0 === \count($values)) { |
200 | | - $this->getLogger()->notice('Invalid filter ignored', [ |
201 | | - 'exception' => new InvalidArgumentException(\sprintf('At least one value is required, multiple values should be in "%1$s[]=019b3c90-e265-72e5-a594-17b446a4067f&%1$s[]=019b3c9b-bce6-76dc-a066-9a44f4ec253f" format', $property)), |
202 | | - ]); |
203 | | - |
204 | | - return null; |
205 | | - } |
206 | | - |
207 | | - return array_values($values); |
| 198 | + return self::UUID_SCHEMA; |
208 | 199 | } |
209 | 200 | } |
0 commit comments