Drupal investigation

PropertyNormalizer.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. use Symfony\Component\Serializer\Exception\CircularReferenceException;
  12. use Symfony\Component\Serializer\Exception\LogicException;
  13. use Symfony\Component\Serializer\Exception\RuntimeException;
  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 AbstractNormalizer
  32. {
  33. /**
  34. * {@inheritdoc}
  35. *
  36. * @throws CircularReferenceException
  37. */
  38. public function normalize($object, $format = null, array $context = array())
  39. {
  40. if ($this->isCircularReference($object, $context)) {
  41. return $this->handleCircularReference($object);
  42. }
  43. $reflectionObject = new \ReflectionObject($object);
  44. $attributes = array();
  45. $allowedAttributes = $this->getAllowedAttributes($object, $context, true);
  46. foreach ($reflectionObject->getProperties() as $property) {
  47. if (in_array($property->name, $this->ignoredAttributes) || $property->isStatic()) {
  48. continue;
  49. }
  50. if (false !== $allowedAttributes && !in_array($property->name, $allowedAttributes)) {
  51. continue;
  52. }
  53. // Override visibility
  54. if (!$property->isPublic()) {
  55. $property->setAccessible(true);
  56. }
  57. $attributeValue = $property->getValue($object);
  58. if (isset($this->callbacks[$property->name])) {
  59. $attributeValue = call_user_func($this->callbacks[$property->name], $attributeValue);
  60. }
  61. if (null !== $attributeValue && !is_scalar($attributeValue)) {
  62. if (!$this->serializer instanceof NormalizerInterface) {
  63. throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $property->name));
  64. }
  65. $attributeValue = $this->serializer->normalize($attributeValue, $format, $context);
  66. }
  67. $propertyName = $property->name;
  68. if ($this->nameConverter) {
  69. $propertyName = $this->nameConverter->normalize($propertyName);
  70. }
  71. $attributes[$propertyName] = $attributeValue;
  72. }
  73. return $attributes;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. *
  78. * @throws RuntimeException
  79. */
  80. public function denormalize($data, $class, $format = null, array $context = array())
  81. {
  82. $allowedAttributes = $this->getAllowedAttributes($class, $context, true);
  83. $data = $this->prepareForDenormalization($data);
  84. $reflectionClass = new \ReflectionClass($class);
  85. $object = $this->instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes);
  86. foreach ($data as $propertyName => $value) {
  87. if ($this->nameConverter) {
  88. $propertyName = $this->nameConverter->denormalize($propertyName);
  89. }
  90. $allowed = $allowedAttributes === false || in_array($propertyName, $allowedAttributes);
  91. $ignored = in_array($propertyName, $this->ignoredAttributes);
  92. if ($allowed && !$ignored && $reflectionClass->hasProperty($propertyName)) {
  93. $property = $reflectionClass->getProperty($propertyName);
  94. if ($property->isStatic()) {
  95. continue;
  96. }
  97. // Override visibility
  98. if (!$property->isPublic()) {
  99. $property->setAccessible(true);
  100. }
  101. $property->setValue($object, $value);
  102. }
  103. }
  104. return $object;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function supportsNormalization($data, $format = null)
  110. {
  111. return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function supportsDenormalization($data, $type, $format = null)
  117. {
  118. return class_exists($type) && $this->supports($type);
  119. }
  120. /**
  121. * Checks if the given class has any non-static property.
  122. *
  123. * @param string $class
  124. *
  125. * @return bool
  126. */
  127. private function supports($class)
  128. {
  129. $class = new \ReflectionClass($class);
  130. // We look for at least one non-static property
  131. foreach ($class->getProperties() as $property) {
  132. if (!$property->isStatic()) {
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. }