vendor/api-platform/core/src/JsonApi/Serializer/ObjectNormalizer.php line 60

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\IriConverterInterface;
  13. use ApiPlatform\Api\ResourceClassResolverInterface;
  14. use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
  15. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  16. use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
  17. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  18. use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
  19. use ApiPlatform\Util\ClassInfoTrait;
  20. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  21. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  22. /**
  23.  * Decorates the output with JSON API metadata when appropriate, but otherwise
  24.  * just passes through to the decorated normalizer.
  25.  */
  26. final class ObjectNormalizer implements NormalizerInterfaceCacheableSupportsMethodInterface
  27. {
  28.     use ClassInfoTrait;
  29.     public const FORMAT 'jsonapi';
  30.     private $decorated;
  31.     /**
  32.      * @var IriConverterInterface|LegacyIriConverterInterface
  33.      */
  34.     private $iriConverter;
  35.     private $resourceClassResolver;
  36.     /**
  37.      * @var ResourceMetadataFactoryInterface|ResourceMetadataCollectionFactoryInterface
  38.      */
  39.     private $resourceMetadataFactory;
  40.     public function __construct(NormalizerInterface $decorated$iriConverterResourceClassResolverInterface $resourceClassResolver$resourceMetadataFactory)
  41.     {
  42.         $this->decorated $decorated;
  43.         $this->iriConverter $iriConverter;
  44.         $this->resourceClassResolver $resourceClassResolver;
  45.         $this->resourceMetadataFactory $resourceMetadataFactory;
  46.         if ($iriConverter instanceof LegacyIriConverterInterface) {
  47.             trigger_deprecation('api-platform/core''2.7'sprintf('Use an implementation of "%s" instead of "%s".'IriConverterInterface::class, LegacyIriConverterInterface::class));
  48.         }
  49.         if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  50.             trigger_deprecation('api-platform/core''2.7'sprintf('Use "%s" instead of "%s".'ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  51.         }
  52.     }
  53.     public function supportsNormalization($data$format null, array $context = []): bool
  54.     {
  55.         return self::FORMAT === $format && $this->decorated->supportsNormalization($data$format$context);
  56.     }
  57.     public function hasCacheableSupportsMethod(): bool
  58.     {
  59.         return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
  60.     }
  61.     /**
  62.      * @param mixed|null $format
  63.      *
  64.      * @return array|string|int|float|bool|\ArrayObject|null
  65.      */
  66.     public function normalize($object$format null, array $context = [])
  67.     {
  68.         if (isset($context['api_resource'])) {
  69.             $originalResource $context['api_resource'];
  70.             unset($context['api_resource']);
  71.         }
  72.         $data $this->decorated->normalize($object$format$context);
  73.         if (!\is_array($data) || isset($context['api_attribute'])) {
  74.             return $data;
  75.         }
  76.         if (isset($originalResource)) {
  77.             $resourceClass $this->resourceClassResolver->getResourceClass($originalResource);
  78.             $resourceData = [
  79.                 'id' => $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getIriFromItem($originalResource) : $this->iriConverter->getIriFromResource($originalResource),
  80.                 'type' => $this->getResourceShortName($resourceClass),
  81.             ];
  82.         } else {
  83.             // Not using an IriConverter here is deprecated in 2.7, avoid spl_object_hash as it may collide
  84.             $resourceData = [
  85.                 'id' => $this->iriConverter instanceof LegacyIriConverterInterface '/.well-known/genid/'.bin2hex(random_bytes(10)) : $this->iriConverter->getIriFromResource($object),
  86.                 'type' => (new \ReflectionClass($this->getObjectClass($object)))->getShortName(),
  87.             ];
  88.         }
  89.         if ($data) {
  90.             $resourceData['attributes'] = $data;
  91.         }
  92.         return ['data' => $resourceData];
  93.     }
  94.     // TODO: 3.0 remove
  95.     private function getResourceShortName(string $resourceClass): string
  96.     {
  97.         /** @var ResourceMetadata|ResourceMetadataCollection */
  98.         $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  99.         if ($resourceMetadata instanceof ResourceMetadata) {
  100.             return $resourceMetadata->getShortName();
  101.         }
  102.         return $resourceMetadata->getOperation()->getShortName();
  103.     }
  104. }
  105. class_alias(ObjectNormalizer::class, \ApiPlatform\Core\JsonApi\Serializer\ObjectNormalizer::class);