/** * @dataProvider childrenDataProvider * @param AChildren $aChildren * @param array $aChildrenAsArray */ public function testNormalize($aChildren, $aChildrenAsArray) { $object = new E(); $object->setRid(3); $object->setObject($aChildren); $object->setArrayOfObjects(array($aChildren)); $className = ObjectHelper::getFullClassName($object); $metadata = $this->metadataFactory->getMetadataForClass($className); $properties = $metadata->getProperties(); $arrayOfObjectsProperty = $properties['arrayOfObjects']; $data = ObjectHelper::expose($object, $arrayOfObjectsProperty); $normalizedArray = $this->arrayTransformer->normalize($data, $arrayOfObjectsProperty); $this->assertEquals(array($aChildrenAsArray), $normalizedArray); }
/** * {@inheritDoc} * * @param object $object * @return mixed */ public function normalize($object) { $result = array(); $className = ObjectHelper::getFullClassName($object); $metadata = $this->metadataFactory->getMetadataForClass($className); foreach ($metadata->getProperties() as $property) { if ($this->propertySkipper->shouldSkip($property)) { continue; } $value = ObjectHelper::expose($object, $property); $value = $this->dataProcessor->normalizeProcess($this, $value, $property); $result[$property->getSerializedName()] = $value; } return $result; }
/** * @param array $value * @param PropertyMetadata $property * @param mixed $object * @return array */ public function denormalize($value, $property, $object) { $result = array(); $itemProperty = $this->makeItemProperty($property); $existsData = ObjectHelper::expose($object, $property); $inner = false; foreach ($value as $subKey => $subValue) { $tmpObject = $object; if (isset($existsData[$subKey]) && is_object($existsData[$subKey])) { $tmpObject = $existsData[$subKey]; $inner = true; } $result[$subKey] = $this->processor->denormalizeProcess($this->normalizer, $subValue, $itemProperty, $tmpObject, $inner); unset($tmpObject); } return $result; }
/** * @param ArrayNormalizer $normalizer * @param mixed $value * @param PropertyMetadata $property * @param mixed $object * @param bool $inner * @return array|bool|DateTime|float|int|null|string * @throws InvalidArgumentException */ public function denormalizeProcess(ArrayNormalizer $normalizer, $value, $property, $object, $inner = false) { if ($value === null) { return null; } $transformerAliases = array(TransformerFactory::TYPE_SIMPLE_TRANSFORMER, TransformerFactory::TYPE_DATETIME_TRANSFORMER, TransformerFactory::TYPE_ARRAY_TRANSFORMER, TransformerFactory::TYPE_OBJECT_TRANSFORMER); $supports = false; foreach ($transformerAliases as $transformerAlias) { $transformer = $this->transformerFactory->getTransformer($transformerAlias, $normalizer, $this); if ($transformer->supportType($property->getType()) && $transformer->supportValueForDenormalization($value)) { if ($transformerAlias === TransformerFactory::TYPE_OBJECT_TRANSFORMER && !$inner) { $object = ObjectHelper::expose($object, $property); } $value = $transformer->denormalize($value, $property, $object); $supports = true; break; } } if (!$supports && $property->getType() !== null) { throw new InvalidArgumentException(sprintf('Unsupported type: %s', $property->getType())); } return $value; }
/** * @expectedException \Opensoft\SimpleSerializer\Exception\RecursionException */ public function testExposeException() { $stdClass = new \stdClass(); $stdClass->recursion = $stdClass; ObjectHelper::expose($stdClass, new PropertyMetadata('recursion')); }