/** * {@inheritDoc} * * @param mixed $data * @param object $object */ public function toObject(array $data, $object) { $className = $this->getFullClassName($object); $metadata = $this->metadataFactory->getMetadataForClass($className); $unserializedProperties = 0; foreach ($metadata->getProperties() as $property) { if (!$property->isExpose() || $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->handleValue($data[$property->getSerializedName()], $property, self::DIRECTION_UNSERIALIZE, $object); if ($value === $object) { throw new RecursionException(sprintf('Invalid self reference detected. %s::%s', $className, $property->getName())); } $this->involveValue($object, $property, $value); $unserializedProperties++; } if ($this->isMediumStrictUnserializeMode() && $unserializedProperties !== count($data)) { throw new InvalidArgumentException('Wrong number of fields in the deserialized data'); } return $object; }
/** * {@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; }
/** * @depends testGetMetadataForClass */ public function testGetMetadataForClassFileCache() { $unitUnderTest = new MetadataFactory($this->driver); $unitUnderTest->setCache($this->cache); $unitUnderTestReadCache = new MetadataFactory($this->driver); $unitUnderTestReadCache->setCache($this->cache); $unitUnderTest->getMetadataForClass('Opensoft\\SimpleSerializer\\Tests\\Metadata\\Driver\\Fixture\\A\\A'); $result = $unitUnderTestReadCache->getMetadataForClass('Opensoft\\SimpleSerializer\\Tests\\Metadata\\Driver\\Fixture\\A\\A'); //die; $this->assertInstanceOf('Opensoft\\SimpleSerializer\\Metadata\\ClassMetadata', $result); $properties = $result->getProperties(); $this->assertCount(4, $properties); $this->assertCount(1, $result->getFileResources()); $this->assertEquals('rid', $properties['rid']->getName()); $this->assertEquals('id', $properties['rid']->getSerializedName()); $this->assertEquals('integer', $properties['rid']->getType()); $this->assertTrue($properties['rid']->isExpose()); $this->assertEquals('name', $properties['name']->getName()); $this->assertEquals('name', $properties['name']->getSerializedName()); $this->assertEquals('string', $properties['name']->getType()); $this->assertTrue($properties['name']->isExpose()); $this->assertEquals('status', $properties['status']->getName()); $this->assertEquals('status', $properties['status']->getSerializedName()); $this->assertEquals('boolean', $properties['status']->getType()); $this->assertTrue($properties['status']->isExpose()); $this->assertEquals('hiddenStatus', $properties['hiddenStatus']->getName()); $this->assertEquals('hiddenStatus', $properties['hiddenStatus']->getSerializedName()); $this->assertEquals('boolean', $properties['hiddenStatus']->getType()); $this->assertFalse($properties['hiddenStatus']->isExpose()); }