|
| 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\Odm\Filter; |
| 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\Filter\OpenApiFilterTrait; |
| 21 | +use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait; |
| 22 | +use ApiPlatform\Metadata\JsonSchemaFilterInterface; |
| 23 | +use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
| 24 | +use ApiPlatform\Metadata\Operation; |
| 25 | +use ApiPlatform\Metadata\Parameter; |
| 26 | +use ApiPlatform\Metadata\QueryParameter; |
| 27 | +use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; |
| 28 | +use Doctrine\ODM\MongoDB\Aggregation\Builder; |
| 29 | + |
| 30 | +/** |
| 31 | + * Decorates an equality filter (ExactFilter) to add comparison operators (gt, gte, lt, lte). |
| 32 | + * |
| 33 | + * @experimental |
| 34 | + */ |
| 35 | +final class ComparisonFilter implements FilterInterface, OpenApiParameterFilterInterface, JsonSchemaFilterInterface, ManagerRegistryAwareInterface, LoggerAwareInterface |
| 36 | +{ |
| 37 | + use BackwardCompatibleFilterDescriptionTrait; |
| 38 | + use LoggerAwareTrait; |
| 39 | + use ManagerRegistryAwareTrait; |
| 40 | + use OpenApiFilterTrait; |
| 41 | + |
| 42 | + private const OPERATORS = [ |
| 43 | + 'gt' => 'gt', |
| 44 | + 'gte' => 'gte', |
| 45 | + 'lt' => 'lt', |
| 46 | + 'lte' => 'lte', |
| 47 | + ]; |
| 48 | + |
| 49 | + public function __construct(private readonly FilterInterface $filter) |
| 50 | + { |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param-out array<string, mixed> $context |
| 55 | + */ |
| 56 | + public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void |
| 57 | + { |
| 58 | + if ($this->filter instanceof ManagerRegistryAwareInterface) { |
| 59 | + $this->filter->setManagerRegistry($this->getManagerRegistry()); |
| 60 | + } |
| 61 | + |
| 62 | + if ($this->filter instanceof LoggerAwareInterface) { |
| 63 | + $this->filter->setLogger($this->getLogger()); |
| 64 | + } |
| 65 | + |
| 66 | + $parameter = $context['parameter']; |
| 67 | + $values = $parameter->getValue(); |
| 68 | + |
| 69 | + if (!\is_array($values)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + foreach ($values as $operator => $value) { |
| 74 | + if ('' === $value || null === $value) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + if (isset(self::OPERATORS[$operator])) { |
| 79 | + $this->applyOperator($aggregationBuilder, $resourceClass, $operation, $context, $parameter, self::OPERATORS[$operator], $value); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public function getOpenApiParameters(Parameter $parameter): array |
| 85 | + { |
| 86 | + $in = $parameter instanceof QueryParameter ? 'query' : 'header'; |
| 87 | + $key = $parameter->getKey(); |
| 88 | + |
| 89 | + return [ |
| 90 | + new OpenApiParameter(name: "{$key}[gt]", in: $in), |
| 91 | + new OpenApiParameter(name: "{$key}[gte]", in: $in), |
| 92 | + new OpenApiParameter(name: "{$key}[lt]", in: $in), |
| 93 | + new OpenApiParameter(name: "{$key}[lte]", in: $in), |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | + public function getSchema(Parameter $parameter): array |
| 98 | + { |
| 99 | + $innerSchema = ['type' => 'string']; |
| 100 | + if ($this->filter instanceof JsonSchemaFilterInterface) { |
| 101 | + $innerSchema = $this->filter->getSchema($parameter); |
| 102 | + } |
| 103 | + |
| 104 | + return [ |
| 105 | + 'type' => 'object', |
| 106 | + 'properties' => [ |
| 107 | + 'gt' => $innerSchema, |
| 108 | + 'gte' => $innerSchema, |
| 109 | + 'lt' => $innerSchema, |
| 110 | + 'lte' => $innerSchema, |
| 111 | + ], |
| 112 | + ]; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @param array<string, mixed> $context |
| 117 | + * |
| 118 | + * @param-out array<string, mixed> $context |
| 119 | + */ |
| 120 | + private function applyOperator(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation, array &$context, Parameter $parameter, string $comparisonMethod, mixed $value): void |
| 121 | + { |
| 122 | + if (!\is_string($value) && !is_numeric($value) && !$value instanceof \DateTimeInterface) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + $subParameter = (clone $parameter)->setValue($value); |
| 127 | + $newContext = ['comparisonMethod' => $comparisonMethod, 'parameter' => $subParameter] + $context; |
| 128 | + $this->filter->apply($aggregationBuilder, $resourceClass, $operation, $newContext); |
| 129 | + if (isset($newContext['match'])) { |
| 130 | + $context['match'] = $newContext['match']; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments