3636use ApiPlatform \Validator \Exception \ValidationException ;
3737use Psr \Log \LoggerInterface ;
3838
39+ /**
40+ * @internal since api-platform 4.2
41+ */
3942trait OperationDefaultsTrait
4043{
4144 private CamelCaseToSnakeCaseNameConverter $ camelCaseToSnakeCaseNameConverter ;
@@ -96,7 +99,10 @@ private function getResourceWithDefaults(string $resourceClass, string $shortNam
9699 return $ this ->addGlobalDefaults ($ resource );
97100 }
98101
99- private function getDefaultHttpOperations ($ resource ): iterable
102+ /**
103+ * @return Operations<HttpOperation>|array<int,HttpOperation>
104+ */
105+ private function getDefaultHttpOperations (ApiResource $ resource ): iterable
100106 {
101107 if (enum_exists ($ resource ->getClass ())) {
102108 return new Operations ([new GetCollection (paginationEnabled: false ), new Get ()]);
@@ -175,30 +181,14 @@ private function completeGraphQlOperations(ApiResource $resource): ApiResource
175181 return $ resource ->withGraphQlOperations ($ graphQlOperations );
176182 }
177183
184+ /**
185+ * @param list<string> $ignoredOptions
186+ *
187+ * @return array<int,mixed>
188+ */
178189 private function getOperationWithDefaults (ApiResource $ resource , Operation $ operation , bool $ generated = false , array $ ignoredOptions = []): array
179190 {
180- // Inherit from resource defaults
181- foreach (get_class_methods ($ resource ) as $ methodName ) {
182- if (!str_starts_with ($ methodName , 'get ' )) {
183- continue ;
184- }
185-
186- if (\in_array (lcfirst (substr ($ methodName , 3 )), $ ignoredOptions , true )) {
187- continue ;
188- }
189-
190- if (!method_exists ($ operation , $ methodName ) || null !== $ operation ->{$ methodName }()) {
191- continue ;
192- }
193-
194- if (null === ($ value = $ resource ->{$ methodName }())) {
195- continue ;
196- }
197-
198- $ operation = $ operation ->{'with ' .substr ($ methodName , 3 )}($ value );
199- }
200-
201- $ operation = $ operation ->withExtraProperties (array_merge (
191+ $ operation = $ operation ->cascadeFromResource ($ resource , $ ignoredOptions )->withExtraProperties (array_merge (
202192 $ resource ->getExtraProperties (),
203193 $ operation ->getExtraProperties (),
204194 $ generated ? ['generated_operation ' => true ] : []
@@ -229,6 +219,7 @@ private function getOperationWithDefaults(ApiResource $resource, Operation $oper
229219 }
230220
231221 $ operationName = $ operation ->getName () ?? $ this ->getDefaultOperationName ($ operation , $ resource ->getClass ());
222+ // $operation = $operation->withName($operationName);
232223
233224 return [
234225 $ operationName ,
@@ -249,6 +240,7 @@ private function getDefaultOperationName(HttpOperation $operation, string $resou
249240 '_api_%s_%s%s ' ,
250241 $ path ?: ($ operation ->getShortName () ?? $ this ->getDefaultShortname ($ resourceClass )),
251242 strtolower ($ operation ->getMethod ()),
252- $ operation instanceof CollectionOperationInterface ? '_collection ' : '' );
243+ $ operation instanceof CollectionOperationInterface ? '_collection ' : ''
244+ );
253245 }
254246}
0 commit comments