-
-
Notifications
You must be signed in to change notification settings - Fork 959
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
Open
soyuka
wants to merge
2
commits into
api-platform:main
Choose a base branch
from
soyuka:comparison
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+555
−10
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| <?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 const ALLOWED_COMPARISON_METHODS = ['equals', 'gt', 'gte', 'lt', '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(); | ||
| $schema = $this->getInnerSchema($parameter); | ||
|
|
||
| return [ | ||
| new OpenApiParameter(name: "{$key}[gt]", in: $in, schema: $schema), | ||
| new OpenApiParameter(name: "{$key}[gte]", in: $in, schema: $schema), | ||
| new OpenApiParameter(name: "{$key}[lt]", in: $in, schema: $schema), | ||
| new OpenApiParameter(name: "{$key}[lte]", in: $in, schema: $schema), | ||
| ]; | ||
| } | ||
|
|
||
| public function getSchema(Parameter $parameter): array | ||
| { | ||
| $innerSchema = $this->getInnerSchema($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']; | ||
| } | ||
| } | ||
|
|
||
| private function getInnerSchema(Parameter $parameter): array | ||
| { | ||
| if ($this->filter instanceof JsonSchemaFilterInterface) { | ||
| return $this->filter->getSchema($parameter); | ||
| } | ||
|
|
||
| return ['type' => 'string']; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| <?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.
Show resolved
Hide resolved
|
||
| 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(); | ||
| $schema = $this->getInnerSchema($parameter); | ||
|
|
||
| return [ | ||
| new OpenApiParameter(name: "{$key}[gt]", in: $in, schema: $schema), | ||
| new OpenApiParameter(name: "{$key}[gte]", in: $in, schema: $schema), | ||
| new OpenApiParameter(name: "{$key}[lt]", in: $in, schema: $schema), | ||
| new OpenApiParameter(name: "{$key}[lte]", in: $in, schema: $schema), | ||
| ]; | ||
| } | ||
|
|
||
| public function getSchema(Parameter $parameter): array | ||
| { | ||
| $innerSchema = $this->getInnerSchema($parameter); | ||
|
|
||
| return [ | ||
| 'type' => 'object', | ||
| 'properties' => [ | ||
| 'gt' => $innerSchema, | ||
| 'gte' => $innerSchema, | ||
| 'lt' => $innerSchema, | ||
| 'lte' => $innerSchema, | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| 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 | ||
| ); | ||
| } | ||
|
|
||
| private function getInnerSchema(Parameter $parameter): array | ||
| { | ||
| if ($this->filter instanceof JsonSchemaFilterInterface) { | ||
| return $this->filter->getSchema($parameter); | ||
| } | ||
|
|
||
| return ['type' => 'string']; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.