vendor/api-platform/core/src/Hydra/Serializer/CollectionNormalizer.php line 67

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\IriConverterInterface;
  13. use ApiPlatform\Api\ResourceClassResolverInterface;
  14. use ApiPlatform\Api\UrlGeneratorInterface;
  15. use ApiPlatform\JsonLd\ContextBuilderInterface;
  16. use ApiPlatform\JsonLd\Serializer\JsonLdContextTrait;
  17. use ApiPlatform\Metadata\CollectionOperationInterface;
  18. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  19. use ApiPlatform\Serializer\ContextTrait;
  20. use ApiPlatform\State\Pagination\PaginatorInterface;
  21. use ApiPlatform\State\Pagination\PartialPaginatorInterface;
  22. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  23. use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
  24. use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
  25. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  26. /**
  27.  * This normalizer handles collections.
  28.  *
  29.  * @author Kevin Dunglas <dunglas@gmail.com>
  30.  * @author Samuel ROZE <samuel.roze@gmail.com>
  31.  */
  32. final class CollectionNormalizer implements NormalizerInterfaceNormalizerAwareInterfaceCacheableSupportsMethodInterface
  33. {
  34.     use ContextTrait;
  35.     use JsonLdContextTrait;
  36.     use NormalizerAwareTrait;
  37.     public const FORMAT 'jsonld';
  38.     public const IRI_ONLY 'iri_only';
  39.     private array $defaultContext = [
  40.         self::IRI_ONLY => false,
  41.     ];
  42.     public function __construct(private readonly ContextBuilderInterface $contextBuilder, private readonly ResourceClassResolverInterface $resourceClassResolver, private readonly IriConverterInterface $iriConverter, private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory null, array $defaultContext = [])
  43.     {
  44.         $this->defaultContext array_merge($this->defaultContext$defaultContext);
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function supportsNormalization(mixed $datastring $format null, array $context = []): bool
  50.     {
  51.         return self::FORMAT === $format && is_iterable($data) && isset($context['resource_class']) && !isset($context['api_sub_level']);
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      *
  56.      * @param iterable $object
  57.      */
  58.     public function normalize(mixed $objectstring $format null, array $context = []): array
  59.     {
  60.         $resourceClass $this->resourceClassResolver->getResourceClass($object$context['resource_class']);
  61.         $context $this->initContext($resourceClass$context);
  62.         $context['api_collection_sub_level'] = true;
  63.         $data $this->addJsonLdContext($this->contextBuilder$resourceClass$context);
  64.         $data['@id'] = $this->iriConverter->getIriFromResource($resourceClassUrlGeneratorInterface::ABS_PATH$context['operation'] ?? null$context);
  65.         $data['@type'] = 'hydra:Collection';
  66.         $data['hydra:member'] = [];
  67.         $iriOnly $context[self::IRI_ONLY] ?? $this->defaultContext[self::IRI_ONLY];
  68.         if ($this->resourceMetadataCollectionFactory && ($operation $context['operation'] ?? null) instanceof CollectionOperationInterface && method_exists($operation'getItemUriTemplate') && ($itemUriTemplate $operation->getItemUriTemplate())) {
  69.             $context['operation'] = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($operation->getItemUriTemplate());
  70.         } else {
  71.             unset($context['operation']);
  72.         }
  73.         unset($context['operation_name'], $context['uri_variables']);
  74.         foreach ($object as $obj) {
  75.             if ($iriOnly) {
  76.                 $data['hydra:member'][] = $this->iriConverter->getIriFromResource($obj);
  77.             } else {
  78.                 $data['hydra:member'][] = $this->normalizer->normalize($obj$format$context);
  79.             }
  80.         }
  81.         if ($object instanceof PaginatorInterface) {
  82.             $data['hydra:totalItems'] = $object->getTotalItems();
  83.         }
  84.         if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) {
  85.             $data['hydra:totalItems'] = \count($object);
  86.         }
  87.         return $data;
  88.     }
  89.     public function hasCacheableSupportsMethod(): bool
  90.     {
  91.         return false;
  92.     }
  93. }