13
13
14
14
namespace ApiPlatform \Doctrine \Odm \Extension ;
15
15
16
+ use ApiPlatform \Doctrine \Common \Filter \ManagerRegistryAwareInterface ;
16
17
use ApiPlatform \Doctrine \Common \ParameterValueExtractorTrait ;
18
+ use ApiPlatform \Doctrine \Odm \Filter \AbstractFilter ;
17
19
use ApiPlatform \Doctrine \Odm \Filter \FilterInterface ;
20
+ use ApiPlatform \Metadata \Exception \InvalidArgumentException ;
18
21
use ApiPlatform \Metadata \Operation ;
19
22
use ApiPlatform \State \ParameterNotFound ;
23
+ use Doctrine \Bundle \MongoDBBundle \ManagerRegistry ;
20
24
use Doctrine \ODM \MongoDB \Aggregation \Builder ;
25
+ use Psr \Container \ContainerExceptionInterface ;
21
26
use Psr \Container \ContainerInterface ;
27
+ use Psr \Container \NotFoundExceptionInterface ;
22
28
23
29
/**
24
30
* Reads operation parameters and execute its filter.
@@ -29,14 +35,20 @@ final class ParameterExtension implements AggregationCollectionExtensionInterfac
29
35
{
30
36
use ParameterValueExtractorTrait;
31
37
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
+ ) {
34
42
}
35
43
44
+ /**
45
+ * @throws ContainerExceptionInterface
46
+ * @throws NotFoundExceptionInterface
47
+ */
36
48
private function applyFilter (Builder $ aggregationBuilder , ?string $ resourceClass = null , ?Operation $ operation = null , array &$ context = []): void
37
49
{
38
50
foreach ($ operation ->getParameters () ?? [] as $ parameter ) {
39
- if (! ($ v = $ parameter ->getValue ()) || $ v instanceof ParameterNotFound) {
51
+ if (null === ($ v = $ parameter ->getValue ()) || $ v instanceof ParameterNotFound) {
40
52
continue ;
41
53
}
42
54
@@ -45,14 +57,29 @@ private function applyFilter(Builder $aggregationBuilder, ?string $resourceClass
45
57
continue ;
46
58
}
47
59
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 ' ];
56
83
}
57
84
}
58
85
}
0 commit comments