/** * {@inheritDoc} * * @param mixed $data * @param object $object * * @return object */ public function denormalize(array $data, $object) { $className = ObjectHelper::getFullClassName($object); $metadata = $this->metadataFactory->getMetadataForClass($className); $unserializedProperties = 0; foreach ($metadata->getProperties() as $property) { if ($this->propertySkipper->shouldSkip($property)) { if ($this->isMediumStrictUnserializeMode() && array_key_exists($property->getSerializedName(), $data)) { throw new InvalidArgumentException(sprintf('%s extra field', $property->getSerializedName())); } continue; } if (!array_key_exists($property->getSerializedName(), $data)) { if ($this->isStrictUnserializeMode()) { throw new InvalidArgumentException(sprintf('%s field is lost', $property->getSerializedName())); } continue; } $value = $this->dataProcessor->denormalizeProcess($this, $data[$property->getSerializedName()], $property, $object); ObjectHelper::involve($object, $property, $value); $unserializedProperties++; } if ($this->isMediumStrictUnserializeMode() && $unserializedProperties !== count($data)) { throw new InvalidArgumentException('Wrong number of fields in the deserialized data'); } return $object; }
public function testShouldSkip() { $this->propertyMetadata->setExpose(true); $this->assertFalse($this->propertySkipper->shouldSkip($this->propertyMetadata)); $skipExclusionGroupSpecification = new GroupsSpecification(array('test1')); $this->propertySkipper->registerSpecification($skipExclusionGroupSpecification); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); $nonSkipExclusionGroupSpecification = new GroupsSpecification(array('test')); $this->propertySkipper->registerSpecification($nonSkipExclusionGroupSpecification); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); $this->propertySkipper->cleanUpSpecifications(); $this->propertySkipper->registerSpecification($nonSkipExclusionGroupSpecification); $this->assertFalse($this->propertySkipper->shouldSkip($this->propertyMetadata)); $skipExclusionVersionSpecification = new VersionSpecification('5.5'); $this->propertySkipper->registerSpecification($skipExclusionVersionSpecification); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); $nonSkipExclusionVersionSpecification = new VersionSpecification('5.1'); $this->propertySkipper->registerSpecification($nonSkipExclusionVersionSpecification); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); $this->propertySkipper->cleanUpSpecifications(); $this->propertySkipper->registerSpecification($nonSkipExclusionGroupSpecification); $this->assertFalse($this->propertySkipper->shouldSkip($this->propertyMetadata)); $this->propertySkipper->registerSpecification($skipExclusionGroupSpecification); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); $this->propertySkipper->cleanUpSpecifications(); $this->assertFalse($this->propertySkipper->shouldSkip($this->propertyMetadata)); $this->propertyMetadata->setExpose(false); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); }