1515
1616use ApiPlatform \Doctrine \Common \Filter \ExistsFilterInterface ;
1717use ApiPlatform \Doctrine \Common \Filter \ExistsFilterTrait ;
18+ use ApiPlatform \Metadata \JsonSchemaFilterInterface ;
19+ use ApiPlatform \Metadata \OpenApiParameterFilterInterface ;
1820use ApiPlatform \Metadata \Operation ;
21+ use ApiPlatform \Metadata \Parameter ;
22+ use ApiPlatform \OpenApi \Model \Parameter as OpenApiParameter ;
1923use Doctrine \ODM \MongoDB \Aggregation \Builder ;
2024use Doctrine \ODM \MongoDB \Mapping \ClassMetadata ;
2125use Doctrine \Persistence \ManagerRegistry ;
107111 * @author Teoh Han Hui <[email protected] > 108112 * @author Alan Poulain <[email protected] > 109113 */
110- final class ExistsFilter extends AbstractFilter implements ExistsFilterInterface
114+ final class ExistsFilter extends AbstractFilter implements ExistsFilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface
111115{
112116 use ExistsFilterTrait;
113117
114- public function __construct (ManagerRegistry $ managerRegistry , ?LoggerInterface $ logger = null , ?array $ properties = null , string $ existsParameterName = self ::QUERY_PARAMETER_KEY , ?NameConverterInterface $ nameConverter = null )
118+ public function __construct (? ManagerRegistry $ managerRegistry = null , ?LoggerInterface $ logger = null , ?array $ properties = null , string $ existsParameterName = self ::QUERY_PARAMETER_KEY , ?NameConverterInterface $ nameConverter = null )
115119 {
120+ if (\is_array ($ properties ) && \is_int (key ($ properties ))) {
121+ $ properties = array_flip ($ properties );
122+ }
123+
116124 parent ::__construct ($ managerRegistry , $ logger , $ properties , $ nameConverter );
117125
118126 $ this ->existsParameterName = $ existsParameterName ;
@@ -123,6 +131,12 @@ public function __construct(ManagerRegistry $managerRegistry, ?LoggerInterface $
123131 */
124132 public function apply (Builder $ aggregationBuilder , string $ resourceClass , ?Operation $ operation = null , array &$ context = []): void
125133 {
134+ $ parameter = $ context ['parameter ' ] ?? null ;
135+
136+ if (null !== ($ value = $ context ['filters ' ][$ parameter ?->getProperty()] ?? null )) {
137+ $ this ->filterProperty ($ this ->denormalizePropertyName ($ parameter ->getProperty ()), $ value , $ aggregationBuilder , $ resourceClass , $ operation , $ context );
138+ }
139+
126140 foreach ($ context ['filters ' ][$ this ->existsParameterName ] ?? [] as $ property => $ value ) {
127141 $ this ->filterProperty ($ this ->denormalizePropertyName ($ property ), $ value , $ aggregationBuilder , $ resourceClass , $ operation , $ context );
128142 }
@@ -167,4 +181,24 @@ protected function isNullableField(string $property, string $resourceClass): boo
167181
168182 return $ metadata instanceof ClassMetadata && $ metadata ->hasField ($ field ) ? $ metadata ->isNullable ($ field ) : false ;
169183 }
184+
185+ public function getSchema (Parameter $ parameter ): array
186+ {
187+ return ['type ' => 'boolean ' ];
188+ }
189+ public function getOpenApiParameters (Parameter $ parameter ): OpenApiParameter |array |null
190+ {
191+ if (str_contains ($ parameter ->getKey (), ':property ' )) {
192+ $ parameters = [];
193+ $ key = str_replace ('[:property] ' , '' , $ parameter ->getKey ());
194+ foreach (array_keys ($ parameter ->getExtraProperties ()['_properties ' ] ?? []) as $ property ) {
195+ $ parameters [] = new OpenApiParameter (name: \sprintf ('%s[%s] ' , $ key , $ property ), in: 'query ' );
196+ }
197+
198+ return $ parameters ;
199+ }
200+
201+ return null ;
202+ }
203+
170204}
0 commit comments