-
-
Notifications
You must be signed in to change notification settings - Fork 950
feat(doctrine): new search filters #7121
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
Merged
soyuka
merged 25 commits into
api-platform:4.2
from
vinceAmstoutz:feat/new-doctrine-iri-search-filters
Aug 27, 2025
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
e04c3c0
feat: iri search filter
soyuka cab6bd6
fix: apply last reviews made in th latest pr
vinceAmstoutz afd57e3
feat(doctrine): add ORM ExactFilter
vinceAmstoutz ed57ace
feat(doctrine): add ORM PartialSearchFilter
vinceAmstoutz 79bfc31
refactor(test): unifies fixtures for filter
vinceAmstoutz e1a676d
feat(doctrine): finish ODM IriFilter
vinceAmstoutz fff2121
feat(doctrine): add ODM ExactFilter
vinceAmstoutz 8b6d41f
feat(doctrine): add ODM PartialSearchFilter
vinceAmstoutz d9911c2
refactor(doctrine): remove dead code
vinceAmstoutz 7943174
fix(test): deprecation on kernel will not always be booted
vinceAmstoutz 0c304f3
feat(mongodb): ODM support for new filters
vinceAmstoutz cbab08b
refactor(state): remove duplicate symbols
vinceAmstoutz 98109cf
cs(state): phpdoc rather than asserting
vinceAmstoutz a471dfa
refactor(doctrine): introduce OpenApiFilterTrait.php
vinceAmstoutz b5e6ac8
feat(doctrine): add OrFilter for ORM and ODM
vinceAmstoutz 11f9298
cs(doctrine): fix typing issues
vinceAmstoutz ec42ce2
fix(doctrine): add authorship
vinceAmstoutz d065380
fix(test): apply review requested changes
vinceAmstoutz 9f82c5a
feat(mongodb): handle aggregation for PartialSearchFilter
vinceAmstoutz 74a21ef
fix(test): apply review requested changes
vinceAmstoutz a27de22
fix(mongodb): start fixing filters when relations
vinceAmstoutz da4c493
fix(mongodb): add LoggerAwareTrait and ManagerRegistryAwareTrait, imp…
soyuka 61e1b9a
fix(mongodb): enhance ExactFilter and IriFilter to better handle rela…
vinceAmstoutz 57ed37e
fixes
soyuka 7349cec
more
soyuka 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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\Common\Filter; | ||
|
|
||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| interface LoggerAwareInterface | ||
| { | ||
| public function hasLogger(): bool; | ||
|
|
||
| public function getLogger(): LoggerInterface; | ||
|
|
||
| public function setLogger(LoggerInterface $logger): void; | ||
| } |
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,37 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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\Common\Filter; | ||
|
|
||
| use Psr\Log\LoggerInterface; | ||
| use Psr\Log\NullLogger; | ||
|
|
||
| trait LoggerAwareTrait | ||
| { | ||
| private ?LoggerInterface $logger = null; | ||
|
|
||
| public function hasLogger(): bool | ||
| { | ||
| return $this->logger instanceof LoggerInterface; | ||
| } | ||
|
|
||
| public function getLogger(): LoggerInterface | ||
| { | ||
| return $this->logger ??= new NullLogger(); | ||
| } | ||
|
|
||
| public function setLogger(LoggerInterface $logger): void | ||
| { | ||
| $this->logger = $logger; | ||
| } | ||
| } |
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,41 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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\Common\Filter; | ||
|
|
||
| use ApiPlatform\Metadata\Exception\RuntimeException; | ||
| use Doctrine\Persistence\ManagerRegistry; | ||
|
|
||
| trait ManagerRegistryAwareTrait | ||
| { | ||
| private ?ManagerRegistry $managerRegistry = null; | ||
|
|
||
| public function hasManagerRegistry(): bool | ||
| { | ||
| return $this->managerRegistry instanceof ManagerRegistry; | ||
| } | ||
|
|
||
| public function getManagerRegistry(): ManagerRegistry | ||
| { | ||
| if (!$this->hasManagerRegistry()) { | ||
| throw new RuntimeException('ManagerRegistry must be initialized before accessing it.'); | ||
| } | ||
|
|
||
| return $this->managerRegistry; | ||
| } | ||
|
|
||
| public function setManagerRegistry(ManagerRegistry $managerRegistry): void | ||
| { | ||
| $this->managerRegistry = $managerRegistry; | ||
| } | ||
| } |
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,28 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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\Common\Filter; | ||
|
|
||
| use ApiPlatform\Metadata\Parameter; | ||
| use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; | ||
|
|
||
| /** | ||
| * @author Vincent Amstoutz <[email protected]> | ||
| */ | ||
| trait OpenApiFilterTrait | ||
| { | ||
| public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null | ||
| { | ||
| return new OpenApiParameter(name: $parameter->getKey().'[]', in: 'query', style: 'deepObject', explode: true); | ||
| } | ||
| } | ||
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,86 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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\ManagerRegistryAwareInterface; | ||
| use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareTrait; | ||
| use ApiPlatform\Doctrine\Common\Filter\OpenApiFilterTrait; | ||
| use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait; | ||
| use ApiPlatform\Metadata\OpenApiParameterFilterInterface; | ||
| use ApiPlatform\Metadata\Operation; | ||
| use Doctrine\ODM\MongoDB\Aggregation\Builder; | ||
| use Doctrine\ODM\MongoDB\DocumentManager; | ||
| use Doctrine\ODM\MongoDB\LockException; | ||
| use Doctrine\ODM\MongoDB\Mapping\MappingException; | ||
|
|
||
| /** | ||
| * @author Vincent Amstoutz <[email protected]> | ||
| */ | ||
| final class ExactFilter implements FilterInterface, OpenApiParameterFilterInterface, ManagerRegistryAwareInterface | ||
| { | ||
| use BackwardCompatibleFilterDescriptionTrait; | ||
| use ManagerRegistryAwareTrait; | ||
| use OpenApiFilterTrait; | ||
|
|
||
| /** | ||
| * @throws MappingException | ||
| * @throws LockException | ||
| */ | ||
| public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void | ||
| { | ||
| $parameter = $context['parameter']; | ||
| $property = $parameter->getProperty(); | ||
| $value = $parameter->getValue(); | ||
| $operator = $context['operator'] ?? 'addAnd'; | ||
| $match = $context['match'] = $context['match'] ?? | ||
| $aggregationBuilder | ||
| ->matchExpr(); | ||
|
|
||
| $documentManager = $this->getManagerRegistry()->getManagerForClass($resourceClass); | ||
| if (!$documentManager instanceof DocumentManager) { | ||
| return; | ||
| } | ||
|
|
||
| $classMetadata = $documentManager->getClassMetadata($resourceClass); | ||
|
|
||
| if (!$classMetadata->hasReference($property)) { | ||
| $match | ||
| ->{$operator}($aggregationBuilder->matchExpr()->field($property)->{is_iterable($value) ? 'in' : 'equals'}($value)); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $mapping = $classMetadata->getFieldMapping($property); | ||
| $method = $classMetadata->isSingleValuedAssociation($property) ? 'references' : 'includesReferenceTo'; | ||
|
|
||
| if (is_iterable($value)) { | ||
| $or = $aggregationBuilder->matchExpr(); | ||
|
|
||
| foreach ($value as $v) { | ||
| $or->addOr($aggregationBuilder->matchExpr()->field($property)->{$method}($documentManager->getPartialReference($mapping['targetDocument'], $v))); | ||
| } | ||
|
|
||
| $match->{$operator}($or); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $match | ||
| ->{$operator}( | ||
| $aggregationBuilder->matchExpr() | ||
| ->field($property) | ||
| ->{$method}($documentManager->getPartialReference($mapping['targetDocument'], $value)) | ||
| ); | ||
| } | ||
| } |
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,62 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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\Metadata\BackwardCompatibleFilterDescriptionTrait; | ||
| use ApiPlatform\Metadata\Operation; | ||
| use Doctrine\ODM\MongoDB\Aggregation\Builder; | ||
|
|
||
| final class FreeTextQueryFilter implements FilterInterface, ManagerRegistryAwareInterface, LoggerAwareInterface | ||
| { | ||
| use BackwardCompatibleFilterDescriptionTrait; | ||
| use LoggerAwareTrait; | ||
| use ManagerRegistryAwareTrait; | ||
|
|
||
| /** | ||
| * @param list<string> $properties an array of properties, defaults to `parameter->getProperties()` | ||
| */ | ||
| public function __construct(private readonly FilterInterface $filter, private readonly ?array $properties = null) | ||
| { | ||
| } | ||
|
|
||
| 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']; | ||
| foreach ($this->properties ?? $parameter->getProperties() ?? [] as $property) { | ||
| $newContext = ['parameter' => $parameter->withProperty($property), 'match' => $context['match'] ?? $aggregationBuilder->match()->expr()] + $context; | ||
| $this->filter->apply( | ||
| $aggregationBuilder, | ||
| $resourceClass, | ||
| $operation, | ||
| $newContext, | ||
| ); | ||
|
|
||
| if (isset($newContext['match'])) { | ||
| $context['match'] = $newContext['match']; | ||
| } | ||
| } | ||
| } | ||
| } |
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.