Skip to content

Commit 09c0715

Browse files
committed
Add a deprecation message when the old syntax is used
(and consider the feedback: order of the constructor parameters, final class, keep constant, ...)
1 parent 8c3eec9 commit 09c0715

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

src/Bridge/Doctrine/Orm/Filter/ExistsFilter.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,28 @@
3434
* Request: GET /products?brand[exists]
3535
* Interpretation: filter products which have a brand
3636
*
37+
* @author Teoh Han Hui <teohhanhui@gmail.com>
3738
* @author Julien Verger <julien.verger@gmail.com>
3839
*/
3940
class ExistsFilter extends AbstractContextAwareFilter
4041
{
42+
const QUERY_PARAMETER_KEY = 'exists';
43+
4144
/**
4245
* @var string Keyword used to retrieve the value
4346
*/
44-
protected $existsParameterName;
47+
private $existsParameterName;
4548

4649
/**
4750
* @param RequestStack|null $requestStack No prefix to prevent autowiring of this deprecated property
4851
*/
49-
public function __construct(ManagerRegistry $managerRegistry, $requestStack = null, string $existsParameterName = 'exists', LoggerInterface $logger = null, array $properties = null)
50-
{
52+
public function __construct(
53+
ManagerRegistry $managerRegistry,
54+
$requestStack = null,
55+
LoggerInterface $logger = null,
56+
array $properties = null,
57+
string $existsParameterName = self::QUERY_PARAMETER_KEY
58+
) {
5159
parent::__construct($managerRegistry, $requestStack, $logger, $properties);
5260

5361
$this->existsParameterName = $existsParameterName;
@@ -85,11 +93,7 @@ public function getDescription(string $resourceClass): array
8593
*/
8694
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = [])
8795
{
88-
if (isset($context['filters']) && !isset($context['filters'][$this->existsParameterName])) {
89-
return;
90-
}
91-
92-
if (!isset($context['filters'][$this->existsParameterName]) || !\is_array($context['filters'][$this->existsParameterName])) {
96+
if (!\is_array($context['filters'][$this->existsParameterName] ?? null)) {
9397
parent::apply($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);
9498

9599
return;
@@ -113,6 +117,14 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
113117
return;
114118
}
115119

120+
if (\is_array($value) && isset($value[self::QUERY_PARAMETER_KEY])) {
121+
@trigger_error(
122+
sprintf('The ExistsFilter syntax "%s[exists]=true/false" is deprecated. Use the syntax "exists[%s]=true/false" instead.', $property, $property),
123+
E_USER_DEPRECATED
124+
);
125+
$value = $value['exists'];
126+
}
127+
116128
if (\in_array($value, [true, 'true', '1', '', null], true)) {
117129
$value = true;
118130
} elseif (\in_array($value, [false, 'false', '0'], true)) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@
9191
<service id="api_platform.doctrine.orm.exists_filter" class="ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter" public="false" abstract="true">
9292
<argument type="service" id="doctrine" />
9393
<argument>null</argument>
94-
<argument>%api_platform.collection.notnull_parameter_name%</argument>
9594
<argument type="service" id="logger" on-invalid="ignore" />
95+
<argument>null</argument>
96+
<argument>%api_platform.collection.notnull_parameter_name%</argument>
9697
</service>
9798

9899
<!-- Metadata loader -->

tests/Bridge/Doctrine/Orm/Filter/ExistsFilterTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ExistsFilterTest extends DoctrineOrmFilterTestCase
2828

2929
public function testGetDescription()
3030
{
31-
$filter = new ExistsFilter($this->managerRegistry, null, 'exists', null, ['name' => null, 'description' => null]);
31+
$filter = new ExistsFilter($this->managerRegistry, null, null, ['name' => null, 'description' => null]);
3232

3333
$this->assertEquals([
3434
'exists[description]' => [
@@ -39,6 +39,19 @@ public function testGetDescription()
3939
], $filter->getDescription($this->resourceClass));
4040
}
4141

42+
public function testGetDescriptionWithCustomExistsParameterName()
43+
{
44+
$filter = new ExistsFilter($this->managerRegistry, null, null, ['name' => null, 'description' => null], 'notnull');
45+
46+
$this->assertEquals([
47+
'notnull[description]' => [
48+
'property' => 'description',
49+
'type' => 'bool',
50+
'required' => false,
51+
],
52+
], $filter->getDescription($this->resourceClass));
53+
}
54+
4255
public function testGetDescriptionDefaultFields()
4356
{
4457
$filter = new ExistsFilter($this->managerRegistry);
@@ -100,10 +113,10 @@ public function testGetDescriptionDefaultFields()
100113
public function provideApplyTestData(): array
101114
{
102115
$existsFilterFactory = function (ManagerRegistry $managerRegistry, RequestStack $requestStack = null, array $properties = null): ExistsFilter {
103-
return new ExistsFilter($managerRegistry, $requestStack, 'exists', null, $properties);
116+
return new ExistsFilter($managerRegistry, $requestStack, null, $properties, 'exists');
104117
};
105118
$customExistsFilterFactory = function (ManagerRegistry $managerRegistry, RequestStack $requestStack = null, array $properties = null): ExistsFilter {
106-
return new ExistsFilter($managerRegistry, $requestStack, 'customExists', null, $properties);
119+
return new ExistsFilter($managerRegistry, $requestStack, null, $properties, 'customExists');
107120
};
108121

109122
return [

0 commit comments

Comments
 (0)