16
16
use ApiPlatform \Doctrine \Common \Filter \DateFilterInterface ;
17
17
use ApiPlatform \Doctrine \Common \Filter \DateFilterTrait ;
18
18
use ApiPlatform \Metadata \Exception \InvalidArgumentException ;
19
+ use ApiPlatform \Metadata \JsonSchemaFilterInterface ;
20
+ use ApiPlatform \Metadata \OpenApiParameterFilterInterface ;
19
21
use ApiPlatform \Metadata \Operation ;
22
+ use ApiPlatform \Metadata \Parameter ;
23
+ use ApiPlatform \Metadata \QueryParameter ;
24
+ use ApiPlatform \OpenApi \Model \Parameter as OpenApiParameter ;
20
25
use Doctrine \ODM \MongoDB \Aggregation \Builder ;
21
26
use Doctrine \ODM \MongoDB \Types \Type as MongoDbType ;
22
27
117
122
* @author Théo FIDRY <[email protected] >
118
123
* @author Alan Poulain <[email protected] >
119
124
*/
120
- final class DateFilter extends AbstractFilter implements DateFilterInterface
125
+ final class DateFilter extends AbstractFilter implements DateFilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface
121
126
{
122
127
use DateFilterTrait;
123
128
@@ -129,11 +134,11 @@ final class DateFilter extends AbstractFilter implements DateFilterInterface
129
134
/**
130
135
* {@inheritdoc}
131
136
*/
132
- protected function filterProperty (string $ property , $ values , Builder $ aggregationBuilder , string $ resourceClass , ?Operation $ operation = null , array &$ context = []): void
137
+ protected function filterProperty (string $ property , $ value , Builder $ aggregationBuilder , string $ resourceClass , ?Operation $ operation = null , array &$ context = []): void
133
138
{
134
- // Expect $values to be an array having the period as keys and the date value as values
139
+ // Expect $value to be an array having the period as keys and the date value as values
135
140
if (
136
- !\is_array ($ values )
141
+ !\is_array ($ value )
137
142
|| !$ this ->isPropertyEnabled ($ property , $ resourceClass )
138
143
|| !$ this ->isPropertyMapped ($ property , $ resourceClass )
139
144
|| !$ this ->isDateField ($ property , $ resourceClass )
@@ -153,42 +158,42 @@ protected function filterProperty(string $property, $values, Builder $aggregatio
153
158
$ aggregationBuilder ->match ()->field ($ matchField )->notEqual (null );
154
159
}
155
160
156
- if (isset ($ values [self ::PARAMETER_BEFORE ])) {
161
+ if (isset ($ value [self ::PARAMETER_BEFORE ])) {
157
162
$ this ->addMatch (
158
163
$ aggregationBuilder ,
159
164
$ matchField ,
160
165
self ::PARAMETER_BEFORE ,
161
- $ values [self ::PARAMETER_BEFORE ],
166
+ $ value [self ::PARAMETER_BEFORE ],
162
167
$ nullManagement
163
168
);
164
169
}
165
170
166
- if (isset ($ values [self ::PARAMETER_STRICTLY_BEFORE ])) {
171
+ if (isset ($ value [self ::PARAMETER_STRICTLY_BEFORE ])) {
167
172
$ this ->addMatch (
168
173
$ aggregationBuilder ,
169
174
$ matchField ,
170
175
self ::PARAMETER_STRICTLY_BEFORE ,
171
- $ values [self ::PARAMETER_STRICTLY_BEFORE ],
176
+ $ value [self ::PARAMETER_STRICTLY_BEFORE ],
172
177
$ nullManagement
173
178
);
174
179
}
175
180
176
- if (isset ($ values [self ::PARAMETER_AFTER ])) {
181
+ if (isset ($ value [self ::PARAMETER_AFTER ])) {
177
182
$ this ->addMatch (
178
183
$ aggregationBuilder ,
179
184
$ matchField ,
180
185
self ::PARAMETER_AFTER ,
181
- $ values [self ::PARAMETER_AFTER ],
186
+ $ value [self ::PARAMETER_AFTER ],
182
187
$ nullManagement
183
188
);
184
189
}
185
190
186
- if (isset ($ values [self ::PARAMETER_STRICTLY_AFTER ])) {
191
+ if (isset ($ value [self ::PARAMETER_STRICTLY_AFTER ])) {
187
192
$ this ->addMatch (
188
193
$ aggregationBuilder ,
189
194
$ matchField ,
190
195
self ::PARAMETER_STRICTLY_AFTER ,
191
- $ values [self ::PARAMETER_STRICTLY_AFTER ],
196
+ $ value [self ::PARAMETER_STRICTLY_AFTER ],
192
197
$ nullManagement
193
198
);
194
199
}
@@ -237,4 +242,25 @@ private function addMatch(Builder $aggregationBuilder, string $field, string $op
237
242
238
243
$ aggregationBuilder ->match ()->addAnd ($ aggregationBuilder ->matchExpr ()->field ($ field )->operator ($ operatorValue [$ operator ], $ value ));
239
244
}
245
+
246
+ /**
247
+ * @return array<string, string>
248
+ */
249
+ public function getSchema (Parameter $ parameter ): array
250
+ {
251
+ return ['type ' => 'date ' ];
252
+ }
253
+
254
+ public function getOpenApiParameters (Parameter $ parameter ): OpenApiParameter |array |null
255
+ {
256
+ $ in = $ parameter instanceof QueryParameter ? 'query ' : 'header ' ;
257
+ $ key = $ parameter ->getKey ();
258
+
259
+ return [
260
+ new OpenApiParameter (name: $ key .'[after] ' , in: $ in ),
261
+ new OpenApiParameter (name: $ key .'[before] ' , in: $ in ),
262
+ new OpenApiParameter (name: $ key .'[strictly_after] ' , in: $ in ),
263
+ new OpenApiParameter (name: $ key .'[strictly_before] ' , in: $ in ),
264
+ ];
265
+ }
240
266
}
0 commit comments