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

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\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
  16. use ApiPlatform\Core\Api\OperationType;
  17. use ApiPlatform\JsonLd\ContextBuilderInterface;
  18. use ApiPlatform\JsonLd\Serializer\JsonLdContextTrait;
  19. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  20. use ApiPlatform\Serializer\ContextTrait;
  21. use ApiPlatform\Serializer\OperationContextTrait;
  22. use ApiPlatform\State\Pagination\PaginatorInterface;
  23. use ApiPlatform\State\Pagination\PartialPaginatorInterface;
  24. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  25. use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
  26. use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
  27. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  28. /**
  29.  * This normalizer handles collections.
  30.  *
  31.  * @author Kevin Dunglas <dunglas@gmail.com>
  32.  * @author Samuel ROZE <samuel.roze@gmail.com>
  33.  */
  34. final class CollectionNormalizer implements NormalizerInterfaceNormalizerAwareInterfaceCacheableSupportsMethodInterface
  35. {
  36.     use ContextTrait;
  37.     use JsonLdContextTrait;
  38.     use NormalizerAwareTrait;
  39.     use OperationContextTrait;
  40.     public const FORMAT 'jsonld';
  41.     public const IRI_ONLY 'iri_only';
  42.     private $contextBuilder;
  43.     private $resourceClassResolver;
  44.     private $iriConverter;
  45.     private $resourceMetadataCollectionFactory;
  46.     private $defaultContext = [
  47.         self::IRI_ONLY => false,
  48.     ];
  49.     public function __construct(ContextBuilderInterface $contextBuilderResourceClassResolverInterface $resourceClassResolver$iriConverterResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory null, array $defaultContext = [])
  50.     {
  51.         $this->contextBuilder $contextBuilder;
  52.         $this->resourceClassResolver $resourceClassResolver;
  53.         if ($iriConverter instanceof LegacyIriConverterInterface) {
  54.             trigger_deprecation('api-platform/core''2.7'sprintf('Use an implementation of "%s" instead of "%s".'IriConverterInterface::class, LegacyIriConverterInterface::class));
  55.         }
  56.         $this->iriConverter $iriConverter;
  57.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  58.         $this->defaultContext array_merge($this->defaultContext$defaultContext);
  59.     }
  60.     public function supportsNormalization($data$format null, array $context = []): bool
  61.     {
  62.         return self::FORMAT === $format && is_iterable($data);
  63.     }
  64.     /**
  65.      * @param iterable   $object
  66.      * @param mixed|null $format
  67.      */
  68.     public function normalize($object$format null, array $context = []): array
  69.     {
  70.         if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
  71.             return $this->normalizeRawCollection($object$format$context);
  72.         }
  73.         $resourceClass $this->resourceClassResolver->getResourceClass($object$context['resource_class']);
  74.         $context $this->initContext($resourceClass$context);
  75.         $context['api_collection_sub_level'] = true;
  76.         $data $this->addJsonLdContext($this->contextBuilder$resourceClass$context);
  77.         if ($this->iriConverter instanceof LegacyIriConverterInterface) {
  78.             // TODO: remove in 3.0
  79.             $data['@id'] = isset($context['operation_type']) && OperationType::SUBRESOURCE === $context['operation_type'] ? $this->iriConverter->getSubresourceIriFromResourceClass($resourceClass$context) : $this->iriConverter->getIriFromResourceClass($resourceClass);
  80.         } else {
  81.             $data['@id'] = $this->iriConverter->getIriFromResource($resourceClassUrlGeneratorInterface::ABS_PATH$context['operation'] ?? null$context);
  82.         }
  83.         $data['@type'] = 'hydra:Collection';
  84.         $data['hydra:member'] = [];
  85.         $iriOnly $context[self::IRI_ONLY] ?? $this->defaultContext[self::IRI_ONLY];
  86.         $childContext $this->createOperationContext($context$resourceClass);
  87.         foreach ($object as $obj) {
  88.             if ($iriOnly) {
  89.                 $data['hydra:member'][] = $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getIriFromItem($obj) : $this->iriConverter->getIriFromResource($obj);
  90.             } else {
  91.                 $data['hydra:member'][] = $this->normalizer->normalize($obj$format$childContext);
  92.             }
  93.         }
  94.         if ($object instanceof PaginatorInterface) {
  95.             $data['hydra:totalItems'] = $object->getTotalItems();
  96.         }
  97.         if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) {
  98.             $data['hydra:totalItems'] = \count($object);
  99.         }
  100.         return $data;
  101.     }
  102.     public function hasCacheableSupportsMethod(): bool
  103.     {
  104.         return true;
  105.     }
  106.     /**
  107.      * Normalizes a raw collection (not API resources).
  108.      */
  109.     private function normalizeRawCollection(iterable $object, ?string $format, array $context): array
  110.     {
  111.         $data = [];
  112.         foreach ($object as $index => $obj) {
  113.             $data[$index] = $this->normalizer->normalize($obj$format$context);
  114.         }
  115.         return $data;
  116.     }
  117. }
  118. class_alias(CollectionNormalizer::class, \ApiPlatform\Core\Hydra\Serializer\CollectionNormalizer::class);