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 <[email protected]>
  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 <[email protected]>
  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.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function supportsNormalization($data$format null, array $context = []): bool
  54.     {
  55.         return self::FORMAT === $format && parent::supportsNormalization($data$format$context);
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      *
  60.      * @throws LogicException
  61.      *
  62.      * @return array|string|int|float|bool|\ArrayObject|null
  63.      */
  64.     public function normalize($object$format null, array $context = [])
  65.     {
  66.         $resourceClass $this->getObjectClass($object);
  67.         if ($outputClass $this->getOutputClass($resourceClass$context) && !isset($context[self::IS_TRANSFORMED_TO_SAME_CLASS])) {
  68.             return parent::normalize($object$format$context);
  69.         }
  70.         // 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
  71.         $previousResourceClass $context['resource_class'] ?? null;
  72.         $metadata = [];
  73.         if ($isResourceClass $this->resourceClassResolver->isResourceClass($resourceClass)) {
  74.             $resourceClass $this->resourceClassResolver->getResourceClass($object$resourceClass);
  75.             $context $this->initContext($resourceClass$context);
  76.             $metadata $this->addJsonLdContext($this->contextBuilder$resourceClass$context);
  77.         } elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
  78.             // We should improve what's behind the context creation, its probably more complicated then it should
  79.             $metadata $this->createJsonLdContext($this->contextBuilder$object$context);
  80.         }
  81.         if (isset($context['operation']) && $previousResourceClass !== $resourceClass) {
  82.             unset($context['operation'], $context['operation_name']);
  83.         }
  84.         if ($this->iriConverter instanceof LegacyIriConverterInterface) {
  85.             $iri $this->iriConverter->getIriFromItem($object);
  86.         } else {
  87.             $iri $this->iriConverter->getIriFromResource($objectUrlGeneratorInterface::ABS_PATH$context['operation'] ?? null$context);
  88.         }
  89.         if (isset($iri)) {
  90.             $context['iri'] = $iri;
  91.             $metadata['@id'] = $iri;
  92.         }
  93.         $context['api_normalize'] = true;
  94.         $data parent::normalize($object$format$context);
  95.         if (!\is_array($data)) {
  96.             return $data;
  97.         }
  98.         // TODO: remove in 3.0
  99.         if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  100.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  101.             $metadata['@type'] = $resourceMetadata->getIri() ?: $resourceMetadata->getShortName();
  102.         } elseif ($this->resourceMetadataFactory) {
  103.             $operation $context['operation'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation();
  104.             $types $operation instanceof HttpOperation $operation->getTypes() : null;
  105.             if (null === $types) {
  106.                 $types = [$operation->getShortName()];
  107.             }
  108.             $metadata['@type'] = === \count($types) ? $types[0] : $types;
  109.         }
  110.         return $metadata $data;
  111.     }
  112.     /**
  113.      * {@inheritdoc}
  114.      */
  115.     public function supportsDenormalization($data$type$format null, array $context = []): bool
  116.     {
  117.         return self::FORMAT === $format && parent::supportsDenormalization($data$type$format$context);
  118.     }
  119.     /**
  120.      * {@inheritdoc}
  121.      *
  122.      * @throws NotNormalizableValueException
  123.      *
  124.      * @return mixed
  125.      */
  126.     public function denormalize($data$class$format null, array $context = [])
  127.     {
  128.         // Avoid issues with proxies if we populated the object
  129.         if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
  130.             if (true !== ($context['api_allow_update'] ?? true)) {
  131.                 throw new NotNormalizableValueException('Update is not allowed for this operation.');
  132.             }
  133.             $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]);
  134.         }
  135.         return parent::denormalize($data$class$format$context);
  136.     }
  137. }
  138. class_alias(ItemNormalizer::class, \ApiPlatform\Core\JsonLd\Serializer\ItemNormalizer::class);