Skip to content

Commit

Permalink
feat(doctrine): doctrine filters like laravel eloquent filters
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceAmstoutz committed Nov 12, 2024
1 parent b458b55 commit aef306c
Show file tree
Hide file tree
Showing 39 changed files with 1,724 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/Doctrine/Common/Filter/BooleanFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getDescription(string $resourceClass): array
return $description;
}

abstract protected function getProperties(): ?array;
abstract public function getProperties(): ?array;

abstract protected function getLogger(): LoggerInterface;

Expand Down
25 changes: 25 additions & 0 deletions src/Doctrine/Common/Filter/ManagerRegistryAwareInterface.php
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;
}
47 changes: 36 additions & 11 deletions src/Doctrine/Odm/Extension/ParameterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

namespace ApiPlatform\Doctrine\Odm\Extension;

use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
use ApiPlatform\Doctrine\Common\ParameterValueExtractorTrait;
use ApiPlatform\Doctrine\Odm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Odm\Filter\FilterInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ParameterNotFound;
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Psr\Container\ContainerInterface;

Expand All @@ -29,14 +32,20 @@ final class ParameterExtension implements AggregationCollectionExtensionInterfac
{
use ParameterValueExtractorTrait;

public function __construct(private readonly ContainerInterface $filterLocator)
{
public function __construct(
private readonly ContainerInterface $filterLocator,
private readonly ?ManagerRegistry $managerRegistry = null,
) {
}

/**
* @param array<string, mixed> $context
*/
private function applyFilter(Builder $aggregationBuilder, ?string $resourceClass = null, ?Operation $operation = null, array &$context = []): void
{
foreach ($operation->getParameters() ?? [] as $parameter) {
if (!($v = $parameter->getValue()) || $v instanceof ParameterNotFound) {
// TODO: remove the null equality as a parameter can have a null value
if (null === ($v = $parameter->getValue()) || $v instanceof ParameterNotFound) {
continue;
}

Expand All @@ -45,14 +54,30 @@ private function applyFilter(Builder $aggregationBuilder, ?string $resourceClass
continue;
}

$filter = $this->filterLocator->has($filterId) ? $this->filterLocator->get($filterId) : null;
if ($filter instanceof FilterInterface) {
$filterContext = ['filters' => $values, 'parameter' => $parameter];
$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'];
}
$filter = match (true) {
$filterId instanceof FilterInterface => $filterId,
\is_string($filterId) && $this->filterLocator->has($filterId) => $this->filterLocator->get($filterId),
default => null,
};

if (!$filter instanceof FilterInterface) {
continue;
}

if ($filter instanceof ManagerRegistryAwareInterface && !$filter->hasManagerRegistry()) {
$filter->setManagerRegistry($this->managerRegistry);
}

if ($filter instanceof AbstractFilter && !$filter->getProperties()) {
$propertyKey = $parameter->getProperty() ?? $parameter->getKey();
$filter->setProperties([$propertyKey => $parameter->getFilterContext()]);
}

$filterContext = ['filters' => $values, 'parameter' => $parameter];
$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'];
}
}
}
Expand Down
35 changes: 29 additions & 6 deletions src/Doctrine/Odm/Filter/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand All @@ -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
{
Expand Down
12 changes: 11 additions & 1 deletion src/Doctrine/Odm/Filter/BooleanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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'];
}
}
28 changes: 27 additions & 1 deletion src/Doctrine/Odm/Filter/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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),
];
}
}
35 changes: 33 additions & 2 deletions src/Doctrine/Odm/Filter/ExistsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@

use ApiPlatform\Doctrine\Common\Filter\ExistsFilterInterface;
use ApiPlatform\Doctrine\Common\Filter\ExistsFilterTrait;
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\Mapping\ClassMetadata;
use Doctrine\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -107,12 +112,16 @@
* @author Teoh Han Hui <[email protected]>
* @author Alan Poulain <[email protected]>
*/
final class ExistsFilter extends AbstractFilter implements ExistsFilterInterface
final class ExistsFilter extends AbstractFilter implements ExistsFilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface
{
use ExistsFilterTrait;

public function __construct(ManagerRegistry $managerRegistry, ?LoggerInterface $logger = null, ?array $properties = null, string $existsParameterName = self::QUERY_PARAMETER_KEY, ?NameConverterInterface $nameConverter = null)
public function __construct(?ManagerRegistry $managerRegistry = null, ?LoggerInterface $logger = null, ?array $properties = null, string $existsParameterName = self::QUERY_PARAMETER_KEY, ?NameConverterInterface $nameConverter = null)
{
if (\is_array($properties) && \is_int(key($properties))) {
$properties = array_flip($properties);
}

parent::__construct($managerRegistry, $logger, $properties, $nameConverter);

$this->existsParameterName = $existsParameterName;
Expand All @@ -123,6 +132,12 @@ public function __construct(ManagerRegistry $managerRegistry, ?LoggerInterface $
*/
public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
{
$parameter = $context['parameter'] ?? null;

if (null !== ($value = $context['filters'][$parameter?->getProperty()] ?? null)) {
$this->filterProperty($this->denormalizePropertyName($parameter->getProperty()), $value, $aggregationBuilder, $resourceClass, $operation, $context);
}

foreach ($context['filters'][$this->existsParameterName] ?? [] as $property => $value) {
$this->filterProperty($this->denormalizePropertyName($property), $value, $aggregationBuilder, $resourceClass, $operation, $context);
}
Expand Down Expand Up @@ -167,4 +182,20 @@ protected function isNullableField(string $property, string $resourceClass): boo

return $metadata instanceof ClassMetadata && $metadata->hasField($field) ? $metadata->isNullable($field) : false;
}

public function getSchema(Parameter $parameter): array
{
return ['type' => 'boolean'];
}

public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null
{
$property = $parameter->getProperty();
$in = $parameter instanceof QueryParameter ? 'query' : 'header';

return new OpenApiParameter(
name: "exists[$property]",
in: $in
);
}
}
9 changes: 8 additions & 1 deletion src/Doctrine/Odm/Filter/NumericFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
namespace ApiPlatform\Doctrine\Odm\Filter;

use ApiPlatform\Doctrine\Common\Filter\NumericFilterTrait;
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;

Expand Down Expand Up @@ -104,7 +106,7 @@
* @author Teoh Han Hui <[email protected]>
* @author Alan Poulain <[email protected]>
*/
final class NumericFilter extends AbstractFilter
final class NumericFilter extends AbstractFilter implements JsonSchemaFilterInterface
{
use NumericFilterTrait;

Expand Down Expand Up @@ -163,4 +165,9 @@ protected function getType(?string $doctrineType = null): string

return 'int';
}

public function getSchema(Parameter $parameter): array
{
return ['type' => 'numeric'];
}
}
19 changes: 18 additions & 1 deletion src/Doctrine/Odm/Filter/RangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

use ApiPlatform\Doctrine\Common\Filter\RangeFilterInterface;
use ApiPlatform\Doctrine\Common\Filter\RangeFilterTrait;
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;

/**
Expand Down Expand Up @@ -104,7 +108,7 @@
* @author Lee Siong Chan <[email protected]>
* @author Alan Poulain <[email protected]>
*/
final class RangeFilter extends AbstractFilter implements RangeFilterInterface
final class RangeFilter extends AbstractFilter implements RangeFilterInterface, OpenApiParameterFilterInterface
{
use RangeFilterTrait;

Expand Down Expand Up @@ -204,4 +208,17 @@ protected function addMatch(Builder $aggregationBuilder, string $field, string $
break;
}
}

public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null
{
$in = $parameter instanceof QueryParameter ? 'query' : 'header';
$key = $parameter->getKey();

return [
new OpenApiParameter(name: $key.'[gt]', in: $in),
new OpenApiParameter(name: $key.'[lt]', in: $in),
new OpenApiParameter(name: $key.'[gte]', in: $in),
new OpenApiParameter(name: $key.'[lte]', in: $in),
];
}
}
Loading

0 comments on commit aef306c

Please sign in to comment.