ApiPlatform\Core\Serializer\AbstractItemNormalizer::denormalizeRelation PHP Method

denormalizeRelation() private method

Denormalizes a relation.
private denormalizeRelation ( string $attributeName, PropertyMetadata $propertyMetadata, string $className, mixed $value, string $format = null, array $context ) : object | null
$attributeName string
$propertyMetadata ApiPlatform\Core\Metadata\Property\PropertyMetadata
$className string
$value mixed
$format string
$context array
return object | null
    private function denormalizeRelation(string $attributeName, PropertyMetadata $propertyMetadata, string $className, $value, string $format = null, array $context)
    {
        if (is_string($value)) {
            try {
                return $this->iriConverter->getItemFromIri($value, $context + ['fetch_data' => false]);
            } catch (InvalidArgumentException $e) {
                // Give a chance to other normalizers (e.g.: DateTimeNormalizer)
            }
        }
        if (!$this->resourceClassResolver->isResourceClass($className) || $propertyMetadata->isWritableLink()) {
            return $this->serializer->denormalize($value, $className, $format, $this->createRelationSerializationContext($className, $context));
        }
        if (!is_array($value)) {
            throw new InvalidArgumentException(sprintf('Expected IRI or nested document for attribute "%s", "%s" given.', $attributeName, gettype($value)));
        }
        throw new InvalidArgumentException(sprintf('Nested documents for attribute "%s" are not allowed. Use IRIs instead.', $attributeName));
    }