Version 1
[yaffs-website] / vendor / symfony / serializer / Normalizer / PropertyNormalizer.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Serializer\Normalizer;
13
14 use Symfony\Component\Serializer\Exception\CircularReferenceException;
15 use Symfony\Component\Serializer\Exception\LogicException;
16 use Symfony\Component\Serializer\Exception\RuntimeException;
17
18 /**
19  * Converts between objects and arrays by mapping properties.
20  *
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.
24  *
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.
31  *
32  * @author Matthieu Napoli <matthieu@mnapoli.fr>
33  * @author Kévin Dunglas <dunglas@gmail.com>
34  */
35 class PropertyNormalizer extends AbstractNormalizer
36 {
37     /**
38      * {@inheritdoc}
39      *
40      * @throws CircularReferenceException
41      */
42     public function normalize($object, $format = null, array $context = array())
43     {
44         if ($this->isCircularReference($object, $context)) {
45             return $this->handleCircularReference($object);
46         }
47
48         $reflectionObject = new \ReflectionObject($object);
49         $attributes = array();
50         $allowedAttributes = $this->getAllowedAttributes($object, $context, true);
51
52         foreach ($reflectionObject->getProperties() as $property) {
53             if (in_array($property->name, $this->ignoredAttributes) || $property->isStatic()) {
54                 continue;
55             }
56
57             if (false !== $allowedAttributes && !in_array($property->name, $allowedAttributes)) {
58                 continue;
59             }
60
61             // Override visibility
62             if (!$property->isPublic()) {
63                 $property->setAccessible(true);
64             }
65
66             $attributeValue = $property->getValue($object);
67
68             if (isset($this->callbacks[$property->name])) {
69                 $attributeValue = call_user_func($this->callbacks[$property->name], $attributeValue);
70             }
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));
74                 }
75
76                 $attributeValue = $this->serializer->normalize($attributeValue, $format, $context);
77             }
78
79             $propertyName = $property->name;
80             if ($this->nameConverter) {
81                 $propertyName = $this->nameConverter->normalize($propertyName);
82             }
83
84             $attributes[$propertyName] = $attributeValue;
85         }
86
87         return $attributes;
88     }
89
90     /**
91      * {@inheritdoc}
92      *
93      * @throws RuntimeException
94      */
95     public function denormalize($data, $class, $format = null, array $context = array())
96     {
97         $allowedAttributes = $this->getAllowedAttributes($class, $context, true);
98         $data = $this->prepareForDenormalization($data);
99
100         $reflectionClass = new \ReflectionClass($class);
101         $object = $this->instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes);
102
103         foreach ($data as $propertyName => $value) {
104             if ($this->nameConverter) {
105                 $propertyName = $this->nameConverter->denormalize($propertyName);
106             }
107
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);
112
113                 if ($property->isStatic()) {
114                     continue;
115                 }
116
117                 // Override visibility
118                 if (!$property->isPublic()) {
119                     $property->setAccessible(true);
120                 }
121
122                 $property->setValue($object, $value);
123             }
124         }
125
126         return $object;
127     }
128
129     /**
130      * {@inheritdoc}
131      */
132     public function supportsNormalization($data, $format = null)
133     {
134         return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
135     }
136
137     /**
138      * {@inheritdoc}
139      */
140     public function supportsDenormalization($data, $type, $format = null)
141     {
142         return class_exists($type) && $this->supports($type);
143     }
144
145     /**
146      * Checks if the given class has any non-static property.
147      *
148      * @param string $class
149      *
150      * @return bool
151      */
152     private function supports($class)
153     {
154         $class = new \ReflectionClass($class);
155
156         // We look for at least one non-static property
157         foreach ($class->getProperties() as $property) {
158             if (!$property->isStatic()) {
159                 return true;
160             }
161         }
162
163         return false;
164     }
165 }