comparison vendor/symfony/serializer/Normalizer/PropertyNormalizer.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
28 * @author Matthieu Napoli <matthieu@mnapoli.fr> 28 * @author Matthieu Napoli <matthieu@mnapoli.fr>
29 * @author Kévin Dunglas <dunglas@gmail.com> 29 * @author Kévin Dunglas <dunglas@gmail.com>
30 */ 30 */
31 class PropertyNormalizer extends AbstractObjectNormalizer 31 class PropertyNormalizer extends AbstractObjectNormalizer
32 { 32 {
33 private $cache = array();
34
33 /** 35 /**
34 * {@inheritdoc} 36 * {@inheritdoc}
35 */ 37 */
36 public function supportsNormalization($data, $format = null) 38 public function supportsNormalization($data, $format = null)
37 { 39 {
38 return parent::supportsNormalization($data, $format) && $this->supports(get_class($data)); 40 return parent::supportsNormalization($data, $format) && (isset($this->cache[$type = \get_class($data)]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
39 } 41 }
40 42
41 /** 43 /**
42 * {@inheritdoc} 44 * {@inheritdoc}
43 */ 45 */
44 public function supportsDenormalization($data, $type, $format = null) 46 public function supportsDenormalization($data, $type, $format = null)
45 { 47 {
46 return parent::supportsDenormalization($data, $type, $format) && $this->supports($type); 48 return parent::supportsDenormalization($data, $type, $format) && (isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
47 } 49 }
48 50
49 /** 51 /**
50 * Checks if the given class has any non-static property. 52 * Checks if the given class has any non-static property.
51 * 53 *
56 private function supports($class) 58 private function supports($class)
57 { 59 {
58 $class = new \ReflectionClass($class); 60 $class = new \ReflectionClass($class);
59 61
60 // We look for at least one non-static property 62 // We look for at least one non-static property
61 foreach ($class->getProperties() as $property) { 63 do {
62 if (!$property->isStatic()) { 64 foreach ($class->getProperties() as $property) {
63 return true; 65 if (!$property->isStatic()) {
66 return true;
67 }
64 } 68 }
65 } 69 } while ($class = $class->getParentClass());
66 70
67 return false; 71 return false;
68 } 72 }
69 73
70 /** 74 /**
75 if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) { 79 if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
76 return false; 80 return false;
77 } 81 }
78 82
79 try { 83 try {
80 $reflectionProperty = new \ReflectionProperty(is_string($classOrObject) ? $classOrObject : get_class($classOrObject), $attribute); 84 $reflectionProperty = $this->getReflectionProperty($classOrObject, $attribute);
81 if ($reflectionProperty->isStatic()) { 85 if ($reflectionProperty->isStatic()) {
82 return false; 86 return false;
83 } 87 }
84 } catch (\ReflectionException $reflectionException) { 88 } catch (\ReflectionException $reflectionException) {
85 return false; 89 return false;
94 protected function extractAttributes($object, $format = null, array $context = array()) 98 protected function extractAttributes($object, $format = null, array $context = array())
95 { 99 {
96 $reflectionObject = new \ReflectionObject($object); 100 $reflectionObject = new \ReflectionObject($object);
97 $attributes = array(); 101 $attributes = array();
98 102
99 foreach ($reflectionObject->getProperties() as $property) { 103 do {
100 if (!$this->isAllowedAttribute($object, $property->name)) { 104 foreach ($reflectionObject->getProperties() as $property) {
101 continue; 105 if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name)) {
106 continue;
107 }
108
109 $attributes[] = $property->name;
102 } 110 }
103 111 } while ($reflectionObject = $reflectionObject->getParentClass());
104 $attributes[] = $property->name;
105 }
106 112
107 return $attributes; 113 return $attributes;
108 } 114 }
109 115
110 /** 116 /**
111 * {@inheritdoc} 117 * {@inheritdoc}
112 */ 118 */
113 protected function getAttributeValue($object, $attribute, $format = null, array $context = array()) 119 protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
114 { 120 {
115 try { 121 try {
116 $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute); 122 $reflectionProperty = $this->getReflectionProperty($object, $attribute);
117 } catch (\ReflectionException $reflectionException) { 123 } catch (\ReflectionException $reflectionException) {
118 return; 124 return;
119 } 125 }
120 126
121 // Override visibility 127 // Override visibility
130 * {@inheritdoc} 136 * {@inheritdoc}
131 */ 137 */
132 protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array()) 138 protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
133 { 139 {
134 try { 140 try {
135 $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute); 141 $reflectionProperty = $this->getReflectionProperty($object, $attribute);
136 } catch (\ReflectionException $reflectionException) { 142 } catch (\ReflectionException $reflectionException) {
137 return; 143 return;
138 } 144 }
139 145
140 if ($reflectionProperty->isStatic()) { 146 if ($reflectionProperty->isStatic()) {
146 $reflectionProperty->setAccessible(true); 152 $reflectionProperty->setAccessible(true);
147 } 153 }
148 154
149 $reflectionProperty->setValue($object, $value); 155 $reflectionProperty->setValue($object, $value);
150 } 156 }
157
158 /**
159 * @param string|object $classOrObject
160 * @param string $attribute
161 *
162 * @return \ReflectionProperty
163 *
164 * @throws \ReflectionException
165 */
166 private function getReflectionProperty($classOrObject, $attribute)
167 {
168 $reflectionClass = new \ReflectionClass($classOrObject);
169 while (true) {
170 try {
171 return $reflectionClass->getProperty($attribute);
172 } catch (\ReflectionException $e) {
173 if (!$reflectionClass = $reflectionClass->getParentClass()) {
174 throw $e;
175 }
176 }
177 }
178 }
151 } 179 }