Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 979
feat(doctrine): ComparisonFilter decorator for range filtering#7760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| <?php | ||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
| namespace ApiPlatform\Doctrine\Odm\Filter; | ||
| use ApiPlatform\Doctrine\Common\Filter\LoggerAwareInterface; | ||
| use ApiPlatform\Doctrine\Common\Filter\LoggerAwareTrait; | ||
| use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface; | ||
| use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareTrait; | ||
| use ApiPlatform\Doctrine\Common\Filter\OpenApiFilterTrait; | ||
| use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait; | ||
| use ApiPlatform\Metadata\JsonSchemaFilterInterface; | ||
| use ApiPlatform\Metadata\OpenApiParameterFilterInterface; | ||
| use ApiPlatform\Metadata\Operation; | ||
| use ApiPlatform\Metadata\Parameter; | ||
| use ApiPlatform\Metadata\QueryParameter; | ||
| use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; | ||
| use Doctrine\ODM\MongoDB\Aggregation\Builder; | ||
| /** | ||
| * Decorates an equality filter (ExactFilter) to add comparison operators (gt, gte, lt, lte). | ||
| * | ||
| * @experimental | ||
| */ | ||
| final class ComparisonFilter implements FilterInterface, OpenApiParameterFilterInterface, JsonSchemaFilterInterface, ManagerRegistryAwareInterface, LoggerAwareInterface | ||
| { | ||
| use BackwardCompatibleFilterDescriptionTrait; | ||
| use LoggerAwareTrait; | ||
| use ManagerRegistryAwareTrait; | ||
| use OpenApiFilterTrait; | ||
| private const OPERATORS = [ | ||
| 'gt' => 'gt', | ||
| 'gte' => 'gte', | ||
| 'lt' => 'lt', | ||
| 'lte' => 'lte', | ||
| ]; | ||
| public function __construct(private readonly FilterInterface $filter) | ||
| { | ||
| } | ||
| /** | ||
| * @param-out array<string, mixed> $context | ||
| */ | ||
| public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void | ||
| { | ||
| if ($this->filter instanceof ManagerRegistryAwareInterface) { | ||
| $this->filter->setManagerRegistry($this->getManagerRegistry()); | ||
| } | ||
| if ($this->filter instanceof LoggerAwareInterface) { | ||
| $this->filter->setLogger($this->getLogger()); | ||
| } | ||
| $parameter = $context['parameter']; | ||
| $values = $parameter->getValue(); | ||
| if (!\is_array($values)) { | ||
| return; | ||
| } | ||
| foreach ($values as $operator => $value) { | ||
| if ('' === $value || null === $value) { | ||
| continue; | ||
| } | ||
| if (isset(self::OPERATORS[$operator])) { | ||
| $this->applyOperator($aggregationBuilder, $resourceClass, $operation, $context, $parameter, self::OPERATORS[$operator], $value); | ||
| } | ||
| } | ||
| } | ||
| public function getOpenApiParameters(Parameter $parameter): array | ||
| { | ||
| $in = $parameter instanceof QueryParameter ? 'query' : 'header'; | ||
| $key = $parameter->getKey(); | ||
| return [ | ||
| new OpenApiParameter(name: "{$key}[gt]", in: $in), | ||
| new OpenApiParameter(name: "{$key}[gte]", in: $in), | ||
| new OpenApiParameter(name: "{$key}[lt]", in: $in), | ||
| new OpenApiParameter(name: "{$key}[lte]", in: $in), | ||
| ]; | ||
| } | ||
| public function getSchema(Parameter $parameter): array | ||
| { | ||
| $innerSchema = ['type' => 'string']; | ||
| if ($this->filter instanceof JsonSchemaFilterInterface) { | ||
| $innerSchema = $this->filter->getSchema($parameter); | ||
| } | ||
| return [ | ||
| 'type' => 'object', | ||
| 'properties' => [ | ||
| 'gt' => $innerSchema, | ||
| 'gte' => $innerSchema, | ||
| 'lt' => $innerSchema, | ||
| 'lte' => $innerSchema, | ||
| ], | ||
| ]; | ||
| } | ||
| /** | ||
| * @param array<string, mixed> $context | ||
| * | ||
| * @param-out array<string, mixed> $context | ||
| */ | ||
| private function applyOperator(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation, array &$context, Parameter $parameter, string $comparisonMethod, mixed $value): void | ||
| { | ||
| if (!\is_string($value) && !is_numeric($value) && !$value instanceof \DateTimeInterface) { | ||
| return; | ||
| } | ||
| $subParameter = (clone $parameter)->setValue($value); | ||
| $newContext = ['comparisonMethod' => $comparisonMethod, 'parameter' => $subParameter] + $context; | ||
| $this->filter->apply($aggregationBuilder, $resourceClass, $operation, $newContext); | ||
| if (isset($newContext['match'])) { | ||
| $context['match'] = $newContext['match']; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| <?php | ||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
| namespace ApiPlatform\Doctrine\Orm\Filter; | ||
| use ApiPlatform\Doctrine\Common\Filter\LoggerAwareInterface; | ||
| use ApiPlatform\Doctrine\Common\Filter\LoggerAwareTrait; | ||
| use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface; | ||
| use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareTrait; | ||
| use ApiPlatform\Doctrine\Common\Filter\OpenApiFilterTrait; | ||
| use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
| use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait; | ||
| use ApiPlatform\Metadata\JsonSchemaFilterInterface; | ||
| use ApiPlatform\Metadata\OpenApiParameterFilterInterface; | ||
| use ApiPlatform\Metadata\Operation; | ||
| use ApiPlatform\Metadata\Parameter; | ||
| use ApiPlatform\Metadata\QueryParameter; | ||
| use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; | ||
| use Doctrine\ORM\QueryBuilder; | ||
| /** | ||
| * Decorates an equality filter (ExactFilter, UuidFilter) to add comparison operators (gt, gte, lt, lte). | ||
| * | ||
| * @experimental | ||
| */ | ||
soyuka marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| final class ComparisonFilter implements FilterInterface, OpenApiParameterFilterInterface, JsonSchemaFilterInterface, ManagerRegistryAwareInterface, LoggerAwareInterface | ||
| { | ||
| use BackwardCompatibleFilterDescriptionTrait; | ||
| use LoggerAwareTrait; | ||
| use ManagerRegistryAwareTrait; | ||
| use OpenApiFilterTrait; | ||
| private const OPERATORS = [ | ||
| 'gt' => '>', | ||
| 'gte' => '>=', | ||
| 'lt' => '<', | ||
| 'lte' => '<=', | ||
| ]; | ||
| public const ALLOWED_DQL_OPERATORS = ['=', '>', '>=', '<', '<=', '!=', '<>']; | ||
| public function __construct(private readonly FilterInterface $filter) | ||
| { | ||
| } | ||
| public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void | ||
| { | ||
| if ($this->filter instanceof ManagerRegistryAwareInterface) { | ||
| $this->filter->setManagerRegistry($this->getManagerRegistry()); | ||
| } | ||
| if ($this->filter instanceof LoggerAwareInterface) { | ||
| $this->filter->setLogger($this->getLogger()); | ||
| } | ||
| $parameter = $context['parameter']; | ||
| $values = $parameter->getValue(); | ||
| if (!\is_array($values)) { | ||
| return; | ||
| } | ||
| foreach ($values as $operator => $value) { | ||
| if ('' === $value || null === $value) { | ||
| continue; | ||
| } | ||
| if (isset(self::OPERATORS[$operator])) { | ||
| $this->applyOperator($queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context, $parameter, self::OPERATORS[$operator], $value); | ||
| } | ||
| } | ||
| } | ||
| public function getOpenApiParameters(Parameter $parameter): array | ||
| { | ||
| $in = $parameter instanceof QueryParameter ? 'query' : 'header'; | ||
| $key = $parameter->getKey(); | ||
| return [ | ||
| new OpenApiParameter(name: "{$key}[gt]", in: $in), | ||
| new OpenApiParameter(name: "{$key}[gte]", in: $in), | ||
| new OpenApiParameter(name: "{$key}[lt]", in: $in), | ||
| new OpenApiParameter(name: "{$key}[lte]", in: $in), | ||
| ]; | ||
| } | ||
| public function getSchema(Parameter $parameter): array | ||
| { | ||
| $innerSchema = ['type' => 'string']; | ||
| if ($this->filter instanceof JsonSchemaFilterInterface) { | ||
| $innerSchema = $this->filter->getSchema($parameter); | ||
| } | ||
| return [ | ||
| 'type' => 'object', | ||
| 'properties' => [ | ||
| 'gt' => $innerSchema, | ||
| 'gte' => $innerSchema, | ||
| 'lt' => $innerSchema, | ||
| 'lte' => $innerSchema, | ||
| ], | ||
| ]; | ||
| } | ||
| /** | ||
| * @param array<string,mixed> $context | ||
| */ | ||
| private function applyOperator(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation, array $context, Parameter $parameter, string $operator, mixed $value): void | ||
| { | ||
| if (!\is_string($value) && !is_numeric($value) && !$value instanceof \DateTimeInterface) { | ||
| return; | ||
| } | ||
| $subParameter = (clone $parameter)->setValue($value); | ||
| $this->filter->apply( | ||
| $queryBuilder, | ||
| $queryNameGenerator, | ||
| $resourceClass, | ||
| $operation, | ||
| ['operator' => $operator, 'parameter' => $subParameter] + $context | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.