1313
1414namespace ApiPlatform \Doctrine \Odm \Extension ;
1515
16+ use ApiPlatform \Doctrine \Common \Filter \ManagerRegistryAwareInterface ;
1617use ApiPlatform \Doctrine \Common \ParameterValueExtractorTrait ;
18+ use ApiPlatform \Doctrine \Odm \Filter \AbstractFilter ;
1719use ApiPlatform \Doctrine \Odm \Filter \FilterInterface ;
1820use ApiPlatform \Metadata \Operation ;
1921use ApiPlatform \State \ParameterNotFound ;
22+ use Doctrine \Bundle \MongoDBBundle \ManagerRegistry ;
2023use Doctrine \ODM \MongoDB \Aggregation \Builder ;
24+ use Psr \Container \ContainerExceptionInterface ;
2125use Psr \Container \ContainerInterface ;
26+ use Psr \Container \NotFoundExceptionInterface ;
2227
2328/**
2429 * Reads operation parameters and execute its filter.
@@ -29,14 +34,20 @@ final class ParameterExtension implements AggregationCollectionExtensionInterfac
2934{
3035 use ParameterValueExtractorTrait;
3136
32- public function __construct (private readonly ContainerInterface $ filterLocator )
33- {
37+ public function __construct (
38+ private readonly ContainerInterface $ filterLocator ,
39+ private readonly ?ManagerRegistry $ managerRegistry = null ,
40+ ) {
3441 }
3542
43+ /**
44+ * @throws ContainerExceptionInterface
45+ * @throws NotFoundExceptionInterface
46+ */
3647 private function applyFilter (Builder $ aggregationBuilder , ?string $ resourceClass = null , ?Operation $ operation = null , array &$ context = []): void
3748 {
3849 foreach ($ operation ->getParameters () ?? [] as $ parameter ) {
39- if (! ($ v = $ parameter ->getValue ()) || $ v instanceof ParameterNotFound) {
50+ if (null === ($ v = $ parameter ->getValue ()) || $ v instanceof ParameterNotFound) {
4051 continue ;
4152 }
4253
@@ -45,14 +56,33 @@ private function applyFilter(Builder $aggregationBuilder, ?string $resourceClass
4556 continue ;
4657 }
4758
48- $ filter = $ this ->filterLocator ->has ($ filterId ) ? $ this ->filterLocator ->get ($ filterId ) : null ;
49- if ($ filter instanceof FilterInterface) {
50- $ filterContext = ['filters ' => $ values , 'parameter ' => $ parameter ];
51- $ filter ->apply ($ aggregationBuilder , $ resourceClass , $ operation , $ filterContext );
52- // update by reference
53- if (isset ($ filterContext ['mongodb_odm_sort_fields ' ])) {
54- $ context ['mongodb_odm_sort_fields ' ] = $ filterContext ['mongodb_odm_sort_fields ' ];
55- }
59+ $ filter = match (true ) {
60+ $ filterId instanceof FilterInterface => $ filterId ,
61+ \is_string ($ filterId ) && $ this ->filterLocator ->has ($ filterId ) => $ this ->filterLocator ->get ($ filterId ),
62+ default => null ,
63+ };
64+
65+ if (!($ filter instanceof FilterInterface)) {
66+ return ;
67+ }
68+
69+ if ($ filter instanceof ManagerRegistryAwareInterface && !$ filter ->hasManagerRegistry ()) {
70+ $ filter ->setManagerRegistry ($ this ->managerRegistry );
71+ }
72+
73+ if ($ filter instanceof AbstractFilter && !$ filter ->getProperties ()) {
74+ $ propertyKey = $ parameter ->getProperty () ?? $ parameter ->getKey ();
75+ $ filterContext = $ parameter ->getFilterContext ();
76+
77+ $ properties = \is_array ($ filterContext ) ? $ filterContext : [$ propertyKey => $ filterContext ];
78+ $ filter ->setProperties ($ properties );
79+ }
80+
81+ $ filterContext = ['filters ' => $ values , 'parameter ' => $ parameter ];
82+ $ filter ->apply ($ aggregationBuilder , $ resourceClass , $ operation , $ filterContext );
83+ // update by reference
84+ if (isset ($ filterContext ['mongodb_odm_sort_fields ' ])) {
85+ $ context ['mongodb_odm_sort_fields ' ] = $ filterContext ['mongodb_odm_sort_fields ' ];
5686 }
5787 }
5888 }
0 commit comments