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

  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\Serializer;
  12. use ApiPlatform\Api\IriConverterInterface;
  13. use ApiPlatform\Api\UrlGeneratorInterface;
  14. use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
  15. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  16. use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
  17. use ApiPlatform\Exception\InvalidArgumentException;
  18. use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
  19. use Psr\Log\LoggerInterface;
  20. use Psr\Log\NullLogger;
  21. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  22. use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
  23. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
  24. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  25. /**
  26.  * Generic item normalizer.
  27.  *
  28.  * @author Kévin Dunglas <dunglas@gmail.com>
  29.  */
  30. class ItemNormalizer extends AbstractItemNormalizer
  31. {
  32.     private $logger;
  33.     /**
  34.      * @param LegacyIriConverterInterface|IriConverterInterface $iriConverter
  35.      * @param mixed|null                                        $resourceMetadataFactory
  36.      */
  37.     public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory$propertyMetadataFactory$iriConverter$resourceClassResolverPropertyAccessorInterface $propertyAccessor nullNameConverterInterface $nameConverter nullClassMetadataFactoryInterface $classMetadataFactory nullItemDataProviderInterface $itemDataProvider nullbool $allowPlainIdentifiers falseLoggerInterface $logger nulliterable $dataTransformers = [], $resourceMetadataFactory nullResourceAccessCheckerInterface $resourceAccessChecker null, array $defaultContext = [])
  38.     {
  39.         parent::__construct($propertyNameCollectionFactory$propertyMetadataFactory$iriConverter$resourceClassResolver$propertyAccessor$nameConverter$classMetadataFactory$itemDataProvider$allowPlainIdentifiers$defaultContext$dataTransformers$resourceMetadataFactory$resourceAccessChecker);
  40.         $this->logger $logger ?: new NullLogger();
  41.     }
  42.     /**
  43.      * @param mixed|null $format
  44.      *
  45.      * @throws NotNormalizableValueException
  46.      */
  47.     public function denormalize($data$class$format null, array $context = [])
  48.     {
  49.         // Avoid issues with proxies if we populated the object
  50.         if (isset($data['id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
  51.             if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
  52.                 throw new NotNormalizableValueException('Update is not allowed for this operation.');
  53.             }
  54.             if (isset($context['resource_class'])) {
  55.                 $this->updateObjectToPopulate($data$context);
  56.             } else {
  57.                 // See https://github.com/api-platform/core/pull/2326 to understand this message.
  58.                 $this->logger->warning('The "resource_class" key is missing from the context.', [
  59.                     'context' => $context,
  60.                 ]);
  61.             }
  62.         }
  63.         return parent::denormalize($data$class$format$context);
  64.     }
  65.     private function updateObjectToPopulate(array $data, array &$context): void
  66.     {
  67.         try {
  68.             $context[self::OBJECT_TO_POPULATE] = $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getItemFromIri((string) $data['id'], $context + ['fetch_data' => true]) : $this->iriConverter->getResourceFromIri((string) $data['id'], $context + ['fetch_data' => true]);
  69.         } catch (InvalidArgumentException $e) {
  70.             if ($this->iriConverter instanceof LegacyIriConverterInterface) {
  71.                 // remove in 3.0
  72.                 $identifier null;
  73.                 $options $this->getFactoryOptions($context);
  74.                 foreach ($this->propertyNameCollectionFactory->create($context['resource_class'], $options) as $propertyName) {
  75.                     if (true === $this->propertyMetadataFactory->create($context['resource_class'], $propertyName)->isIdentifier()) {
  76.                         $identifier $propertyName;
  77.                         break;
  78.                     }
  79.                 }
  80.                 if (null === $identifier) {
  81.                     throw $e;
  82.                 }
  83.                 $iri sprintf('%s/%s'$this->iriConverter->getIriFromResourceClass($context['resource_class']), $data[$identifier]);
  84.             } else {
  85.                 $operation $this->resourceMetadataFactory->create($context['resource_class'])->getOperation();
  86.                 // todo: we could guess uri variables with the operation and the data instead of hardcoding id
  87.                 $iri $this->iriConverter->getIriFromResource($context['resource_class'], UrlGeneratorInterface::ABS_PATH$operation, ['uri_variables' => ['id' => $data['id']]]);
  88.             }
  89.             $context[self::OBJECT_TO_POPULATE] = $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getItemFromIri($iri, ['fetch_data' => true]) : $this->iriConverter->getResourceFromIri($iri, ['fetch_data' => true]);
  90.         }
  91.     }
  92. }
  93. class_alias(ItemNormalizer::class, \ApiPlatform\Core\Serializer\ItemNormalizer::class);