|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 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\Odm\Filter; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
| 17 | +use ApiPlatform\Metadata\Operation; |
| 18 | +use ApiPlatform\Metadata\Parameter; |
| 19 | +use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; |
| 20 | +use Doctrine\ODM\MongoDB\Aggregation\Builder; |
| 21 | + |
| 22 | +final class ExactFilter implements FilterInterface, OpenApiParameterFilterInterface |
| 23 | +{ |
| 24 | + public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void |
| 25 | + { |
| 26 | + if (!$parameter = $context['parameter'] ?? null) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + \assert($parameter instanceof Parameter); |
| 31 | + |
| 32 | + $values = (array) $parameter->getValue(); |
| 33 | + if ([] === $values) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + $matchField = $parameter->getProperty(); |
| 38 | + $fieldQuery = $aggregationBuilder->match()->field($matchField); |
| 39 | + |
| 40 | + if (\count($values) > 1) { |
| 41 | + $fieldQuery->in($values); |
| 42 | + } else { |
| 43 | + $fieldQuery->equals(reset($values)); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null |
| 48 | + { |
| 49 | + return new OpenApiParameter(name: $parameter->getKey().'[]', in: 'query', style: 'deepObject', explode: true); |
| 50 | + } |
| 51 | + |
| 52 | + public function getDescription(string $resourceClass): array |
| 53 | + { |
| 54 | + return []; |
| 55 | + } |
| 56 | +} |
0 commit comments