comparison vendor/symfony/serializer/Normalizer/PropertyNormalizer.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 /**
15 * Converts between objects and arrays by mapping properties.
16 *
17 * The normalization process looks for all the object's properties (public and private).
18 * The result is a map from property names to property values. Property values
19 * are normalized through the serializer.
20 *
21 * The denormalization first looks at the constructor of the given class to see
22 * if any of the parameters have the same name as one of the properties. The
23 * constructor is then called with all parameters or an exception is thrown if
24 * any required parameters were not present as properties. Then the denormalizer
25 * walks through the given map of property names to property values to see if a
26 * property with the corresponding name exists. If found, the property gets the value.
27 *
28 * @author Matthieu Napoli <matthieu@mnapoli.fr>
29 * @author Kévin Dunglas <dunglas@gmail.com>
30 */
31 class PropertyNormalizer extends AbstractObjectNormalizer
32 {
33 /**
34 * {@inheritdoc}
35 */
36 public function supportsNormalization($data, $format = null)
37 {
38 return parent::supportsNormalization($data, $format) && $this->supports(get_class($data));
39 }
40
41 /**
42 * {@inheritdoc}
43 */
44 public function supportsDenormalization($data, $type, $format = null)
45 {
46 return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
47 }
48
49 /**
50 * Checks if the given class has any non-static property.
51 *
52 * @param string $class
53 *
54 * @return bool
55 */
56 private function supports($class)
57 {
58 $class = new \ReflectionClass($class);
59
60 // We look for at least one non-static property
61 foreach ($class->getProperties() as $property) {
62 if (!$property->isStatic()) {
63 return true;
64 }
65 }
66
67 return false;
68 }
69
70 /**
71 * {@inheritdoc}
72 */
73 protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
74 {
75 if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
76 return false;
77 }
78
79 try {
80 $reflectionProperty = new \ReflectionProperty(is_string($classOrObject) ? $classOrObject : get_class($classOrObject), $attribute);
81 if ($reflectionProperty->isStatic()) {
82 return false;
83 }
84 } catch (\ReflectionException $reflectionException) {
85 return false;
86 }
87
88 return true;
89 }
90
91 /**
92 * {@inheritdoc}
93 */
94 protected function extractAttributes($object, $format = null, array $context = array())
95 {
96 $reflectionObject = new \ReflectionObject($object);
97 $attributes = array();
98
99 foreach ($reflectionObject->getProperties() as $property) {
100 if (!$this->isAllowedAttribute($object, $property->name)) {
101 continue;
102 }
103
104 $attributes[] = $property->name;
105 }
106
107 return $attributes;
108 }
109
110 /**
111 * {@inheritdoc}
112 */
113 protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
114 {
115 try {
116 $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
117 } catch (\ReflectionException $reflectionException) {
118 return;
119 }
120
121 // Override visibility
122 if (!$reflectionProperty->isPublic()) {
123 $reflectionProperty->setAccessible(true);
124 }
125
126 return $reflectionProperty->getValue($object);
127 }
128
129 /**
130 * {@inheritdoc}
131 */
132 protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
133 {
134 try {
135 $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
136 } catch (\ReflectionException $reflectionException) {
137 return;
138 }
139
140 if ($reflectionProperty->isStatic()) {
141 return;
142 }
143
144 // Override visibility
145 if (!$reflectionProperty->isPublic()) {
146 $reflectionProperty->setAccessible(true);
147 }
148
149 $reflectionProperty->setValue($object, $value);
150 }
151 }