|
21 | 21 | use ApiPlatform\Metadata\ApiResource; |
22 | 22 | use ApiPlatform\Metadata\CollectionOperationInterface; |
23 | 23 | use ApiPlatform\Metadata\ErrorResource; |
24 | | -use ApiPlatform\Metadata\HttpOperation; |
25 | 24 | use ApiPlatform\Metadata\Operation; |
26 | 25 | use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
27 | 26 | use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
|
48 | 47 | */ |
49 | 48 | final class DocumentationNormalizer implements NormalizerInterface |
50 | 49 | { |
| 50 | + use HydraOperationsTrait; |
51 | 51 | use HydraPrefixTrait; |
52 | 52 | public const FORMAT = 'jsonld'; |
53 | 53 |
|
@@ -254,106 +254,6 @@ private function getHydraProperties(string $resourceClass, ApiResource $resource |
254 | 254 | return $properties; |
255 | 255 | } |
256 | 256 |
|
257 | | - /** |
258 | | - * Gets Hydra operations. |
259 | | - */ |
260 | | - private function getHydraOperations(bool $collection, ApiResource $resourceMetadata, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array |
261 | | - { |
262 | | - $hydraOperations = []; |
263 | | - foreach ($resourceMetadata->getOperations() as $operation) { |
264 | | - if (true === $operation->getHideHydraOperation()) { |
265 | | - continue; |
266 | | - } |
267 | | - |
268 | | - if (('POST' === $operation->getMethod() || $operation instanceof CollectionOperationInterface) !== $collection) { |
269 | | - continue; |
270 | | - } |
271 | | - |
272 | | - $hydraOperations[] = $this->getHydraOperation($operation, $operation->getShortName(), $hydraPrefix); |
273 | | - } |
274 | | - |
275 | | - return $hydraOperations; |
276 | | - } |
277 | | - |
278 | | - /** |
279 | | - * Gets and populates if applicable a Hydra operation. |
280 | | - */ |
281 | | - private function getHydraOperation(HttpOperation $operation, string $prefixedShortName, string $hydraPrefix): array |
282 | | - { |
283 | | - $method = $operation->getMethod() ?: 'GET'; |
284 | | - |
285 | | - $hydraOperation = $operation->getHydraContext() ?? []; |
286 | | - if ($operation->getDeprecationReason()) { |
287 | | - $hydraOperation['owl:deprecated'] = true; |
288 | | - } |
289 | | - |
290 | | - $shortName = $operation->getShortName(); |
291 | | - $inputMetadata = $operation->getInput() ?? []; |
292 | | - $outputMetadata = $operation->getOutput() ?? []; |
293 | | - |
294 | | - $inputClass = \array_key_exists('class', $inputMetadata) ? $inputMetadata['class'] : false; |
295 | | - $outputClass = \array_key_exists('class', $outputMetadata) ? $outputMetadata['class'] : false; |
296 | | - |
297 | | - if ('GET' === $method && $operation instanceof CollectionOperationInterface) { |
298 | | - $hydraOperation += [ |
299 | | - '@type' => [$hydraPrefix.'Operation', 'schema:FindAction'], |
300 | | - $hydraPrefix.'description' => "Retrieves the collection of $shortName resources.", |
301 | | - 'returns' => null === $outputClass ? 'owl:Nothing' : $hydraPrefix.'Collection', |
302 | | - ]; |
303 | | - } elseif ('GET' === $method) { |
304 | | - $hydraOperation += [ |
305 | | - '@type' => [$hydraPrefix.'Operation', 'schema:FindAction'], |
306 | | - $hydraPrefix.'description' => "Retrieves a $shortName resource.", |
307 | | - 'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName, |
308 | | - ]; |
309 | | - } elseif ('PATCH' === $method) { |
310 | | - $hydraOperation += [ |
311 | | - '@type' => $hydraPrefix.'Operation', |
312 | | - $hydraPrefix.'description' => "Updates the $shortName resource.", |
313 | | - 'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName, |
314 | | - 'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName, |
315 | | - ]; |
316 | | - |
317 | | - if (null !== $inputClass) { |
318 | | - $possibleValue = []; |
319 | | - foreach ($operation->getInputFormats() ?? [] as $mimeTypes) { |
320 | | - foreach ($mimeTypes as $mimeType) { |
321 | | - $possibleValue[] = $mimeType; |
322 | | - } |
323 | | - } |
324 | | - |
325 | | - $hydraOperation['expectsHeader'] = [['headerName' => 'Content-Type', 'possibleValue' => $possibleValue]]; |
326 | | - } |
327 | | - } elseif ('POST' === $method) { |
328 | | - $hydraOperation += [ |
329 | | - '@type' => [$hydraPrefix.'Operation', 'schema:CreateAction'], |
330 | | - $hydraPrefix.'description' => "Creates a $shortName resource.", |
331 | | - 'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName, |
332 | | - 'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName, |
333 | | - ]; |
334 | | - } elseif ('PUT' === $method) { |
335 | | - $hydraOperation += [ |
336 | | - '@type' => [$hydraPrefix.'Operation', 'schema:ReplaceAction'], |
337 | | - $hydraPrefix.'description' => "Replaces the $shortName resource.", |
338 | | - 'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName, |
339 | | - 'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName, |
340 | | - ]; |
341 | | - } elseif ('DELETE' === $method) { |
342 | | - $hydraOperation += [ |
343 | | - '@type' => [$hydraPrefix.'Operation', 'schema:DeleteAction'], |
344 | | - $hydraPrefix.'description' => "Deletes the $shortName resource.", |
345 | | - 'returns' => 'owl:Nothing', |
346 | | - ]; |
347 | | - } |
348 | | - |
349 | | - $hydraOperation[$hydraPrefix.'method'] ??= $method; |
350 | | - $hydraOperation[$hydraPrefix.'title'] ??= strtolower($method).$shortName.($operation instanceof CollectionOperationInterface ? 'Collection' : ''); |
351 | | - |
352 | | - ksort($hydraOperation); |
353 | | - |
354 | | - return $hydraOperation; |
355 | | - } |
356 | | - |
357 | 257 | /** |
358 | 258 | * Gets the range of the property. |
359 | 259 | */ |
|
0 commit comments