vendor/api-platform/core/src/JsonLd/Serializer/ItemNormalizer.php line 55

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\JsonLd\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\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
  17. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  18. use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
  19. use ApiPlatform\JsonLd\ContextBuilderInterface;
  20. use ApiPlatform\Metadata\HttpOperation;
  21. use ApiPlatform\Serializer\AbstractItemNormalizer;
  22. use ApiPlatform\Serializer\ContextTrait;
  23. use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
  24. use ApiPlatform\Util\ClassInfoTrait;
  25. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  26. use Symfony\Component\Serializer\Exception\LogicException;
  27. use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
  28. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
  29. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  30. /**
  31.  * Converts between objects and array including JSON-LD and Hydra metadata.
  32.  *
  33.  * @author Kévin Dunglas <dunglas@gmail.com>
  34.  */
  35. final class ItemNormalizer extends AbstractItemNormalizer
  36. {
  37.     use ClassInfoTrait;
  38.     use ContextTrait;
  39.     use JsonLdContextTrait;
  40.     public const FORMAT 'jsonld';
  41.     private $contextBuilder;
  42.     public function __construct($resourceMetadataFactoryPropertyNameCollectionFactoryInterface $propertyNameCollectionFactory$propertyMetadataFactory$iriConverterResourceClassResolverInterface $resourceClassResolverContextBuilderInterface $contextBuilderPropertyAccessorInterface $propertyAccessor nullNameConverterInterface $nameConverter nullClassMetadataFactoryInterface $classMetadataFactory null, array $defaultContext = [], iterable $dataTransformers = [], ResourceAccessCheckerInterface $resourceAccessChecker null)
  43.     {
  44.         parent::__construct($propertyNameCollectionFactory$propertyMetadataFactory$iriConverter$resourceClassResolver$propertyAccessor$nameConverter$classMetadataFactorynullfalse$defaultContext$dataTransformers$resourceMetadataFactory$resourceAccessChecker);
  45.         if ($iriConverter instanceof LegacyIriConverterInterface) {
  46.             trigger_deprecation('api-platform/core''2.7'sprintf('Use an implementation of "%s" instead of "%s".'IriConverterInterface::class, LegacyIriConverterInterface::class));
  47.         }
  48.         $this->contextBuilder $contextBuilder;
  49.     }
  50.     public function supportsNormalization($data$format null, array $context = []): bool
  51.     {
  52.         return self::FORMAT === $format && parent::supportsNormalization($data$format$context);
  53.     }
  54.     /**
  55.      * @param mixed|null $format
  56.      *
  57.      * @throws LogicException
  58.      *
  59.      * @return array|string|int|float|bool|\ArrayObject|null
  60.      */
  61.     public function normalize($object$format null, array $context = [])
  62.     {
  63.         $resourceClass $this->getObjectClass($object);
  64.         if ($outputClass $this->getOutputClass($resourceClass$context) && !isset($context[self::IS_TRANSFORMED_TO_SAME_CLASS])) {
  65.             return parent::normalize($object$format$context);
  66.         }
  67.         // TODO: we should not remove the resource_class in the normalizeRawCollection as we would find out anyway that it's not the same as the requested one
  68.         $previousResourceClass $context['resource_class'] ?? null;
  69.         $metadata = [];
  70.         if ($isResourceClass $this->resourceClassResolver->isResourceClass($resourceClass)) {
  71.             $resourceClass $this->resourceClassResolver->getResourceClass($object$resourceClass);
  72.             $context $this->initContext($resourceClass$context);
  73.             $metadata $this->addJsonLdContext($this->contextBuilder$resourceClass$context);
  74.         } elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
  75.             if ($context['api_collection_sub_level'] ?? false) {
  76.                 unset($context['api_collection_sub_level']);
  77.                 $context['output']['genid'] = true;
  78.                 $context['output']['iri'] = null;
  79.             }
  80.             // We should improve what's behind the context creation, its probably more complicated then it should
  81.             $metadata $this->createJsonLdContext($this->contextBuilder$object$context);
  82.         }
  83.         if (isset($context['operation']) && $previousResourceClass !== $resourceClass) {
  84.             unset($context['operation'], $context['operation_name']);
  85.         }
  86.         if ($this->iriConverter instanceof LegacyIriConverterInterface) {
  87.             $iri $this->iriConverter->getIriFromItem($object);
  88.         } else {
  89.             $iri $this->iriConverter->getIriFromResource($objectUrlGeneratorInterface::ABS_PATH$context['operation'] ?? null$context);
  90.         }
  91.         if (isset($iri)) {
  92.             $context['iri'] = $iri;
  93.             $metadata['@id'] = $iri;
  94.         }
  95.         $context['api_normalize'] = true;
  96.         $data parent::normalize($object$format$context);
  97.         if (!\is_array($data)) {
  98.             return $data;
  99.         }
  100.         // TODO: remove in 3.0
  101.         if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  102.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  103.             $metadata['@type'] = $resourceMetadata->getIri() ?: $resourceMetadata->getShortName();
  104.         } elseif ($this->resourceMetadataFactory) {
  105.             $operation $context['operation'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation();
  106.             $types $operation instanceof HttpOperation $operation->getTypes() : null;
  107.             if (null === $types) {
  108.                 $types = [$operation->getShortName()];
  109.             }
  110.             $metadata['@type'] = === \count($types) ? $types[0] : $types;
  111.         }
  112.         return $metadata $data;
  113.     }
  114.     public function supportsDenormalization($data$type$format null, array $context = []): bool
  115.     {
  116.         return self::FORMAT === $format && parent::supportsDenormalization($data$type$format$context);
  117.     }
  118.     /**
  119.      * @param mixed|null $format
  120.      *
  121.      * @throws NotNormalizableValueException
  122.      */
  123.     public function denormalize($data$class$format null, array $context = [])
  124.     {
  125.         // Avoid issues with proxies if we populated the object
  126.         if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
  127.             if (true !== ($context['api_allow_update'] ?? true)) {
  128.                 throw new NotNormalizableValueException('Update is not allowed for this operation.');
  129.             }
  130.             $context[self::OBJECT_TO_POPULATE] = $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getItemFromIri($data['@id'], $context + ['fetch_data' => true]) : $this->iriConverter->getResourceFromIri($data['@id'], $context + ['fetch_data' => true]);
  131.         }
  132.         return parent::denormalize($data$class$format$context);
  133.     }
  134. }
  135. class_alias(ItemNormalizer::class, \ApiPlatform\Core\JsonLd\Serializer\ItemNormalizer::class);