vendor/api-platform/core/src/Hydra/Serializer/EntrypointNormalizer.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Hydra\Serializer;
  12. use ApiPlatform\Api\Entrypoint;
  13. use ApiPlatform\Api\IriConverterInterface;
  14. use ApiPlatform\Api\UrlGeneratorInterface;
  15. use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
  16. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  17. use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
  18. use ApiPlatform\Exception\InvalidArgumentException;
  19. use ApiPlatform\Exception\OperationNotFoundException;
  20. use ApiPlatform\Metadata\CollectionOperationInterface;
  21. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  22. use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
  23. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  24. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  25. /**
  26.  * Normalizes the API entrypoint.
  27.  *
  28.  * @author Kévin Dunglas <dunglas@gmail.com>
  29.  */
  30. final class EntrypointNormalizer implements NormalizerInterfaceCacheableSupportsMethodInterface
  31. {
  32.     public const FORMAT 'jsonld';
  33.     private $resourceMetadataFactory;
  34.     private $iriConverter;
  35.     private $urlGenerator;
  36.     public function __construct($resourceMetadataFactory$iriConverterUrlGeneratorInterface $urlGenerator)
  37.     {
  38.         if ($iriConverter instanceof LegacyIriConverterInterface) {
  39.             trigger_deprecation('api-platform/core''2.7'sprintf('Use an implementation of "%s" instead of "%s".'IriConverterInterface::class, LegacyIriConverterInterface::class));
  40.         }
  41.         $this->iriConverter $iriConverter;
  42.         if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  43.             trigger_deprecation('api-platform/core''2.7'sprintf('Use "%s" instead of "%s".'ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  44.         }
  45.         $this->resourceMetadataFactory $resourceMetadataFactory;
  46.         $this->urlGenerator $urlGenerator;
  47.     }
  48.     public function normalize($object$format null, array $context = []): array
  49.     {
  50.         $entrypoint = [
  51.             '@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Entrypoint']),
  52.             '@id' => $this->urlGenerator->generate('api_entrypoint'),
  53.             '@type' => 'Entrypoint',
  54.         ];
  55.         foreach ($object->getResourceNameCollection() as $resourceClass) {
  56.             /** @var ResourceMetadata|ResourceMetadataCollection */
  57.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  58.             if ($resourceMetadata instanceof ResourceMetadata) {
  59.                 if (empty($resourceMetadata->getCollectionOperations())) {
  60.                     continue;
  61.                 }
  62.                 try {
  63.                     $entrypoint[lcfirst($resourceMetadata->getShortName())] = $this->iriConverter->getIriFromResourceClass($resourceClass);
  64.                 } catch (InvalidArgumentException $ex) {
  65.                     // Ignore resources without GET operations
  66.                 }
  67.                 continue;
  68.             }
  69.             foreach ($resourceMetadata as $resource) {
  70.                 if ($resource->getExtraProperties()['is_alternate_resource_metadata'] ?? false) {
  71.                     continue;
  72.                 }
  73.                 foreach ($resource->getOperations() as $operationName => $operation) {
  74.                     $key lcfirst($resource->getShortName());
  75.                     if (!$operation instanceof CollectionOperationInterface || isset($entrypoint[$key])) {
  76.                         continue;
  77.                     }
  78.                     try {
  79.                         $entrypoint[$key] = $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getIriFromResourceClass($resourceClass) : $this->iriConverter->getIriFromResource($resourceClassUrlGeneratorInterface::ABS_PATH$operation);
  80.                     } catch (InvalidArgumentException|OperationNotFoundException $ex) {
  81.                         // Ignore resources without GET operations
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         ksort($entrypoint);
  87.         return $entrypoint;
  88.     }
  89.     public function supportsNormalization($data$format null, array $context = []): bool
  90.     {
  91.         return self::FORMAT === $format && $data instanceof Entrypoint;
  92.     }
  93.     public function hasCacheableSupportsMethod(): bool
  94.     {
  95.         return true;
  96.     }
  97. }
  98. class_alias(EntrypointNormalizer::class, \ApiPlatform\Core\Hydra\Serializer\EntrypointNormalizer::class);