Skip to content

Commit 7144d25

Browse files
committed
feat(doctrine): boolean filter like laravel filters
1 parent 1ac0c3d commit 7144d25

File tree

8 files changed

+351
-12
lines changed

8 files changed

+351
-12
lines changed

src/Doctrine/Orm/Extension/ParameterExtension.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
namespace ApiPlatform\Doctrine\Orm\Extension;
1515

1616
use ApiPlatform\Doctrine\Common\ParameterValueExtractorTrait;
17+
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
1718
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
1819
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
1920
use ApiPlatform\Metadata\Operation;
2021
use ApiPlatform\State\ParameterNotFound;
2122
use Doctrine\ORM\QueryBuilder;
23+
use Psr\Container\ContainerExceptionInterface;
2224
use Psr\Container\ContainerInterface;
25+
use Psr\Container\NotFoundExceptionInterface;
26+
use Symfony\Bridge\Doctrine\ManagerRegistry;
2327

2428
/**
2529
* Reads operation parameters and execute its filter.
@@ -30,17 +34,22 @@ final class ParameterExtension implements QueryCollectionExtensionInterface, Que
3034
{
3135
use ParameterValueExtractorTrait;
3236

33-
public function __construct(private readonly ContainerInterface $filterLocator)
34-
{
37+
public function __construct(
38+
private readonly ContainerInterface $filterLocator,
39+
private readonly ?ManagerRegistry $managerRegistry = null,
40+
) {
3541
}
3642

3743
/**
3844
* @param array<string, mixed> $context
45+
*
46+
* @throws ContainerExceptionInterface
47+
* @throws NotFoundExceptionInterface
3948
*/
4049
private function applyFilter(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
4150
{
4251
foreach ($operation?->getParameters() ?? [] as $parameter) {
43-
if (!($v = $parameter->getValue()) || $v instanceof ParameterNotFound) {
52+
if (null === ($v = $parameter->getValue()) || $v instanceof ParameterNotFound) {
4453
continue;
4554
}
4655

@@ -49,9 +58,12 @@ private function applyFilter(QueryBuilder $queryBuilder, QueryNameGeneratorInter
4958
continue;
5059
}
5160

52-
$filter = $this->filterLocator->has($filterId) ? $this->filterLocator->get($filterId) : null;
61+
$filter = $this->getFilter($filterId, $values);
62+
5363
if ($filter instanceof FilterInterface) {
54-
$filter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, ['filters' => $values, 'parameter' => $parameter] + $context);
64+
$filter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation,
65+
['filters' => $values, 'parameter' => $parameter] + $context
66+
);
5567
}
5668
}
5769
}
@@ -71,4 +83,24 @@ public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterf
7183
{
7284
$this->applyFilter($queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
7385
}
86+
87+
/**
88+
* @throws ContainerExceptionInterface
89+
* @throws NotFoundExceptionInterface
90+
*/
91+
private function getFilter($filterId, array $values = []): ?FilterInterface
92+
{
93+
if ($filterId instanceof AbstractFilter) {
94+
$filterId->setManagerRegistry($this->managerRegistry);
95+
$filterId->setProperties($values);
96+
97+
return $filterId;
98+
}
99+
100+
if (\is_string($filterId) && $this->filterLocator->has($filterId)) {
101+
return $this->filterLocator->get($filterId);
102+
}
103+
104+
return null;
105+
}
74106
}

src/Doctrine/Orm/Filter/AbstractFilter.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInt
3030
use PropertyHelperTrait;
3131
protected LoggerInterface $logger;
3232

33-
public function __construct(protected ManagerRegistry $managerRegistry, ?LoggerInterface $logger = null, protected ?array $properties = null, protected ?NameConverterInterface $nameConverter = null)
34-
{
33+
public function __construct(
34+
protected ?ManagerRegistry $managerRegistry = null,
35+
?LoggerInterface $logger = null,
36+
protected ?array $properties = null,
37+
protected ?NameConverterInterface $nameConverter = null,
38+
) {
3539
$this->logger = $logger ?? new NullLogger();
3640
}
3741

@@ -53,12 +57,17 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
5357
*/
5458
abstract protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void;
5559

56-
protected function getManagerRegistry(): ManagerRegistry
60+
protected function getManagerRegistry(): ?ManagerRegistry
5761
{
5862
return $this->managerRegistry;
5963
}
6064

61-
protected function getProperties(): ?array
65+
public function setManagerRegistry(?ManagerRegistry $managerRegistry): ?ManagerRegistry
66+
{
67+
return $this->managerRegistry = $managerRegistry;
68+
}
69+
70+
public function getProperties(): ?array
6271
{
6372
return $this->properties;
6473
}

src/Doctrine/Orm/Filter/BooleanFilter.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515

1616
use ApiPlatform\Doctrine\Common\Filter\BooleanFilterTrait;
1717
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18+
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
19+
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
1820
use ApiPlatform\Metadata\Operation;
21+
use ApiPlatform\Metadata\Parameter;
22+
use ApiPlatform\Metadata\QueryParameter;
23+
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
1924
use Doctrine\DBAL\Types\Types;
2025
use Doctrine\ORM\Query\Expr\Join;
2126
use Doctrine\ORM\QueryBuilder;
@@ -106,7 +111,7 @@
106111
* @author Amrouche Hamza <[email protected]>
107112
* @author Teoh Han Hui <[email protected]>
108113
*/
109-
final class BooleanFilter extends AbstractFilter
114+
final class BooleanFilter extends AbstractFilter implements OpenApiParameterFilterInterface, JsonSchemaFilterInterface
110115
{
111116
use BooleanFilterTrait;
112117

@@ -145,4 +150,27 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
145150
->andWhere(\sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
146151
->setParameter($valueParameter, $value);
147152
}
153+
154+
/**
155+
* @return array<string, mixed>
156+
*/
157+
public function getSchema(Parameter $parameter): array
158+
{
159+
return ['type' => 'boolean'];
160+
}
161+
162+
public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null
163+
{
164+
$in = $parameter instanceof QueryParameter ? 'query' : 'header';
165+
$key = $parameter->getKey();
166+
167+
return [
168+
new OpenApiParameter(
169+
name: $key,
170+
in: $in,
171+
required: false,
172+
schema: ['type' => 'boolean'],
173+
),
174+
];
175+
}
148176
}

src/Doctrine/Orm/PropertyHelperTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
trait PropertyHelperTrait
3131
{
32-
abstract protected function getManagerRegistry(): ManagerRegistry;
32+
abstract protected function getManagerRegistry(): ?ManagerRegistry;
3333

3434
/**
3535
* Splits the given property into parts.
@@ -43,7 +43,7 @@ protected function getClassMetadata(string $resourceClass): ClassMetadata
4343
{
4444
$manager = $this
4545
->getManagerRegistry()
46-
->getManagerForClass($resourceClass);
46+
?->getManagerForClass($resourceClass);
4747

4848
if ($manager) {
4949
return $manager->getClassMetadata($resourceClass);

src/Symfony/Bundle/Resources/config/doctrine_orm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150

151151
<service id="api_platform.doctrine.orm.extension.parameter_extension" class="ApiPlatform\Doctrine\Orm\Extension\ParameterExtension" public="false">
152152
<argument type="service" id="api_platform.filter_locator" />
153+
<argument type="service" id="doctrine" on-invalid="null"/>
153154
<tag name="api_platform.doctrine.orm.query_extension.collection" priority="-16" />
154155
<tag name="api_platform.doctrine.orm.query_extension.item" priority="-9" />
155156
</service>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Document;
15+
16+
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Metadata\QueryParameter;
20+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
21+
22+
#[ApiResource]
23+
#[GetCollection(
24+
filters: [
25+
'active' => new QueryParameter(
26+
filter: new BooleanFilter(),
27+
),
28+
'enabled' => new QueryParameter(
29+
filter: new BooleanFilter(),
30+
property: 'active',
31+
),
32+
],
33+
)]
34+
#[ODM\Document]
35+
class FilteredBooleanParameter
36+
{
37+
public function __construct(
38+
#[ODM\Id(type: 'int', strategy: 'INCREMENT')]
39+
public ?int $id = null,
40+
41+
#[ODM\Field(type: 'bool', nullable: true)]
42+
public ?bool $active = null,
43+
) {
44+
}
45+
46+
public function getId(): ?int
47+
{
48+
return $this->id;
49+
}
50+
51+
public function isActive(): bool
52+
{
53+
return $this->active;
54+
}
55+
56+
public function setActive(?bool $active): void
57+
{
58+
$this->active = $active;
59+
}
60+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Metadata\QueryParameter;
20+
use Doctrine\ORM\Mapping as ORM;
21+
22+
#[ApiResource]
23+
#[GetCollection(
24+
parameters: [
25+
'active' => new QueryParameter(
26+
filter: new BooleanFilter(),
27+
),
28+
'enabled' => new QueryParameter(
29+
filter: new BooleanFilter(),
30+
property: 'active',
31+
),
32+
],
33+
)]
34+
#[ORM\Entity]
35+
class FilteredBooleanParameter
36+
{
37+
public function __construct(
38+
#[ORM\Column]
39+
#[ORM\Id]
40+
#[ORM\GeneratedValue(strategy: 'AUTO')]
41+
public ?int $id = null,
42+
43+
#[ORM\Column(nullable: true)]
44+
public ?bool $active = null,
45+
) {
46+
}
47+
48+
public function getId(): ?int
49+
{
50+
return $this->id;
51+
}
52+
53+
public function isActive(): bool
54+
{
55+
return $this->active;
56+
}
57+
58+
public function setActive(?bool $isActive): void
59+
{
60+
$this->active = $isActive;
61+
}
62+
}

0 commit comments

Comments
 (0)