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

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