PropertyNormalizer.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Normalizer;
  11. /**
  12. * Converts between objects and arrays by mapping properties.
  13. *
  14. * The normalization process looks for all the object's properties (public and private).
  15. * The result is a map from property names to property values. Property values
  16. * are normalized through the serializer.
  17. *
  18. * The denormalization first looks at the constructor of the given class to see
  19. * if any of the parameters have the same name as one of the properties. The
  20. * constructor is then called with all parameters or an exception is thrown if
  21. * any required parameters were not present as properties. Then the denormalizer
  22. * walks through the given map of property names to property values to see if a
  23. * property with the corresponding name exists. If found, the property gets the value.
  24. *
  25. * @author Matthieu Napoli <matthieu@mnapoli.fr>
  26. * @author Kévin Dunglas <dunglas@gmail.com>
  27. */
  28. class PropertyNormalizer extends AbstractObjectNormalizer
  29. {
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function supportsNormalization($data, string $format = null)
  34. {
  35. return parent::supportsNormalization($data, $format) && $this->supports(\get_class($data));
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function supportsDenormalization($data, string $type, string $format = null)
  41. {
  42. return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function hasCacheableSupportsMethod(): bool
  48. {
  49. return __CLASS__ === static::class;
  50. }
  51. /**
  52. * Checks if the given class has any non-static property.
  53. */
  54. private function supports(string $class): bool
  55. {
  56. $class = new \ReflectionClass($class);
  57. // We look for at least one non-static property
  58. do {
  59. foreach ($class->getProperties() as $property) {
  60. if (!$property->isStatic()) {
  61. return true;
  62. }
  63. }
  64. } while ($class = $class->getParentClass());
  65. return false;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function isAllowedAttribute($classOrObject, string $attribute, string $format = null, array $context = [])
  71. {
  72. if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
  73. return false;
  74. }
  75. try {
  76. $reflectionProperty = $this->getReflectionProperty($classOrObject, $attribute);
  77. if ($reflectionProperty->isStatic()) {
  78. return false;
  79. }
  80. } catch (\ReflectionException $reflectionException) {
  81. return false;
  82. }
  83. return true;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. protected function extractAttributes(object $object, string $format = null, array $context = [])
  89. {
  90. $reflectionObject = new \ReflectionObject($object);
  91. $attributes = [];
  92. $checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
  93. do {
  94. foreach ($reflectionObject->getProperties() as $property) {
  95. if ($checkPropertyInitialization) {
  96. if (!$property->isPublic()) {
  97. $property->setAccessible(true);
  98. }
  99. if (!$property->isInitialized($object)) {
  100. continue;
  101. }
  102. }
  103. if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)) {
  104. continue;
  105. }
  106. $attributes[] = $property->name;
  107. }
  108. } while ($reflectionObject = $reflectionObject->getParentClass());
  109. return $attributes;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
  115. {
  116. try {
  117. $reflectionProperty = $this->getReflectionProperty($object, $attribute);
  118. } catch (\ReflectionException $reflectionException) {
  119. return null;
  120. }
  121. // Override visibility
  122. if (!$reflectionProperty->isPublic()) {
  123. $reflectionProperty->setAccessible(true);
  124. }
  125. return $reflectionProperty->getValue($object);
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = [])
  131. {
  132. try {
  133. $reflectionProperty = $this->getReflectionProperty($object, $attribute);
  134. } catch (\ReflectionException $reflectionException) {
  135. return;
  136. }
  137. if ($reflectionProperty->isStatic()) {
  138. return;
  139. }
  140. // Override visibility
  141. if (!$reflectionProperty->isPublic()) {
  142. $reflectionProperty->setAccessible(true);
  143. }
  144. $reflectionProperty->setValue($object, $value);
  145. }
  146. /**
  147. * @param string|object $classOrObject
  148. *
  149. * @throws \ReflectionException
  150. */
  151. private function getReflectionProperty($classOrObject, string $attribute): \ReflectionProperty
  152. {
  153. $reflectionClass = new \ReflectionClass($classOrObject);
  154. while (true) {
  155. try {
  156. return $reflectionClass->getProperty($attribute);
  157. } catch (\ReflectionException $e) {
  158. if (!$reflectionClass = $reflectionClass->getParentClass()) {
  159. throw $e;
  160. }
  161. }
  162. }
  163. }
  164. }