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.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function supportsNormalization($data$format null, array $context = []): bool
  57.     {
  58.         return self::FORMAT === $format && $this->decorated->supportsNormalization($data$format$context);
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function hasCacheableSupportsMethod(): bool
  64.     {
  65.         return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      *
  70.      * @return array|string|int|float|bool|\ArrayObject|null
  71.      */
  72.     public function normalize($object$format null, array $context = [])
  73.     {
  74.         if (isset($context['api_resource'])) {
  75.             $originalResource $context['api_resource'];
  76.             unset($context['api_resource']);
  77.         }
  78.         $data $this->decorated->normalize($object$format$context);
  79.         if (!\is_array($data) || isset($context['api_attribute'])) {
  80.             return $data;
  81.         }
  82.         if (isset($originalResource)) {
  83.             $resourceClass $this->resourceClassResolver->getResourceClass($originalResource);
  84.             $resourceData = [
  85.                 'id' => $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getIriFromItem($originalResource) : $this->iriConverter->getIriFromResource($originalResource),
  86.                 'type' => $this->getResourceShortName($resourceClass),
  87.             ];
  88.         } else {
  89.             // Not using an IriConverter here is deprecated in 2.7, avoid spl_object_hash as it may collide
  90.             $resourceData = [
  91.                 'id' => $this->iriConverter instanceof LegacyIriConverterInterface '/.well-known/genid/'.bin2hex(random_bytes(10)) : $this->iriConverter->getIriFromResource($object),
  92.                 'type' => (new \ReflectionClass($this->getObjectClass($object)))->getShortName(),
  93.             ];
  94.         }
  95.         if ($data) {
  96.             $resourceData['attributes'] = $data;
  97.         }
  98.         return ['data' => $resourceData];
  99.     }
  100.     // TODO: 3.0 remove
  101.     private function getResourceShortName(string $resourceClass): string
  102.     {
  103.         /** @var ResourceMetadata|ResourceMetadataCollection */
  104.         $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  105.         if ($resourceMetadata instanceof ResourceMetadata) {
  106.             return $resourceMetadata->getShortName();
  107.         }
  108.         return $resourceMetadata->getOperation()->getShortName();
  109.     }
  110. }
  111. class_alias(ObjectNormalizer::class, \ApiPlatform\Core\JsonApi\Serializer\ObjectNormalizer::class);