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

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.     /**
  49.      * {@inheritdoc}
  50.      */
  51.     public function normalize($object$format null, array $context = []): array
  52.     {
  53.         $entrypoint = [
  54.             '@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Entrypoint']),
  55.             '@id' => $this->urlGenerator->generate('api_entrypoint'),
  56.             '@type' => 'Entrypoint',
  57.         ];
  58.         foreach ($object->getResourceNameCollection() as $resourceClass) {
  59.             /** @var ResourceMetadata|ResourceMetadataCollection */
  60.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  61.             if ($resourceMetadata instanceof ResourceMetadata) {
  62.                 if (empty($resourceMetadata->getCollectionOperations())) {
  63.                     continue;
  64.                 }
  65.                 try {
  66.                     $entrypoint[lcfirst($resourceMetadata->getShortName())] = $this->iriConverter->getIriFromResourceClass($resourceClass);
  67.                 } catch (InvalidArgumentException $ex) {
  68.                     // Ignore resources without GET operations
  69.                 }
  70.                 continue;
  71.             }
  72.             foreach ($resourceMetadata as $resource) {
  73.                 if ($resource->getExtraProperties()['is_alternate_resource_metadata'] ?? false) {
  74.                     continue;
  75.                 }
  76.                 foreach ($resource->getOperations() as $operationName => $operation) {
  77.                     $key lcfirst($resource->getShortName());
  78.                     if (!$operation instanceof CollectionOperationInterface || isset($entrypoint[$key])) {
  79.                         continue;
  80.                     }
  81.                     try {
  82.                         $entrypoint[$key] = $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getIriFromResourceClass($resourceClass) : $this->iriConverter->getIriFromResource($resourceClassUrlGeneratorInterface::ABS_PATH$operation);
  83.                     } catch (InvalidArgumentException|OperationNotFoundException $ex) {
  84.                         // Ignore resources without GET operations
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.         ksort($entrypoint);
  90.         return $entrypoint;
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     public function supportsNormalization($data$format null, array $context = []): bool
  96.     {
  97.         return self::FORMAT === $format && $data instanceof Entrypoint;
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function hasCacheableSupportsMethod(): bool
  103.     {
  104.         return true;
  105.     }
  106. }
  107. class_alias(EntrypointNormalizer::class, \ApiPlatform\Core\Hydra\Serializer\EntrypointNormalizer::class);