Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Serializer\Normalizer; Chris@0: Chris@0: /** Chris@0: * Converts between objects and arrays by mapping properties. Chris@0: * Chris@0: * The normalization process looks for all the object's properties (public and private). Chris@0: * The result is a map from property names to property values. Property values Chris@0: * are normalized through the serializer. Chris@0: * Chris@0: * The denormalization first looks at the constructor of the given class to see Chris@0: * if any of the parameters have the same name as one of the properties. The Chris@0: * constructor is then called with all parameters or an exception is thrown if Chris@0: * any required parameters were not present as properties. Then the denormalizer Chris@0: * walks through the given map of property names to property values to see if a Chris@0: * property with the corresponding name exists. If found, the property gets the value. Chris@0: * Chris@0: * @author Matthieu Napoli Chris@0: * @author Kévin Dunglas Chris@0: */ Chris@0: class PropertyNormalizer extends AbstractObjectNormalizer Chris@0: { Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supportsNormalization($data, $format = null) Chris@0: { Chris@0: return parent::supportsNormalization($data, $format) && $this->supports(get_class($data)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supportsDenormalization($data, $type, $format = null) Chris@0: { Chris@0: return parent::supportsDenormalization($data, $type, $format) && $this->supports($type); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if the given class has any non-static property. Chris@0: * Chris@0: * @param string $class Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: private function supports($class) Chris@0: { Chris@0: $class = new \ReflectionClass($class); Chris@0: Chris@0: // We look for at least one non-static property Chris@0: foreach ($class->getProperties() as $property) { Chris@0: if (!$property->isStatic()) { Chris@0: return true; Chris@0: } Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array()) Chris@0: { Chris@0: if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: try { Chris@0: $reflectionProperty = new \ReflectionProperty(is_string($classOrObject) ? $classOrObject : get_class($classOrObject), $attribute); Chris@0: if ($reflectionProperty->isStatic()) { Chris@0: return false; Chris@0: } Chris@0: } catch (\ReflectionException $reflectionException) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function extractAttributes($object, $format = null, array $context = array()) Chris@0: { Chris@0: $reflectionObject = new \ReflectionObject($object); Chris@0: $attributes = array(); Chris@0: Chris@0: foreach ($reflectionObject->getProperties() as $property) { Chris@0: if (!$this->isAllowedAttribute($object, $property->name)) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $attributes[] = $property->name; Chris@0: } Chris@0: Chris@0: return $attributes; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function getAttributeValue($object, $attribute, $format = null, array $context = array()) Chris@0: { Chris@0: try { Chris@0: $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute); Chris@0: } catch (\ReflectionException $reflectionException) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Override visibility Chris@0: if (!$reflectionProperty->isPublic()) { Chris@0: $reflectionProperty->setAccessible(true); Chris@0: } Chris@0: Chris@0: return $reflectionProperty->getValue($object); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array()) Chris@0: { Chris@0: try { Chris@0: $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute); Chris@0: } catch (\ReflectionException $reflectionException) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if ($reflectionProperty->isStatic()) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Override visibility Chris@0: if (!$reflectionProperty->isPublic()) { Chris@0: $reflectionProperty->setAccessible(true); Chris@0: } Chris@0: Chris@0: $reflectionProperty->setValue($object, $value); Chris@0: } Chris@0: }