-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoo.diff
More file actions
289 lines (277 loc) · 11.3 KB
/
foo.diff
File metadata and controls
289 lines (277 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
diff --git a/src/Doctrine/Common/Filter/ParameterAwareFilterInterface.php b/src/Doctrine/Common/Filter/ParameterAwareFilterInterface.php
new file mode 100644
index 00000000000..d1974a32c55
--- /dev/null
+++ b/src/Doctrine/Common/Filter/ParameterAwareFilterInterface.php
@@ -0,0 +1,31 @@
+<?php
+
+/*
+ * This file is part of the API Platform project.
+ *
+ * (c) Kévin Dunglas <dunglas@gmail.com>
+ *
+ * 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 ApiPlatform\Metadata\Operation;
+
+/**
+ * Interface for filters that can be applied by a ParameterExtension.
+ *
+ * @author Antoine Bluchet <soyuka@gmail.com>
+ */
+interface ParameterAwareFilterInterface
+{
+ /**
+ * Applies the filter to the query.
+ *
+ * @param array<string, mixed> $context
+ */
+ public function apply(object $queryBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void;
+}
diff --git a/src/Doctrine/Common/Filter/PropertyAwareFilterInterface.php b/src/Doctrine/Common/Filter/PropertyAwareFilterInterface.php
index aa0857cef20..1b041f480fb 100644
--- a/src/Doctrine/Common/Filter/PropertyAwareFilterInterface.php
+++ b/src/Doctrine/Common/Filter/PropertyAwareFilterInterface.php
@@ -16,6 +16,8 @@
/**
* @author Antoine Bluchet <soyuka@gmail.com>
*
+ * @method ?array getProperties()
+ *
* @experimental
*/
interface PropertyAwareFilterInterface
@@ -24,4 +26,9 @@ interface PropertyAwareFilterInterface
* @param string[] $properties
*/
public function setProperties(array $properties): void;
+
+ // /**
+ // * @return string[]
+ // */
+ // public function getProperties(): ?array;
}
diff --git a/src/Doctrine/Common/Filter/PropertyPlaceholderOpenApiParameterTrait.php b/src/Doctrine/Common/Filter/PropertyPlaceholderOpenApiParameterTrait.php
index f2f09e5758c..3acab18a05f 100644
--- a/src/Doctrine/Common/Filter/PropertyPlaceholderOpenApiParameterTrait.php
+++ b/src/Doctrine/Common/Filter/PropertyPlaceholderOpenApiParameterTrait.php
@@ -23,16 +23,6 @@ trait PropertyPlaceholderOpenApiParameterTrait
*/
public function getOpenApiParameters(Parameter $parameter): ?array
{
- if (str_contains($parameter->getKey(), ':property')) {
- $parameters = [];
- $key = str_replace('[:property]', '', $parameter->getKey());
- foreach (array_keys($parameter->getExtraProperties()['_properties'] ?? []) as $property) {
- $parameters[] = new OpenApiParameter(name: \sprintf('%s[%s]', $key, $property), in: 'query');
- }
-
- return $parameters;
- }
-
- return null;
+ return [new OpenApiParameter(name: $parameter->getKey(), in: 'query')];
}
}
diff --git a/src/Doctrine/Common/ParameterExtensionTrait.php b/src/Doctrine/Common/ParameterExtensionTrait.php
new file mode 100644
index 00000000000..4d865a8730f
--- /dev/null
+++ b/src/Doctrine/Common/ParameterExtensionTrait.php
@@ -0,0 +1,63 @@
+<?php
+
+/*
+ * This file is part of the API Platform project.
+ *
+ * (c) Kévin Dunglas <dunglas@gmail.com>
+ *
+ * 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;
+
+use ApiPlatform\Doctrine\Common\Filter\LoggerAwareInterface;
+use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
+use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface;
+use ApiPlatform\Metadata\Parameter;
+use Doctrine\Persistence\ManagerRegistry;
+use Psr\Container\ContainerInterface;
+use Psr\Log\LoggerInterface;
+
+trait ParameterExtensionTrait
+{
+ use ParameterValueExtractorTrait;
+
+ protected ContainerInterface $filterLocator;
+ protected ?ManagerRegistry $managerRegistry = null;
+ protected ?LoggerInterface $logger = null;
+
+ /**
+ * @param object $filter the filter instance to configure
+ * @param Parameter $parameter the operation parameter associated with the filter
+ */
+ private function configureFilter(object $filter, Parameter $parameter): void
+ {
+ if ($this->managerRegistry && $filter instanceof ManagerRegistryAwareInterface && !$filter->hasManagerRegistry()) {
+ $filter->setManagerRegistry($this->managerRegistry);
+ }
+
+ if ($this->logger && $filter instanceof LoggerAwareInterface && !$filter->hasLogger()) {
+ $filter->setLogger($this->logger);
+ }
+
+ if ($filter instanceof PropertyAwareFilterInterface) {
+ $properties = [];
+ // Check if the filter has getProperties method (e.g., if it's an AbstractFilter)
+ if (method_exists($filter, 'getProperties')) { // @phpstan-ignore-line todo 5.x remove this check @see interface
+ $properties = $filter->getProperties() ?? [];
+ }
+
+ $propertyKey = $parameter->getProperty() ?? $parameter->getKey();
+ foreach ($parameter->getProperties() ?? [$propertyKey] as $property) {
+ if (!isset($properties[$property])) {
+ $properties[$property] = $parameter->getFilterContext();
+ }
+ }
+
+ $filter->setProperties($properties);
+ }
+ }
+}
diff --git a/src/Doctrine/Common/ParameterValueExtractorTrait.php b/src/Doctrine/Common/ParameterValueExtractorTrait.php
index b50dede8f76..62c18f6483c 100644
--- a/src/Doctrine/Common/ParameterValueExtractorTrait.php
+++ b/src/Doctrine/Common/ParameterValueExtractorTrait.php
@@ -23,10 +23,7 @@ trait ParameterValueExtractorTrait
private function extractParameterValue(Parameter $parameter, mixed $value): array
{
$key = $parameter->getProperty() ?? $parameter->getKey();
- if (!str_contains($key, ':property')) {
- return [$key => $value];
- }
- return [str_replace('[:property]', '', $key) => $value];
+ return [$key => $value];
}
}
diff --git a/src/Doctrine/Odm/Extension/ParameterExtension.php b/src/Doctrine/Odm/Extension/ParameterExtension.php
index d68e6e9ed3b..d841fb9240e 100644
--- a/src/Doctrine/Odm/Extension/ParameterExtension.php
+++ b/src/Doctrine/Odm/Extension/ParameterExtension.php
@@ -13,11 +13,9 @@
namespace ApiPlatform\Doctrine\Odm\Extension;
-use ApiPlatform\Doctrine\Common\Filter\LoggerAwareInterface;
-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\Doctrine\Common\Filter\PropertyAwareFilterInterface;
+use ApiPlatform\Doctrine\Common\ParameterExtensionTrait;
+use ApiPlatform\Doctrine\Odm\Filter\FilterInterface; // Explicitly import PropertyAwareFilterInterface
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ParameterNotFound;
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
@@ -32,13 +30,16 @@
*/
final class ParameterExtension implements AggregationCollectionExtensionInterface, AggregationItemExtensionInterface
{
- use ParameterValueExtractorTrait;
+ use ParameterExtensionTrait;
public function __construct(
- private readonly ContainerInterface $filterLocator,
- private readonly ?ManagerRegistry $managerRegistry = null,
- private readonly ?LoggerInterface $logger = null,
+ ContainerInterface $filterLocator,
+ ?ManagerRegistry $managerRegistry = null,
+ ?LoggerInterface $logger = null,
) {
+ $this->filterLocator = $filterLocator;
+ $this->managerRegistry = $managerRegistry;
+ $this->logger = $logger;
}
/**
@@ -66,28 +67,7 @@ private function applyFilter(Builder $aggregationBuilder, ?string $resourceClass
continue;
}
- if ($this->managerRegistry && $filter instanceof ManagerRegistryAwareInterface && !$filter->hasManagerRegistry()) {
- $filter->setManagerRegistry($this->managerRegistry);
- }
-
- if ($this->logger && $filter instanceof LoggerAwareInterface && !$filter->hasLogger()) {
- $filter->setLogger($this->logger);
- }
-
- if ($filter instanceof AbstractFilter && !$filter->getProperties()) {
- $propertyKey = $parameter->getProperty() ?? $parameter->getKey();
-
- if (str_contains($propertyKey, ':property')) {
- $extraProperties = $parameter->getExtraProperties()['_properties'] ?? [];
- foreach (array_keys($extraProperties) as $property) {
- $properties[$property] = $parameter->getFilterContext();
- }
- } else {
- $properties = [$propertyKey => $parameter->getFilterContext()];
- }
-
- $filter->setProperties($properties ?? []);
- }
+ $this->configureFilter($filter, $parameter);
$context['filters'] = $values;
$context['parameter'] = $parameter;
diff --git a/src/Doctrine/Orm/Extension/ParameterExtension.php b/src/Doctrine/Orm/Extension/ParameterExtension.php
index b7c8d8938b9..0264567a602 100644
--- a/src/Doctrine/Orm/Extension/ParameterExtension.php
+++ b/src/Doctrine/Orm/Extension/ParameterExtension.php
@@ -13,11 +13,8 @@
namespace ApiPlatform\Doctrine\Orm\Extension;
-use ApiPlatform\Doctrine\Common\Filter\LoggerAwareInterface;
-use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface;
-use ApiPlatform\Doctrine\Common\ParameterValueExtractorTrait;
-use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
+use ApiPlatform\Doctrine\Common\ParameterExtensionTrait; // Explicitly import PropertyAwareFilterInterface
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
@@ -34,13 +31,16 @@
*/
final class ParameterExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
{
- use ParameterValueExtractorTrait;
+ use ParameterExtensionTrait;
public function __construct(
- private readonly ContainerInterface $filterLocator,
- private readonly ?ManagerRegistry $managerRegistry = null,
- private readonly ?LoggerInterface $logger = null,
+ ContainerInterface $filterLocator,
+ ?ManagerRegistry $managerRegistry = null,
+ ?LoggerInterface $logger = null,
) {
+ $this->filterLocator = $filterLocator;
+ $this->managerRegistry = $managerRegistry;
+ $this->logger = $logger;
}
/**
@@ -68,30 +68,7 @@ private function applyFilter(QueryBuilder $queryBuilder, QueryNameGeneratorInter
continue;
}
- if ($this->managerRegistry && $filter instanceof ManagerRegistryAwareInterface && !$filter->hasManagerRegistry()) {
- $filter->setManagerRegistry($this->managerRegistry);
- }
-
- if ($this->logger && $filter instanceof LoggerAwareInterface && !$filter->hasLogger()) {
- $filter->setLogger($this->logger);
- }
-
- if ($filter instanceof PropertyAwareFilterInterface) {
- $properties = [];