-
-
Notifications
You must be signed in to change notification settings - Fork 928
feat(doctrine): doctrine filters like laravel eloquent filters #6775
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 4 commits into
api-platform:main
from
vinceAmstoutz:feat-doctrine-filters-like-laravel-filters
Dec 10, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
494c1bf
feat(doctrine): doctrine filters like laravel eloquent filters
vinceAmstoutz d9bc540
fix: allow multiple validation with :property placeholder
soyuka 3dde14a
fix: correct escape filter condition
vinceAmstoutz d1ffc14
fix: remove duplicated block
vinceAmstoutz 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
25 changes: 25 additions & 0 deletions
25
src/Doctrine/Common/Filter/ManagerRegistryAwareInterface.php
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 Doctrine\Persistence\ManagerRegistry; | ||
|
||
interface ManagerRegistryAwareInterface | ||
{ | ||
public function hasManagerRegistry(): bool; | ||
|
||
public function getManagerRegistry(): ManagerRegistry; | ||
|
||
public function setManagerRegistry(ManagerRegistry $managerRegistry): void; | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Doctrine/Common/Filter/PropertyPlaceholderOpenApiParameterTrait.php
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,38 @@ | ||
<?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; | ||
|
||
trait PropertyPlaceholderOpenApiParameterTrait | ||
{ | ||
/** | ||
* @return array<OpenApiParameter>|null | ||
*/ | ||
public function getOpenApiParameters(Parameter $parameter): ?array | ||
{ | ||
if (str_contains($parameter->getKey(), ':property')) { | ||
$parameters = []; | ||
$key = str_replace('[:property]', '', $parameter->getKey()); | ||
foreach (array_keys($parameter->getExtraProperties()['_properties'] ?? []) as $property) { | ||
$parameters[] = new OpenApiParameter(name: \sprintf('%s[%s]', $key, $property), in: 'query'); | ||
} | ||
|
||
return $parameters; | ||
} | ||
|
||
return null; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -13,9 +13,11 @@ | |
|
||
namespace ApiPlatform\Doctrine\Odm\Filter; | ||
|
||
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface; | ||
use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface; | ||
use ApiPlatform\Doctrine\Common\PropertyHelperTrait; | ||
use ApiPlatform\Doctrine\Odm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait; | ||
use ApiPlatform\Metadata\Exception\RuntimeException; | ||
use ApiPlatform\Metadata\Operation; | ||
use Doctrine\ODM\MongoDB\Aggregation\Builder; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
|
@@ -30,14 +32,18 @@ | |
* | ||
* @author Alan Poulain <[email protected]> | ||
*/ | ||
abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface | ||
abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface, ManagerRegistryAwareInterface | ||
{ | ||
use MongoDbOdmPropertyHelperTrait; | ||
use PropertyHelperTrait; | ||
protected LoggerInterface $logger; | ||
|
||
public function __construct(protected ManagerRegistry $managerRegistry, ?LoggerInterface $logger = null, protected ?array $properties = null, protected ?NameConverterInterface $nameConverter = null) | ||
{ | ||
public function __construct( | ||
protected ?ManagerRegistry $managerRegistry = null, | ||
?LoggerInterface $logger = null, | ||
protected ?array $properties = null, | ||
protected ?NameConverterInterface $nameConverter = null, | ||
) { | ||
$this->logger = $logger ?? new NullLogger(); | ||
} | ||
|
||
|
@@ -56,18 +62,35 @@ public function apply(Builder $aggregationBuilder, string $resourceClass, ?Opera | |
*/ | ||
abstract protected function filterProperty(string $property, $value, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void; | ||
|
||
protected function getManagerRegistry(): ManagerRegistry | ||
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; | ||
} | ||
|
||
protected function getProperties(): ?array | ||
public function setManagerRegistry(ManagerRegistry $managerRegistry): void | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed>|null | ||
*/ | ||
public function getProperties(): ?array | ||
{ | ||
return $this->properties; | ||
} | ||
|
||
/** | ||
* @param string[] $properties | ||
* @param array<string, mixed> $properties | ||
*/ | ||
public function setProperties(array $properties): 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 |
---|---|---|
|
@@ -14,7 +14,9 @@ | |
namespace ApiPlatform\Doctrine\Odm\Filter; | ||
|
||
use ApiPlatform\Doctrine\Common\Filter\BooleanFilterTrait; | ||
use ApiPlatform\Metadata\JsonSchemaFilterInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Metadata\Parameter; | ||
use Doctrine\ODM\MongoDB\Aggregation\Builder; | ||
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType; | ||
|
||
|
@@ -104,7 +106,7 @@ | |
* @author Teoh Han Hui <[email protected]> | ||
* @author Alan Poulain <[email protected]> | ||
*/ | ||
final class BooleanFilter extends AbstractFilter | ||
final class BooleanFilter extends AbstractFilter implements JsonSchemaFilterInterface | ||
{ | ||
use BooleanFilterTrait; | ||
|
||
|
@@ -139,4 +141,12 @@ protected function filterProperty(string $property, $value, Builder $aggregation | |
|
||
$aggregationBuilder->match()->field($matchField)->equals($value); | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
public function getSchema(Parameter $parameter): array | ||
{ | ||
return ['type' => 'boolean']; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -16,7 +16,12 @@ | |
use ApiPlatform\Doctrine\Common\Filter\DateFilterInterface; | ||
use ApiPlatform\Doctrine\Common\Filter\DateFilterTrait; | ||
use ApiPlatform\Metadata\Exception\InvalidArgumentException; | ||
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; | ||
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType; | ||
|
||
|
@@ -117,7 +122,7 @@ | |
* @author Théo FIDRY <[email protected]> | ||
* @author Alan Poulain <[email protected]> | ||
*/ | ||
final class DateFilter extends AbstractFilter implements DateFilterInterface | ||
final class DateFilter extends AbstractFilter implements DateFilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface | ||
{ | ||
use DateFilterTrait; | ||
|
||
|
@@ -129,11 +134,11 @@ final class DateFilter extends AbstractFilter implements DateFilterInterface | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function filterProperty(string $property, $values, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void | ||
protected function filterProperty(string $property, $value, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void | ||
{ | ||
// Expect $values to be an array having the period as keys and the date value as values | ||
// Expect $value to be an array having the period as keys and the date value as values | ||
if ( | ||
!\is_array($values) | ||
!\is_array($value) | ||
|| !$this->isPropertyEnabled($property, $resourceClass) | ||
|| !$this->isPropertyMapped($property, $resourceClass) | ||
|| !$this->isDateField($property, $resourceClass) | ||
|
@@ -153,42 +158,42 @@ protected function filterProperty(string $property, $values, Builder $aggregatio | |
$aggregationBuilder->match()->field($matchField)->notEqual(null); | ||
} | ||
|
||
if (isset($values[self::PARAMETER_BEFORE])) { | ||
if (isset($value[self::PARAMETER_BEFORE])) { | ||
$this->addMatch( | ||
$aggregationBuilder, | ||
$matchField, | ||
self::PARAMETER_BEFORE, | ||
$values[self::PARAMETER_BEFORE], | ||
$value[self::PARAMETER_BEFORE], | ||
$nullManagement | ||
); | ||
} | ||
|
||
if (isset($values[self::PARAMETER_STRICTLY_BEFORE])) { | ||
if (isset($value[self::PARAMETER_STRICTLY_BEFORE])) { | ||
$this->addMatch( | ||
$aggregationBuilder, | ||
$matchField, | ||
self::PARAMETER_STRICTLY_BEFORE, | ||
$values[self::PARAMETER_STRICTLY_BEFORE], | ||
$value[self::PARAMETER_STRICTLY_BEFORE], | ||
$nullManagement | ||
); | ||
} | ||
|
||
if (isset($values[self::PARAMETER_AFTER])) { | ||
if (isset($value[self::PARAMETER_AFTER])) { | ||
$this->addMatch( | ||
$aggregationBuilder, | ||
$matchField, | ||
self::PARAMETER_AFTER, | ||
$values[self::PARAMETER_AFTER], | ||
$value[self::PARAMETER_AFTER], | ||
$nullManagement | ||
); | ||
} | ||
|
||
if (isset($values[self::PARAMETER_STRICTLY_AFTER])) { | ||
if (isset($value[self::PARAMETER_STRICTLY_AFTER])) { | ||
$this->addMatch( | ||
$aggregationBuilder, | ||
$matchField, | ||
self::PARAMETER_STRICTLY_AFTER, | ||
$values[self::PARAMETER_STRICTLY_AFTER], | ||
$value[self::PARAMETER_STRICTLY_AFTER], | ||
$nullManagement | ||
); | ||
} | ||
|
@@ -237,4 +242,25 @@ private function addMatch(Builder $aggregationBuilder, string $field, string $op | |
|
||
$aggregationBuilder->match()->addAnd($aggregationBuilder->matchExpr()->field($field)->operator($operatorValue[$operator], $value)); | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
public function getSchema(Parameter $parameter): array | ||
{ | ||
return ['type' => 'date']; | ||
} | ||
|
||
public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null | ||
{ | ||
$in = $parameter instanceof QueryParameter ? 'query' : 'header'; | ||
$key = $parameter->getKey(); | ||
|
||
return [ | ||
new OpenApiParameter(name: $key.'[after]', in: $in), | ||
new OpenApiParameter(name: $key.'[before]', in: $in), | ||
new OpenApiParameter(name: $key.'[strictly_after]', in: $in), | ||
new OpenApiParameter(name: $key.'[strictly_before]', in: $in), | ||
]; | ||
} | ||
} |
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.