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 ;
20+ use ApiPlatform \Metadata \Exception \InvalidArgumentException ;
1821use ApiPlatform \Metadata \Operation ;
1922use ApiPlatform \State \ParameterNotFound ;
23+ use Doctrine \Bundle \MongoDBBundle \ManagerRegistry ;
2024use Doctrine \ODM \MongoDB \Aggregation \Builder ;
25+ use Psr \Container \ContainerExceptionInterface ;
2126use Psr \Container \ContainerInterface ;
27+ use Psr \Container \NotFoundExceptionInterface ;
2228
2329/**
2430 * Reads operation parameters and execute its filter.
@@ -29,14 +35,20 @@ final class ParameterExtension implements AggregationCollectionExtensionInterfac
2935{
3036 use ParameterValueExtractorTrait;
3137
32- public function __construct (private readonly ContainerInterface $ filterLocator )
33- {
38+ public function __construct (
39+ private readonly ContainerInterface $ filterLocator ,
40+ private readonly ?ManagerRegistry $ managerRegistry = null ,
41+ ) {
3442 }
3543
44+ /**
45+ * @throws ContainerExceptionInterface
46+ * @throws NotFoundExceptionInterface
47+ */
3648 private function applyFilter (Builder $ aggregationBuilder , ?string $ resourceClass = null , ?Operation $ operation = null , array &$ context = []): void
3749 {
3850 foreach ($ operation ->getParameters () ?? [] as $ parameter ) {
39- if (! ($ v = $ parameter ->getValue ()) || $ v instanceof ParameterNotFound) {
51+ if (null === ($ v = $ parameter ->getValue ()) || $ v instanceof ParameterNotFound) {
4052 continue ;
4153 }
4254
@@ -45,14 +57,29 @@ private function applyFilter(Builder $aggregationBuilder, ?string $resourceClass
4557 continue ;
4658 }
4759
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- }
60+ $ filter = match (true ) {
61+ $ filterId instanceof FilterInterface => $ filterId ,
62+ \is_string ($ filterId ) && $ this ->filterLocator ->has ($ filterId ) => $ this ->filterLocator ->get ($ filterId ),
63+ default => null ,
64+ };
65+
66+ if (!($ filter instanceof FilterInterface)) {
67+ throw new InvalidArgumentException (\sprintf ('Could not find filter "%s" for parameter "%s" in operation "%s" for resource "%s". ' , $ filterId , $ parameter ->getKey (), $ operation ?->getShortName(), $ resourceClass ));
68+ }
69+
70+ if ($ filter instanceof ManagerRegistryAwareInterface && !$ filter ->hasManagerRegistry ()) {
71+ $ filter ->setManagerRegistry ($ this ->managerRegistry );
72+ }
73+
74+ if ($ filter instanceof AbstractFilter && !$ filter ->getProperties ()) {
75+ $ filter ->setProperties ([$ parameter ->getProperty () ?? $ parameter ->getKey () => []]);
76+ }
77+
78+ $ filterContext = ['filters ' => $ values , 'parameter ' => $ parameter ];
79+ $ filter ->apply ($ aggregationBuilder , $ resourceClass , $ operation , $ filterContext );
80+ // update by reference
81+ if (isset ($ filterContext ['mongodb_odm_sort_fields ' ])) {
82+ $ context ['mongodb_odm_sort_fields ' ] = $ filterContext ['mongodb_odm_sort_fields ' ];
5683 }
5784 }
5885 }
0 commit comments