4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Normalizer;
14 use Symfony\Component\Serializer\Exception\CircularReferenceException;
15 use Symfony\Component\Serializer\Exception\LogicException;
16 use Symfony\Component\Serializer\Exception\RuntimeException;
19 * Converts between objects and arrays by mapping properties.
21 * The normalization process looks for all the object's properties (public and private).
22 * The result is a map from property names to property values. Property values
23 * are normalized through the serializer.
25 * The denormalization first looks at the constructor of the given class to see
26 * if any of the parameters have the same name as one of the properties. The
27 * constructor is then called with all parameters or an exception is thrown if
28 * any required parameters were not present as properties. Then the denormalizer
29 * walks through the given map of property names to property values to see if a
30 * property with the corresponding name exists. If found, the property gets the value.
32 * @author Matthieu Napoli <matthieu@mnapoli.fr>
33 * @author Kévin Dunglas <dunglas@gmail.com>
35 class PropertyNormalizer extends AbstractNormalizer
40 * @throws CircularReferenceException
42 public function normalize($object, $format = null, array $context = array())
44 if ($this->isCircularReference($object, $context)) {
45 return $this->handleCircularReference($object);
48 $reflectionObject = new \ReflectionObject($object);
49 $attributes = array();
50 $allowedAttributes = $this->getAllowedAttributes($object, $context, true);
52 foreach ($reflectionObject->getProperties() as $property) {
53 if (in_array($property->name, $this->ignoredAttributes) || $property->isStatic()) {
57 if (false !== $allowedAttributes && !in_array($property->name, $allowedAttributes)) {
61 // Override visibility
62 if (!$property->isPublic()) {
63 $property->setAccessible(true);
66 $attributeValue = $property->getValue($object);
68 if (isset($this->callbacks[$property->name])) {
69 $attributeValue = call_user_func($this->callbacks[$property->name], $attributeValue);
71 if (null !== $attributeValue && !is_scalar($attributeValue)) {
72 if (!$this->serializer instanceof NormalizerInterface) {
73 throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $property->name));
76 $attributeValue = $this->serializer->normalize($attributeValue, $format, $context);
79 $propertyName = $property->name;
80 if ($this->nameConverter) {
81 $propertyName = $this->nameConverter->normalize($propertyName);
84 $attributes[$propertyName] = $attributeValue;
93 * @throws RuntimeException
95 public function denormalize($data, $class, $format = null, array $context = array())
97 $allowedAttributes = $this->getAllowedAttributes($class, $context, true);
98 $data = $this->prepareForDenormalization($data);
100 $reflectionClass = new \ReflectionClass($class);
101 $object = $this->instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes);
103 foreach ($data as $propertyName => $value) {
104 if ($this->nameConverter) {
105 $propertyName = $this->nameConverter->denormalize($propertyName);
108 $allowed = $allowedAttributes === false || in_array($propertyName, $allowedAttributes);
109 $ignored = in_array($propertyName, $this->ignoredAttributes);
110 if ($allowed && !$ignored && $reflectionClass->hasProperty($propertyName)) {
111 $property = $reflectionClass->getProperty($propertyName);
113 if ($property->isStatic()) {
117 // Override visibility
118 if (!$property->isPublic()) {
119 $property->setAccessible(true);
122 $property->setValue($object, $value);
132 public function supportsNormalization($data, $format = null)
134 return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
140 public function supportsDenormalization($data, $type, $format = null)
142 return class_exists($type) && $this->supports($type);
146 * Checks if the given class has any non-static property.
148 * @param string $class
152 private function supports($class)
154 $class = new \ReflectionClass($class);
156 // We look for at least one non-static property
157 foreach ($class->getProperties() as $property) {
158 if (!$property->isStatic()) {