ClassMethods.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Stdlib\Hydrator;
  10. use ReflectionMethod;
  11. use Traversable;
  12. use Zend\Stdlib\Exception;
  13. use Zend\Stdlib\ArrayUtils;
  14. use Zend\Stdlib\Hydrator\Filter\FilterComposite;
  15. use Zend\Stdlib\Hydrator\Filter\FilterProviderInterface;
  16. use Zend\Stdlib\Hydrator\Filter\GetFilter;
  17. use Zend\Stdlib\Hydrator\Filter\HasFilter;
  18. use Zend\Stdlib\Hydrator\Filter\IsFilter;
  19. use Zend\Stdlib\Hydrator\Filter\MethodMatchFilter;
  20. use Zend\Stdlib\Hydrator\Filter\OptionalParametersFilter;
  21. class ClassMethods extends AbstractHydrator implements HydratorOptionsInterface
  22. {
  23. /**
  24. * Flag defining whether array keys are underscore-separated (true) or camel case (false)
  25. * @var bool
  26. */
  27. protected $underscoreSeparatedKeys = true;
  28. /**
  29. * @var \Zend\Stdlib\Hydrator\Filter\FilterInterface
  30. */
  31. private $callableMethodFilter;
  32. /**
  33. * Define if extract values will use camel case or name with underscore
  34. * @param bool|array $underscoreSeparatedKeys
  35. */
  36. public function __construct($underscoreSeparatedKeys = true)
  37. {
  38. parent::__construct();
  39. $this->setUnderscoreSeparatedKeys($underscoreSeparatedKeys);
  40. $this->callableMethodFilter = new OptionalParametersFilter();
  41. $this->filterComposite->addFilter("is", new IsFilter());
  42. $this->filterComposite->addFilter("has", new HasFilter());
  43. $this->filterComposite->addFilter("get", new GetFilter());
  44. $this->filterComposite->addFilter("parameter", new OptionalParametersFilter(), FilterComposite::CONDITION_AND);
  45. }
  46. /**
  47. * @param array|Traversable $options
  48. * @return ClassMethods
  49. * @throws Exception\InvalidArgumentException
  50. */
  51. public function setOptions($options)
  52. {
  53. if ($options instanceof Traversable) {
  54. $options = ArrayUtils::iteratorToArray($options);
  55. } elseif (!is_array($options)) {
  56. throw new Exception\InvalidArgumentException(
  57. 'The options parameter must be an array or a Traversable'
  58. );
  59. }
  60. if (isset($options['underscoreSeparatedKeys'])) {
  61. $this->setUnderscoreSeparatedKeys($options['underscoreSeparatedKeys']);
  62. }
  63. return $this;
  64. }
  65. /**
  66. * @param bool $underscoreSeparatedKeys
  67. * @return ClassMethods
  68. */
  69. public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys)
  70. {
  71. $this->underscoreSeparatedKeys = $underscoreSeparatedKeys;
  72. return $this;
  73. }
  74. /**
  75. * @return bool
  76. */
  77. public function getUnderscoreSeparatedKeys()
  78. {
  79. return $this->underscoreSeparatedKeys;
  80. }
  81. /**
  82. * Extract values from an object with class methods
  83. *
  84. * Extracts the getter/setter of the given $object.
  85. *
  86. * @param object $object
  87. * @return array
  88. * @throws Exception\BadMethodCallException for a non-object $object
  89. */
  90. public function extract($object)
  91. {
  92. if (!is_object($object)) {
  93. throw new Exception\BadMethodCallException(sprintf(
  94. '%s expects the provided $object to be a PHP object)', __METHOD__
  95. ));
  96. }
  97. $filter = null;
  98. if ($object instanceof FilterProviderInterface) {
  99. $filter = new FilterComposite(
  100. array($object->getFilter()),
  101. array(new MethodMatchFilter("getFilter"))
  102. );
  103. } else {
  104. $filter = $this->filterComposite;
  105. }
  106. $transform = function ($letters) {
  107. $letter = array_shift($letters);
  108. return '_' . strtolower($letter);
  109. };
  110. $attributes = array();
  111. $methods = get_class_methods($object);
  112. foreach ($methods as $method) {
  113. if (
  114. !$filter->filter(
  115. get_class($object) . '::' . $method
  116. )
  117. ) {
  118. continue;
  119. }
  120. if (!$this->callableMethodFilter->filter(get_class($object) . '::' . $method)) {
  121. continue;
  122. }
  123. $attribute = $method;
  124. if (preg_match('/^get/', $method)) {
  125. $attribute = substr($method, 3);
  126. if (!property_exists($object, $attribute)) {
  127. $attribute = lcfirst($attribute);
  128. }
  129. }
  130. if ($this->underscoreSeparatedKeys) {
  131. $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute);
  132. }
  133. $attributes[$attribute] = $this->extractValue($attribute, $object->$method(), $object);
  134. }
  135. return $attributes;
  136. }
  137. /**
  138. * Hydrate an object by populating getter/setter methods
  139. *
  140. * Hydrates an object by getter/setter methods of the object.
  141. *
  142. * @param array $data
  143. * @param object $object
  144. * @return object
  145. * @throws Exception\BadMethodCallException for a non-object $object
  146. */
  147. public function hydrate(array $data, $object)
  148. {
  149. if (!is_object($object)) {
  150. throw new Exception\BadMethodCallException(sprintf(
  151. '%s expects the provided $object to be a PHP object)', __METHOD__
  152. ));
  153. }
  154. $transform = function ($letters) {
  155. $letter = substr(array_shift($letters), 1, 1);
  156. return ucfirst($letter);
  157. };
  158. foreach ($data as $property => $value) {
  159. $method = 'set' . ucfirst($property);
  160. if ($this->underscoreSeparatedKeys) {
  161. $method = preg_replace_callback('/(_[a-z])/i', $transform, $method);
  162. }
  163. if (is_callable(array($object, $method))) {
  164. $value = $this->hydrateValue($property, $value, $data);
  165. $object->$method($value);
  166. }
  167. }
  168. return $object;
  169. }
  170. }