ApiPlatform\Core\Serializer\ItemNormalizer::denormalize PHP Method

denormalize() public method

public denormalize ( $data, $class, $format = null, array $context = [] )
$context array
    public function denormalize($data, $class, $format = null, array $context = [])
    {
        // Avoid issues with proxies if we populated the object
        if (isset($data['id']) && !isset($context['object_to_populate'])) {
            if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
                throw new InvalidArgumentException('Update is not allowed for this operation.');
            }
            $context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['id'], $context + ['fetch_data' => false]);
        }
        return parent::denormalize($data, $class, $format, $context);
    }

Usage Example

Example #1
0
 /**
  * @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
  * @expectedExceptionMessage Update is not allowed for this operation.
  */
 public function testDenormalizeWithIdAndUpdateNotAllowed()
 {
     $context = ['resource_class' => Dummy::class, 'api_allow_update' => false];
     $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
     $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
     $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
     $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
     $serializerProphecy = $this->prophesize(SerializerInterface::class);
     $serializerProphecy->willImplement(DenormalizerInterface::class);
     $normalizer = new ItemNormalizer($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal());
     $normalizer->setSerializer($serializerProphecy->reveal());
     $normalizer->denormalize(['id' => '/dummies/12', 'name' => 'hello'], Dummy::class, null, $context);
 }
ItemNormalizer