13
13
14
14
namespace ApiPlatform \Core \Hydra \Serializer ;
15
15
16
+ use ApiPlatform \Api \ResourceClassResolverInterface ;
16
17
use ApiPlatform \Core \Api \FilterCollection ;
17
18
use ApiPlatform \Core \Api \FilterInterface ;
18
19
use ApiPlatform \Core \Api \FilterLocatorTrait ;
19
- use ApiPlatform \Core \Api \ResourceClassResolverInterface ;
20
+ use ApiPlatform \Core \Api \ResourceClassResolverInterface as LegacyResourceClassResolverInterface ;
20
21
use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
21
22
use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
22
23
use Psr \Container \ContainerInterface ;
33
34
final class CollectionFiltersNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface
34
35
{
35
36
use FilterLocatorTrait;
36
-
37
37
private $ collectionNormalizer ;
38
38
private $ resourceMetadataFactory ;
39
39
private $ resourceClassResolver ;
40
40
41
41
/**
42
- * @param ContainerInterface|FilterCollection $filterLocator The new filter locator or the deprecated filter collection
43
- * @param mixed $resourceMetadataFactory
42
+ * @param ContainerInterface|FilterCollection $filterLocator The new filter locator or the deprecated filter collection
43
+ * @param mixed $resourceMetadataFactory
44
+ * @param ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver
44
45
*/
45
- public function __construct (NormalizerInterface $ collectionNormalizer , $ resourceMetadataFactory , ResourceClassResolverInterface $ resourceClassResolver , $ filterLocator )
46
+ public function __construct (NormalizerInterface $ collectionNormalizer , $ resourceMetadataFactory , $ resourceClassResolver , $ filterLocator )
46
47
{
47
48
$ this ->setFilterLocator ($ filterLocator );
48
-
49
49
$ this ->collectionNormalizer = $ collectionNormalizer ;
50
50
$ this ->resourceMetadataFactory = $ resourceMetadataFactory ;
51
51
$ this ->resourceClassResolver = $ resourceClassResolver ;
52
-
53
52
if (!$ resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
54
53
trigger_deprecation ('api-platform/core ' , '2.7 ' , sprintf ('Use "%s" instead of "%s". ' , ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
55
54
}
@@ -80,17 +79,13 @@ public function normalize($object, $format = null, array $context = []): array
80
79
if (!\is_array ($ data )) {
81
80
throw new UnexpectedValueException ('Expected data to be an array ' );
82
81
}
83
-
84
82
if (!isset ($ context ['resource_class ' ]) || isset ($ context ['api_sub_level ' ])) {
85
83
return $ data ;
86
84
}
87
-
88
85
$ resourceClass = $ this ->resourceClassResolver ->getResourceClass ($ object , $ context ['resource_class ' ]);
89
86
$ resourceFilters = null ;
90
-
91
87
if ($ this ->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
92
88
$ resourceMetadata = $ this ->resourceMetadataFactory ->create ($ resourceClass );
93
-
94
89
$ operationName = $ context ['collection_operation_name ' ] ?? null ;
95
90
if (null === $ operationName ) {
96
91
$ resourceFilters = $ resourceMetadata ->getAttribute ('filters ' , []);
@@ -101,23 +96,19 @@ public function normalize($object, $format = null, array $context = []): array
101
96
$ operation = $ context ['operation ' ] ?? $ this ->resourceMetadataFactory ->create ($ resourceClass )->getOperation ($ context ['operation_name ' ] ?? null );
102
97
$ resourceFilters = $ operation ->getFilters ();
103
98
}
104
-
105
99
if (!$ resourceFilters ) {
106
100
return $ data ;
107
101
}
108
-
109
102
$ requestParts = parse_url ($ context ['request_uri ' ] ?? '' );
110
103
if (!\is_array ($ requestParts )) {
111
104
return $ data ;
112
105
}
113
-
114
106
$ currentFilters = [];
115
107
foreach ($ resourceFilters as $ filterId ) {
116
108
if ($ filter = $ this ->getFilter ($ filterId )) {
117
109
$ currentFilters [] = $ filter ;
118
110
}
119
111
}
120
-
121
112
if ($ currentFilters ) {
122
113
$ data ['hydra:search ' ] = $ this ->getSearch ($ resourceClass , $ requestParts , $ currentFilters );
123
114
}
@@ -147,20 +138,10 @@ private function getSearch(string $resourceClass, array $parts, array $filters):
147
138
foreach ($ filters as $ filter ) {
148
139
foreach ($ filter ->getDescription ($ resourceClass ) as $ variable => $ data ) {
149
140
$ variables [] = $ variable ;
150
- $ mapping [] = [
151
- '@type ' => 'IriTemplateMapping ' ,
152
- 'variable ' => $ variable ,
153
- 'property ' => $ data ['property ' ],
154
- 'required ' => $ data ['required ' ],
155
- ];
141
+ $ mapping [] = ['@type ' => 'IriTemplateMapping ' , 'variable ' => $ variable , 'property ' => $ data ['property ' ], 'required ' => $ data ['required ' ]];
156
142
}
157
143
}
158
144
159
- return [
160
- '@type ' => 'hydra:IriTemplate ' ,
161
- 'hydra:template ' => sprintf ('%s{?%s} ' , $ parts ['path ' ], implode (', ' , $ variables )),
162
- 'hydra:variableRepresentation ' => 'BasicRepresentation ' ,
163
- 'hydra:mapping ' => $ mapping ,
164
- ];
145
+ return ['@type ' => 'hydra:IriTemplate ' , 'hydra:template ' => sprintf ('%s{?%s} ' , $ parts ['path ' ], implode (', ' , $ variables )), 'hydra:variableRepresentation ' => 'BasicRepresentation ' , 'hydra:mapping ' => $ mapping ];
165
146
}
166
147
}
0 commit comments