|
36 | 36 | use ApiPlatform\Validator\Exception\ValidationException; |
37 | 37 | use Psr\Log\LoggerInterface; |
38 | 38 |
|
| 39 | +/** |
| 40 | + * @internal since api-platform 4.2 |
| 41 | + */ |
39 | 42 | trait OperationDefaultsTrait |
40 | 43 | { |
41 | 44 | private CamelCaseToSnakeCaseNameConverter $camelCaseToSnakeCaseNameConverter; |
@@ -96,7 +99,10 @@ private function getResourceWithDefaults(string $resourceClass, string $shortNam |
96 | 99 | return $this->addGlobalDefaults($resource); |
97 | 100 | } |
98 | 101 |
|
99 | | - private function getDefaultHttpOperations($resource): iterable |
| 102 | + /** |
| 103 | + * @return Operations<HttpOperation>|array<int,HttpOperation> |
| 104 | + */ |
| 105 | + private function getDefaultHttpOperations(ApiResource $resource): iterable |
100 | 106 | { |
101 | 107 | if (enum_exists($resource->getClass())) { |
102 | 108 | return new Operations([new GetCollection(paginationEnabled: false), new Get()]); |
@@ -175,30 +181,14 @@ private function completeGraphQlOperations(ApiResource $resource): ApiResource |
175 | 181 | return $resource->withGraphQlOperations($graphQlOperations); |
176 | 182 | } |
177 | 183 |
|
| 184 | + /** |
| 185 | + * @param list<string> $ignoredOptions |
| 186 | + * |
| 187 | + * @return array<int,mixed> |
| 188 | + */ |
178 | 189 | private function getOperationWithDefaults(ApiResource $resource, Operation $operation, bool $generated = false, array $ignoredOptions = []): array |
179 | 190 | { |
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( |
202 | 192 | $resource->getExtraProperties(), |
203 | 193 | $operation->getExtraProperties(), |
204 | 194 | $generated ? ['generated_operation' => true] : [] |
@@ -249,6 +239,7 @@ private function getDefaultOperationName(HttpOperation $operation, string $resou |
249 | 239 | '_api_%s_%s%s', |
250 | 240 | $path ?: ($operation->getShortName() ?? $this->getDefaultShortname($resourceClass)), |
251 | 241 | strtolower($operation->getMethod()), |
252 | | - $operation instanceof CollectionOperationInterface ? '_collection' : ''); |
| 242 | + $operation instanceof CollectionOperationInterface ? '_collection' : '' |
| 243 | + ); |
253 | 244 | } |
254 | 245 | } |
0 commit comments