16
16
use ApiPlatform \Core \Operation \PathSegmentNameGeneratorInterface ;
17
17
use ApiPlatform \Metadata \ApiResource ;
18
18
use ApiPlatform \Metadata \Operation ;
19
+ use ApiPlatform \Metadata \Operations ;
19
20
use ApiPlatform \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
20
21
use ApiPlatform \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
21
22
use ApiPlatform \Metadata \Resource \ResourceMetadataCollection ;
@@ -58,8 +59,8 @@ public function create(string $resourceClass): ResourceMetadataCollection
58
59
$ resourceMetadataCollection [$ i ] = $ resource ->withExtraProperties ($ resource ->getExtraProperties () + ['user_defined_uri_template ' => true ]);
59
60
}
60
61
61
- $ operations = $ resource -> getOperations ();
62
- foreach ($ resource ->getOperations () as $ key => $ operation ) {
62
+ $ operations = new Operations ();
63
+ foreach ($ resource ->getOperations () ?? new Operations () as $ key => $ operation ) {
63
64
$ operation = $ this ->configureUriVariables ($ operation );
64
65
65
66
if ($ operation ->getUriTemplate ()) {
@@ -69,16 +70,14 @@ public function create(string $resourceClass): ResourceMetadataCollection
69
70
}
70
71
71
72
if ($ routeName = $ operation ->getRouteName ()) {
72
- $ operations ->remove ( $ key )-> add ($ routeName , $ operation );
73
+ $ operations ->add ($ routeName , $ operation );
73
74
continue ;
74
75
}
75
76
76
77
$ operation = $ operation ->withUriTemplate ($ this ->generateUriTemplate ($ operation ));
77
78
$ operationName = $ operation ->getName () ?: sprintf ('_api_%s_%s%s ' , $ operation ->getUriTemplate (), strtolower ($ operation ->getMethod () ?? Operation::METHOD_GET ), $ operation ->isCollection () ? '_collection ' : '' );
78
79
79
- // Change the operation key
80
- $ operations ->remove ($ key )
81
- ->add ($ operationName , $ operation );
80
+ $ operations ->add ($ operationName , $ operation );
82
81
}
83
82
84
83
$ resource = $ resource ->withOperations ($ operations ->sort ());
@@ -163,32 +162,38 @@ private function configureUriVariables($operation)
163
162
*/
164
163
private function normalizeUriVariables ($ operation )
165
164
{
166
- $ uriVariables = $ operation ->getUriVariables () ?? [];
165
+ $ uriVariables = (array ) ($ operation ->getUriVariables () ?? []);
166
+ $ normalizedUriVariables = [];
167
167
$ resourceClass = $ operation ->getClass ();
168
168
169
169
foreach ($ uriVariables as $ parameterName => $ uriVariable ) {
170
- if ($ uriVariable instanceof UriVariable) {
171
- continue ;
172
- }
170
+ $ normalizedParameterName = $ parameterName ;
171
+ $ normalizedUriVariable = $ uriVariable ;
173
172
174
- if (\is_int ($ parameterName )) {
175
- $ uriVariables [$ uriVariable ] = (new UriVariable ())->withIdentifiers ([$ uriVariable ])->withTargetClass ($ resourceClass );
176
- } elseif (\is_string ($ uriVariable )) {
177
- $ uriVariables [$ parameterName ] = (new UriVariable ())->withIdentifiers ([$ uriVariable ])->withTargetClass ($ resourceClass );
178
- } elseif (\is_array ($ uriVariable ) && !isset ($ uriVariable ['class ' ])) {
179
- $ uriVariables [$ parameterName ] = (new UriVariable ())->withIdentifiers ($ uriVariable )->withTargetClass ($ resourceClass );
180
- } elseif (\is_array ($ uriVariable )) {
181
- $ uriVariables [$ parameterName ] = new UriVariable (null , $ uriVariable ['inverse_property ' ] ?? null , $ uriVariable ['property ' ] ?? null , $ uriVariable ['class ' ], $ uriVariable ['identifiers ' ] ?? null , $ uriVariable ['composite_identifier ' ] ?? null );
182
- } else {
183
- $ uriVariables [$ parameterName ] = $ uriVariable ;
173
+ if (\is_int ($ normalizedParameterName )) {
174
+ $ normalizedParameterName = $ normalizedUriVariable ;
175
+ }
176
+ if (\is_string ($ normalizedUriVariable )) {
177
+ $ normalizedUriVariable = (new UriVariable ())->withIdentifiers ([$ normalizedUriVariable ])->withTargetClass ($ resourceClass );
178
+ }
179
+ if (\is_array ($ normalizedUriVariable )) {
180
+ if (!isset ($ normalizedUriVariable ['class ' ])) {
181
+ if (2 !== \count ($ normalizedUriVariable )) {
182
+ throw new \LogicException ("The uriVariables shortcut syntax needs to be the tuple: 'uriVariable' => [targetClass, targetProperty] " );
183
+ }
184
+ $ normalizedUriVariable = (new UriVariable ())->withIdentifiers ([$ normalizedUriVariable [1 ]])->withTargetClass ($ normalizedUriVariable [0 ]);
185
+ } else {
186
+ $ normalizedUriVariable = new UriVariable (null , $ normalizedUriVariable ['inverse_property ' ] ?? null , $ normalizedUriVariable ['property ' ] ?? null , $ normalizedUriVariable ['class ' ], $ normalizedUriVariable ['identifiers ' ] ?? null , $ normalizedUriVariable ['composite_identifier ' ] ?? null );
187
+ }
184
188
}
185
-
186
189
if (null !== ($ hasCompositeIdentifier = $ operation ->getCompositeIdentifier ())) {
187
- $ uriVariables [ $ parameterName ] = $ uriVariables [ $ parameterName ] ->withCompositeIdentifier ($ hasCompositeIdentifier );
190
+ $ normalizedUriVariable = $ normalizedUriVariable ->withCompositeIdentifier ($ hasCompositeIdentifier );
188
191
}
192
+
193
+ $ normalizedUriVariables [$ normalizedParameterName ] = $ normalizedUriVariable ;
189
194
}
190
195
191
- return $ this ->mergeUriVariablesAttributes ($ operation ->withUriVariables ($ uriVariables ));
196
+ return $ this ->mergeUriVariablesAttributes ($ operation ->withUriVariables ($ normalizedUriVariables ));
192
197
}
193
198
194
199
/**
0 commit comments