-
-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(doctrine): parameter filter extension (#6248)
* feat(doctrine): parameter filtering * feat(graphql): parameter graphql arguments
- Loading branch information
Showing
19 changed files
with
866 additions
and
19 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/Doctrine/Common/Filter/PropertyAwareFilterInterface.php
This file contains 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,27 @@ | ||
<?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; | ||
|
||
/** | ||
* @author Antoine Bluchet <[email protected]> | ||
* | ||
* @experimental | ||
*/ | ||
interface PropertyAwareFilterInterface | ||
{ | ||
/** | ||
* @param string[] $properties | ||
*/ | ||
public function setProperties(array $properties): void; | ||
} |
This file contains 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,71 @@ | ||
<?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\Extension; | ||
|
||
use ApiPlatform\Doctrine\Odm\Filter\FilterInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use Doctrine\ODM\MongoDB\Aggregation\Builder; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* Reads operation parameters and execute its filter. | ||
* | ||
* @author Antoine Bluchet <[email protected]> | ||
*/ | ||
final class ParameterExtension implements AggregationCollectionExtensionInterface, AggregationItemExtensionInterface | ||
{ | ||
public function __construct(private readonly ContainerInterface $filterLocator) | ||
{ | ||
} | ||
|
||
private function applyFilter(Builder $aggregationBuilder, ?string $resourceClass = null, ?Operation $operation = null, array &$context = []): void | ||
{ | ||
foreach ($operation->getParameters() ?? [] as $parameter) { | ||
$values = $parameter->getExtraProperties()['_api_values'] ?? []; | ||
if (!$values) { | ||
continue; | ||
} | ||
|
||
if (null === ($filterId = $parameter->getFilter())) { | ||
continue; | ||
} | ||
|
||
$filter = $this->filterLocator->has($filterId) ? $this->filterLocator->get($filterId) : null; | ||
if ($filter instanceof FilterInterface) { | ||
$filterContext = ['filters' => $values]; | ||
$filter->apply($aggregationBuilder, $resourceClass, $operation, $filterContext); | ||
// update by reference | ||
if (isset($filterContext['mongodb_odm_sort_fields'])) { | ||
$context['mongodb_odm_sort_fields'] = $filterContext['mongodb_odm_sort_fields']; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function applyToCollection(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void | ||
{ | ||
$this->applyFilter($aggregationBuilder, $resourceClass, $operation, $context); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function applyToItem(Builder $aggregationBuilder, string $resourceClass, array $identifiers, ?Operation $operation = null, array &$context = []): void | ||
{ | ||
$this->applyFilter($aggregationBuilder, $resourceClass, $operation, $context); | ||
} | ||
} |
This file contains 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,6 +13,7 @@ | |
|
||
namespace ApiPlatform\Doctrine\Odm\Filter; | ||
|
||
use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface; | ||
use ApiPlatform\Doctrine\Common\PropertyHelperTrait; | ||
use ApiPlatform\Doctrine\Odm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait; | ||
use ApiPlatform\Metadata\Operation; | ||
|
@@ -29,7 +30,7 @@ | |
* | ||
* @author Alan Poulain <[email protected]> | ||
*/ | ||
abstract class AbstractFilter implements FilterInterface | ||
abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface | ||
{ | ||
use MongoDbOdmPropertyHelperTrait; | ||
use PropertyHelperTrait; | ||
|
@@ -65,6 +66,14 @@ protected function getProperties(): ?array | |
return $this->properties; | ||
} | ||
|
||
/** | ||
* @param string[] $properties | ||
*/ | ||
public function setProperties(array $properties): void | ||
{ | ||
$this->properties = $properties; | ||
} | ||
|
||
protected function getLogger(): LoggerInterface | ||
{ | ||
return $this->logger; | ||
|
This file contains 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,70 @@ | ||
<?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\Orm\Extension; | ||
|
||
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface; | ||
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use Doctrine\ORM\QueryBuilder; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* Reads operation parameters and execute its filter. | ||
* | ||
* @author Antoine Bluchet <[email protected]> | ||
*/ | ||
final class ParameterExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface | ||
{ | ||
public function __construct(private readonly ContainerInterface $filterLocator) | ||
{ | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $context | ||
*/ | ||
private function applyFilter(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void | ||
{ | ||
foreach ($operation->getParameters() ?? [] as $parameter) { | ||
$values = $parameter->getExtraProperties()['_api_values'] ?? []; | ||
if (!$values) { | ||
continue; | ||
} | ||
|
||
if (null === ($filterId = $parameter->getFilter())) { | ||
continue; | ||
} | ||
|
||
$filter = $this->filterLocator->has($filterId) ? $this->filterLocator->get($filterId) : null; | ||
if ($filter instanceof FilterInterface) { | ||
$filter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, ['filters' => $values] + $context); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void | ||
{ | ||
$this->applyFilter($queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, ?Operation $operation = null, array $context = []): void | ||
{ | ||
$this->applyFilter($queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context); | ||
} | ||
} |
This file contains 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 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 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.