|
| 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\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\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
| 22 | +use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait; |
| 23 | +use ApiPlatform\Metadata\JsonSchemaFilterInterface; |
| 24 | +use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
| 25 | +use ApiPlatform\Metadata\Operation; |
| 26 | +use ApiPlatform\Metadata\Parameter; |
| 27 | +use ApiPlatform\Metadata\QueryParameter; |
| 28 | +use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; |
| 29 | +use Doctrine\ORM\QueryBuilder; |
| 30 | + |
| 31 | +/** |
| 32 | + * Decorates an equality filter (ExactFilter, UuidFilter) to add comparison operators (gt, gte, lt, lte). |
| 33 | + * |
| 34 | + * @experimental |
| 35 | + */ |
| 36 | +final class ComparisonFilter implements FilterInterface, OpenApiParameterFilterInterface, JsonSchemaFilterInterface, ManagerRegistryAwareInterface, LoggerAwareInterface |
| 37 | +{ |
| 38 | + use BackwardCompatibleFilterDescriptionTrait; |
| 39 | + use LoggerAwareTrait; |
| 40 | + use ManagerRegistryAwareTrait; |
| 41 | + use OpenApiFilterTrait; |
| 42 | + |
| 43 | + private const OPERATORS = [ |
| 44 | + 'gt' => '>', |
| 45 | + 'gte' => '>=', |
| 46 | + 'lt' => '<', |
| 47 | + 'lte' => '<=', |
| 48 | + ]; |
| 49 | + |
| 50 | + public const ALLOWED_DQL_OPERATORS = ['=', '>', '>=', '<', '<=', '!=', '<>']; |
| 51 | + |
| 52 | + public function __construct(private readonly FilterInterface $filter) |
| 53 | + { |
| 54 | + } |
| 55 | + |
| 56 | + public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, 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($queryBuilder, $queryNameGenerator, $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 | + $schema = $this->getInnerSchema($parameter); |
| 89 | + |
| 90 | + return [ |
| 91 | + new OpenApiParameter(name: "{$key}[gt]", in: $in, schema: $schema), |
| 92 | + new OpenApiParameter(name: "{$key}[gte]", in: $in, schema: $schema), |
| 93 | + new OpenApiParameter(name: "{$key}[lt]", in: $in, schema: $schema), |
| 94 | + new OpenApiParameter(name: "{$key}[lte]", in: $in, schema: $schema), |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | + public function getSchema(Parameter $parameter): array |
| 99 | + { |
| 100 | + $innerSchema = $this->getInnerSchema($parameter); |
| 101 | + |
| 102 | + return [ |
| 103 | + 'type' => 'object', |
| 104 | + 'properties' => [ |
| 105 | + 'gt' => $innerSchema, |
| 106 | + 'gte' => $innerSchema, |
| 107 | + 'lt' => $innerSchema, |
| 108 | + 'lte' => $innerSchema, |
| 109 | + ], |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
| 113 | + private function applyOperator(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation, array $context, Parameter $parameter, string $operator, mixed $value): void |
| 114 | + { |
| 115 | + if (!\is_string($value) && !is_numeric($value) && !$value instanceof \DateTimeInterface) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + $subParameter = (clone $parameter)->setValue($value); |
| 120 | + $this->filter->apply( |
| 121 | + $queryBuilder, |
| 122 | + $queryNameGenerator, |
| 123 | + $resourceClass, |
| 124 | + $operation, |
| 125 | + ['operator' => $operator, 'parameter' => $subParameter] + $context |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + private function getInnerSchema(Parameter $parameter): array |
| 130 | + { |
| 131 | + if ($this->filter instanceof JsonSchemaFilterInterface) { |
| 132 | + return $this->filter->getSchema($parameter); |
| 133 | + } |
| 134 | + |
| 135 | + return ['type' => 'string']; |
| 136 | + } |
| 137 | +} |
0 commit comments