ClassMethods.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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-2014 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 Traversable;
  11. use Zend\Stdlib\Exception;
  12. use Zend\Stdlib\ArrayUtils;
  13. use Zend\Stdlib\Hydrator\Filter\FilterComposite;
  14. use Zend\Stdlib\Hydrator\Filter\FilterProviderInterface;
  15. use Zend\Stdlib\Hydrator\Filter\GetFilter;
  16. use Zend\Stdlib\Hydrator\Filter\HasFilter;
  17. use Zend\Stdlib\Hydrator\Filter\IsFilter;
  18. use Zend\Stdlib\Hydrator\Filter\MethodMatchFilter;
  19. use Zend\Stdlib\Hydrator\Filter\OptionalParametersFilter;
  20. use Zend\Stdlib\Hydrator\NamingStrategy\UnderscoreNamingStrategy;
  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 = (bool) $underscoreSeparatedKeys;
  72. if ($this->underscoreSeparatedKeys) {
  73. $this->setNamingStrategy(new UnderscoreNamingStrategy);
  74. } elseif ($this->getNamingStrategy() instanceof UnderscoreNamingStrategy) {
  75. $this->removeNamingStrategy();
  76. }
  77. return $this;
  78. }
  79. /**
  80. * @return bool
  81. */
  82. public function getUnderscoreSeparatedKeys()
  83. {
  84. return $this->underscoreSeparatedKeys;
  85. }
  86. /**
  87. * Extract values from an object with class methods
  88. *
  89. * Extracts the getter/setter of the given $object.
  90. *
  91. * @param object $object
  92. * @return array
  93. * @throws Exception\BadMethodCallException for a non-object $object
  94. */
  95. public function extract($object)
  96. {
  97. if (!is_object($object)) {
  98. throw new Exception\BadMethodCallException(sprintf(
  99. '%s expects the provided $object to be a PHP object)', __METHOD__
  100. ));
  101. }
  102. $filter = null;
  103. if ($object instanceof FilterProviderInterface) {
  104. $filter = new FilterComposite(
  105. array($object->getFilter()),
  106. array(new MethodMatchFilter("getFilter"))
  107. );
  108. } else {
  109. $filter = $this->filterComposite;
  110. }
  111. $attributes = array();
  112. $methods = get_class_methods($object);
  113. foreach ($methods as $method) {
  114. if (
  115. !$filter->filter(
  116. get_class($object) . '::' . $method
  117. )
  118. ) {
  119. continue;
  120. }
  121. if (!$this->callableMethodFilter->filter(get_class($object) . '::' . $method)) {
  122. continue;
  123. }
  124. $attribute = $method;
  125. if (preg_match('/^get/', $method)) {
  126. $attribute = substr($method, 3);
  127. if (!property_exists($object, $attribute)) {
  128. $attribute = lcfirst($attribute);
  129. }
  130. }
  131. $attribute = $this->extractName($attribute, $object);
  132. $attributes[$attribute] = $this->extractValue($attribute, $object->$method(), $object);
  133. }
  134. return $attributes;
  135. }
  136. /**
  137. * Hydrate an object by populating getter/setter methods
  138. *
  139. * Hydrates an object by getter/setter methods of the object.
  140. *
  141. * @param array $data
  142. * @param object $object
  143. * @return object
  144. * @throws Exception\BadMethodCallException for a non-object $object
  145. */
  146. public function hydrate(array $data, $object)
  147. {
  148. if (!is_object($object)) {
  149. throw new Exception\BadMethodCallException(sprintf(
  150. '%s expects the provided $object to be a PHP object)', __METHOD__
  151. ));
  152. }
  153. foreach ($data as $property => $value) {
  154. $method = 'set' . ucfirst($this->hydrateName($property, $data));
  155. if (is_callable(array($object, $method))) {
  156. $value = $this->hydrateValue($property, $value, $data);
  157. $object->$method($value);
  158. }
  159. }
  160. return $object;
  161. }
  162. }