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

Open in your IDE?
  1.      */
  2.     public function denormalize($data$class$format null, array $context = [])
  3.     {
  4.         // Avoid issues with proxies if we populated the object
  5.         if (isset($data['id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
  6.             if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
  7.                 throw new NotNormalizableValueException('Update is not allowed for this operation.');
  8.             }
  9.             if (isset($context['resource_class'])) {
  10.                 $this->updateObjectToPopulate($data$context);
  11.             } else {
  12.                 // See https://github.com/api-platform/core/pull/2326 to understand this message.
  13.                 $this->logger->warning('The "resource_class" key is missing from the context.', [
  14.                     'context' => $context,
  15.                 ]);
  16.             }
  17.         }
  18.         return parent::denormalize($data$class$format$context);
  19.     }
  20.     private function updateObjectToPopulate(array $data, array &$context): void
  21.     {
  22.         try {
  23.             $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]);
  24.         } catch (InvalidArgumentException $e) {
  25.             if ($this->iriConverter instanceof LegacyIriConverterInterface) {
  26.                 // remove in 3.0
  27.                 $identifier null;
  28.                 $options $this->getFactoryOptions($context);
  29.                 foreach ($this->propertyNameCollectionFactory->create($context['resource_class'], $options) as $propertyName) {
  30.                     if (true === $this->propertyMetadataFactory->create($context['resource_class'], $propertyName)->isIdentifier()) {
  31.                         $identifier $propertyName;
  32.                         break;
  33.                     }
  34.                 }
  35.                 if (null === $identifier) {
  36.                     throw $e;
  37.                 }
  38.                 $iri sprintf('%s/%s'$this->iriConverter->getIriFromResourceClass($context['resource_class']), $data[$identifier]);
  39.             } else {
  40.                 $operation $this->resourceMetadataFactory->create($context['resource_class'])->getOperation();
  41.                 // todo: we could guess uri variables with the operation and the data instead of hardcoding id
  42.                 $iri $this->iriConverter->getIriFromResource($context['resource_class'], UrlGeneratorInterface::ABS_PATH$operation, ['uri_variables' => ['id' => $data['id']]]);
  43.             }
  44.             $context[self::OBJECT_TO_POPULATE] = $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getItemFromIri($iri, ['fetch_data' => true]) : $this->iriConverter->getResourceFromIri($iri, ['fetch_data' => true]);
  45.         }
  46.     }
  47. }
  48. class_alias(ItemNormalizer::class, \ApiPlatform\Core\Serializer\ItemNormalizer::class);