34
34
* Request: GET /products?brand[exists]
35
35
* Interpretation: filter products which have a brand
36
36
*
37
+ * @author Teoh Han Hui <teohhanhui@gmail.com>
37
38
* @author Julien Verger <julien.verger@gmail.com>
38
39
*/
39
40
class ExistsFilter extends AbstractContextAwareFilter
40
41
{
42
+ const QUERY_PARAMETER_KEY = 'exists ' ;
43
+
41
44
/**
42
45
* @var string Keyword used to retrieve the value
43
46
*/
44
- protected $ existsParameterName ;
47
+ private $ existsParameterName ;
45
48
46
49
/**
47
50
* @param RequestStack|null $requestStack No prefix to prevent autowiring of this deprecated property
48
51
*/
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
+ ) {
51
59
parent ::__construct ($ managerRegistry , $ requestStack , $ logger , $ properties );
52
60
53
61
$ this ->existsParameterName = $ existsParameterName ;
@@ -85,11 +93,7 @@ public function getDescription(string $resourceClass): array
85
93
*/
86
94
public function apply (QueryBuilder $ queryBuilder , QueryNameGeneratorInterface $ queryNameGenerator , string $ resourceClass , string $ operationName = null , array $ context = [])
87
95
{
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 )) {
93
97
parent ::apply ($ queryBuilder , $ queryNameGenerator , $ resourceClass , $ operationName , $ context );
94
98
95
99
return ;
@@ -113,6 +117,14 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
113
117
return ;
114
118
}
115
119
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
+
116
128
if (\in_array ($ value , [true , 'true ' , '1 ' , '' , null ], true )) {
117
129
$ value = true ;
118
130
} elseif (\in_array ($ value , [false , 'false ' , '0 ' ], true )) {
0 commit comments