vendor/api-platform/core/src/Hal/Serializer/EntrypointNormalizer.php line 58

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\Hal\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\Metadata\CollectionOperationInterface;
  20. use ApiPlatform\Metadata\Operation;
  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 'jsonhal';
  33.     /**
  34.      * @var ResourceMetadataFactoryInterface|ResourceMetadataCollectionFactoryInterface
  35.      */
  36.     private $resourceMetadataFactory;
  37.     /** @var LegacyIriConverterInterface|IriConverterInterface */
  38.     private $iriConverter;
  39.     private $urlGenerator;
  40.     public function __construct($resourceMetadataFactory$iriConverterUrlGeneratorInterface $urlGenerator)
  41.     {
  42.         $this->resourceMetadataFactory $resourceMetadataFactory;
  43.         $this->iriConverter $iriConverter;
  44.         $this->urlGenerator $urlGenerator;
  45.         if ($iriConverter instanceof LegacyIriConverterInterface) {
  46.             trigger_deprecation('api-platform/core''2.7'sprintf('Use an implementation of "%s" instead of "%s".'IriConverterInterface::class, LegacyIriConverterInterface::class));
  47.         }
  48.         if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  49.             trigger_deprecation('api-platform/core''2.7'sprintf('Use "%s" instead of "%s".'ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  50.         }
  51.     }
  52.     public function normalize($object$format null, array $context = []): array
  53.     {
  54.         $entrypoint = ['_links' => ['self' => ['href' => $this->urlGenerator->generate('api_entrypoint')]]];
  55.         foreach ($object->getResourceNameCollection() as $resourceClass) {
  56.             /** @var ResourceMetadata|ResourceMetadataCollection */
  57.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  58.             if ($resourceMetadata instanceof ResourceMetadata) {
  59.                 if (!$resourceMetadata->getCollectionOperations()) {
  60.                     continue;
  61.                 }
  62.                 try {
  63.                     $entrypoint['_links'][lcfirst($resourceMetadata->getShortName())]['href'] = $this->iriConverter->getIriFromResourceClass($resourceClass);
  64.                 } catch (InvalidArgumentException $ex) {
  65.                     // Ignore resources without GET operations
  66.                 }
  67.             }
  68.             foreach ($resourceMetadata as $resource) {
  69.                 foreach ($resource->getOperations() as $operationName => $operation) {
  70.                     /** @var Operation $operation */
  71.                     if (!$operation instanceof CollectionOperationInterface) {
  72.                         continue;
  73.                     }
  74.                     try {
  75.                         $href $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getIriFromResourceClass($resourceClass) : $this->iriConverter->getIriFromResource($operation->getClass(), UrlGeneratorInterface::ABS_PATH$operation);
  76.                         $entrypoint['_links'][lcfirst($operation->getShortName())]['href'] = $href;
  77.                     } catch (InvalidArgumentException $ex) {
  78.                         // Ignore resources without GET operations
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.         return $entrypoint;
  84.     }
  85.     public function supportsNormalization($data$format null, array $context = []): bool
  86.     {
  87.         return self::FORMAT === $format && $data instanceof Entrypoint;
  88.     }
  89.     public function hasCacheableSupportsMethod(): bool
  90.     {
  91.         return true;
  92.     }
  93. }
  94. class_alias(EntrypointNormalizer::class, \ApiPlatform\Core\Hal\Serializer\EntrypointNormalizer::class);