vendor/api-platform/core/src/JsonApi/Serializer/EntrypointNormalizer.php line 55

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\JsonApi\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\HttpOperation;
  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 Amrouche Hamza <hamza.simperfit@gmail.com>
  29.  * @author Kévin Dunglas <dunglas@gmail.com>
  30.  */
  31. final class EntrypointNormalizer implements NormalizerInterfaceCacheableSupportsMethodInterface
  32. {
  33.     public const FORMAT 'jsonapi';
  34.     private $resourceMetadataFactory;
  35.     private $iriConverter;
  36.     private $urlGenerator;
  37.     public function __construct($resourceMetadataFactory$iriConverterUrlGeneratorInterface $urlGenerator)
  38.     {
  39.         $this->resourceMetadataFactory $resourceMetadataFactory;
  40.         $this->iriConverter $iriConverter;
  41.         $this->urlGenerator $urlGenerator;
  42.         if ($iriConverter instanceof LegacyIriConverterInterface) {
  43.             trigger_deprecation('api-platform/core''2.7'sprintf('Use an implementation of "%s" instead of "%s".'IriConverterInterface::class, LegacyIriConverterInterface::class));
  44.         }
  45.         if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  46.             trigger_deprecation('api-platform/core''2.7'sprintf('Use "%s" instead of "%s".'ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  47.         }
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      *
  52.      * @return array|string|int|float|bool|\ArrayObject|null
  53.      */
  54.     public function normalize($object$format null, array $context = [])
  55.     {
  56.         $entrypoint = ['links' => ['self' => $this->urlGenerator->generate('api_entrypoint', [], UrlGeneratorInterface::ABS_URL)]];
  57.         foreach ($object->getResourceNameCollection() as $resourceClass) {
  58.             /** @var ResourceMetadata|ResourceMetadataCollection */
  59.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  60.             if ($resourceMetadata instanceof ResourceMetadata) {
  61.                 if (!$resourceMetadata->getCollectionOperations()) {
  62.                     continue;
  63.                 }
  64.                 try {
  65.                     $entrypoint['links'][lcfirst($resourceMetadata->getShortName())] = $this->iriConverter->getIriFromResourceClass($resourceClassUrlGeneratorInterface::ABS_URL);
  66.                 } catch (InvalidArgumentException $ex) {
  67.                     // Ignore resources without GET operations
  68.                 }
  69.             }
  70.             foreach ($resourceMetadata as $resource) {
  71.                 foreach ($resource->getOperations() as $operationName => $operation) {
  72.                     if (!$operation instanceof CollectionOperationInterface || ($operation instanceof HttpOperation && $operation->getUriVariables())) {
  73.                         continue;
  74.                     }
  75.                     try {
  76.                         if ($this->iriConverter instanceof LegacyIriConverterInterface) {
  77.                             $iri $this->iriConverter->getIriFromResourceClass($resourceClassUrlGeneratorInterface::ABS_URL);
  78.                         } else {
  79.                             $iri $this->iriConverter->getIriFromResource($resourceClassUrlGeneratorInterface::ABS_URL$operation);
  80.                         }
  81.                         $entrypoint['links'][lcfirst($resource->getShortName())] = $iri;
  82.                     } catch (InvalidArgumentException $ex) {
  83.                         // Ignore resources without GET operations
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.         return $entrypoint;
  89.     }
  90.     /**
  91.      * {@inheritdoc}
  92.      */
  93.     public function supportsNormalization($data$format null, array $context = []): bool
  94.     {
  95.         return self::FORMAT === $format && $data instanceof Entrypoint;
  96.     }
  97.     /**
  98.      * {@inheritdoc}
  99.      */
  100.     public function hasCacheableSupportsMethod(): bool
  101.     {
  102.         return true;
  103.     }
  104. }
  105. class_alias(EntrypointNormalizer::class, \ApiPlatform\Core\JsonApi\Serializer\EntrypointNormalizer::class);