GetSetMethodNormalizer.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 with getter and setter methods and arrays.
  13. *
  14. * The normalization process looks at all public methods and calls the ones
  15. * which have a name starting with get and take no parameters. The result is a
  16. * map from property names (method name stripped of the get prefix and converted
  17. * to lower case) to property values. Property values are normalized through the
  18. * serializer.
  19. *
  20. * The denormalization first looks at the constructor of the given class to see
  21. * if any of the parameters have the same name as one of the properties. The
  22. * constructor is then called with all parameters or an exception is thrown if
  23. * any required parameters were not present as properties. Then the denormalizer
  24. * walks through the given map of property names to property values to see if a
  25. * setter method exists for any of the properties. If a setter exists it is
  26. * called with the property value. No automatic denormalization of the value
  27. * takes place.
  28. *
  29. * @author Nils Adermann <naderman@naderman.de>
  30. * @author Kévin Dunglas <dunglas@gmail.com>
  31. */
  32. class GetSetMethodNormalizer extends AbstractObjectNormalizer
  33. {
  34. private static $setterAccessibleCache = [];
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function supportsNormalization($data, string $format = null)
  39. {
  40. return parent::supportsNormalization($data, $format) && $this->supports(\get_class($data));
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function supportsDenormalization($data, string $type, string $format = null)
  46. {
  47. return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function hasCacheableSupportsMethod(): bool
  53. {
  54. return __CLASS__ === static::class;
  55. }
  56. /**
  57. * Checks if the given class has any get{Property} method.
  58. */
  59. private function supports(string $class): bool
  60. {
  61. $class = new \ReflectionClass($class);
  62. $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
  63. foreach ($methods as $method) {
  64. if ($this->isGetMethod($method)) {
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. /**
  71. * Checks if a method's name is get.* or is.*, and can be called without parameters.
  72. */
  73. private function isGetMethod(\ReflectionMethod $method): bool
  74. {
  75. $methodLength = \strlen($method->name);
  76. return
  77. !$method->isStatic() &&
  78. (
  79. ((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
  80. (0 === strpos($method->name, 'is') && 2 < $methodLength) ||
  81. (0 === strpos($method->name, 'has') && 3 < $methodLength)) &&
  82. 0 === $method->getNumberOfRequiredParameters()
  83. )
  84. ;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. protected function extractAttributes(object $object, string $format = null, array $context = [])
  90. {
  91. $reflectionObject = new \ReflectionObject($object);
  92. $reflectionMethods = $reflectionObject->getMethods(\ReflectionMethod::IS_PUBLIC);
  93. $attributes = [];
  94. foreach ($reflectionMethods as $method) {
  95. if (!$this->isGetMethod($method)) {
  96. continue;
  97. }
  98. $attributeName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
  99. if ($this->isAllowedAttribute($object, $attributeName, $format, $context)) {
  100. $attributes[] = $attributeName;
  101. }
  102. }
  103. return $attributes;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
  109. {
  110. $ucfirsted = ucfirst($attribute);
  111. $getter = 'get'.$ucfirsted;
  112. if (\is_callable([$object, $getter])) {
  113. return $object->$getter();
  114. }
  115. $isser = 'is'.$ucfirsted;
  116. if (\is_callable([$object, $isser])) {
  117. return $object->$isser();
  118. }
  119. $haser = 'has'.$ucfirsted;
  120. if (\is_callable([$object, $haser])) {
  121. return $object->$haser();
  122. }
  123. return null;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = [])
  129. {
  130. $setter = 'set'.ucfirst($attribute);
  131. $key = \get_class($object).':'.$setter;
  132. if (!isset(self::$setterAccessibleCache[$key])) {
  133. self::$setterAccessibleCache[$key] = \is_callable([$object, $setter]) && !(new \ReflectionMethod($object, $setter))->isStatic();
  134. }
  135. if (self::$setterAccessibleCache[$key]) {
  136. $object->$setter($value);
  137. }
  138. }
  139. }