GetSetMethodNormalizer.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 = array();
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function supportsNormalization($data, $format = null)
  39. {
  40. return parent::supportsNormalization($data, $format) && $this->supports(get_class($data));
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function supportsDenormalization($data, $type, $format = null)
  46. {
  47. return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
  48. }
  49. /**
  50. * Checks if the given class has any get{Property} method.
  51. *
  52. * @param string $class
  53. *
  54. * @return bool
  55. */
  56. private function supports($class)
  57. {
  58. $class = new \ReflectionClass($class);
  59. $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
  60. foreach ($methods as $method) {
  61. if ($this->isGetMethod($method)) {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. /**
  68. * Checks if a method's name is get.* or is.*, and can be called without parameters.
  69. *
  70. * @param \ReflectionMethod $method the method to check
  71. *
  72. * @return bool whether the method is a getter or boolean getter
  73. */
  74. private function isGetMethod(\ReflectionMethod $method)
  75. {
  76. $methodLength = strlen($method->name);
  77. return
  78. !$method->isStatic() &&
  79. (
  80. ((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
  81. (0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
  82. 0 === $method->getNumberOfRequiredParameters()
  83. )
  84. ;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. protected function extractAttributes($object, $format = null, array $context = array())
  90. {
  91. $reflectionObject = new \ReflectionObject($object);
  92. $reflectionMethods = $reflectionObject->getMethods(\ReflectionMethod::IS_PUBLIC);
  93. $attributes = array();
  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)) {
  100. $attributes[] = $attributeName;
  101. }
  102. }
  103. return $attributes;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
  109. {
  110. $ucfirsted = ucfirst($attribute);
  111. $getter = 'get'.$ucfirsted;
  112. if (is_callable(array($object, $getter))) {
  113. return $object->$getter();
  114. }
  115. $isser = 'is'.$ucfirsted;
  116. if (is_callable(array($object, $isser))) {
  117. return $object->$isser();
  118. }
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
  124. {
  125. $setter = 'set'.ucfirst($attribute);
  126. $key = get_class($object).':'.$setter;
  127. if (!isset(self::$setterAccessibleCache[$key])) {
  128. self::$setterAccessibleCache[$key] = is_callable(array($object, $setter)) && !(new \ReflectionMethod($object, $setter))->isStatic();
  129. }
  130. if (self::$setterAccessibleCache[$key]) {
  131. $object->$setter($value);
  132. }
  133. }
  134. }