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

  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\Exception\InvalidArgumentException;
  16. use ApiPlatform\Exception\OperationNotFoundException;
  17. use ApiPlatform\Metadata\CollectionOperationInterface;
  18. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  19. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  20. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  21. /**
  22.  * Normalizes the API entrypoint.
  23.  *
  24.  * @author Kévin Dunglas <dunglas@gmail.com>
  25.  */
  26. final class EntrypointNormalizer implements NormalizerInterfaceCacheableSupportsMethodInterface
  27. {
  28.     public const FORMAT 'jsonld';
  29.     public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly IriConverterInterface $iriConverter, private readonly UrlGeneratorInterface $urlGenerator)
  30.     {
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function normalize(mixed $objectstring $format null, array $context = []): array
  36.     {
  37.         $entrypoint = [
  38.             '@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Entrypoint']),
  39.             '@id' => $this->urlGenerator->generate('api_entrypoint'),
  40.             '@type' => 'Entrypoint',
  41.         ];
  42.         foreach ($object->getResourceNameCollection() as $resourceClass) {
  43.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  44.             foreach ($resourceMetadata as $resource) {
  45.                 if ($resource->getExtraProperties()['is_alternate_resource_metadata'] ?? false) {
  46.                     continue;
  47.                 }
  48.                 foreach ($resource->getOperations() as $operation) {
  49.                     $key lcfirst($resource->getShortName());
  50.                     if (!$operation instanceof CollectionOperationInterface || isset($entrypoint[$key])) {
  51.                         continue;
  52.                     }
  53.                     try {
  54.                         $entrypoint[$key] = $this->iriConverter->getIriFromResource($resourceClassUrlGeneratorInterface::ABS_PATH$operation); // @phpstan-ignore-line phpstan issue as type is CollectionOperationInterface & Operation
  55.                     } catch (InvalidArgumentException|OperationNotFoundException) {
  56.                         // Ignore resources without GET operations
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.         ksort($entrypoint);
  62.         return $entrypoint;
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public function supportsNormalization(mixed $datastring $format null, array $context = []): bool
  68.     {
  69.         return self::FORMAT === $format && $data instanceof Entrypoint;
  70.     }
  71.     public function hasCacheableSupportsMethod(): bool
  72.     {
  73.         return true;
  74.     }
  75. }