PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/vendor/zendframework/zend-stdlib/src/Hydrator/ClassMethods.php

https://gitlab.com/koodersmiikka/operaatio-terveys
PHP | 195 lines | 120 code | 26 blank | 49 comment | 13 complexity | 66caacb1542f5989230bbc544717d6cf MD5 | raw file
  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. class ClassMethods extends AbstractHydrator implements HydratorOptionsInterface
  21. {
  22. /**
  23. * Flag defining whether array keys are underscore-separated (true) or camel case (false)
  24. * @var bool
  25. */
  26. protected $underscoreSeparatedKeys = true;
  27. /**
  28. * @var \Zend\Stdlib\Hydrator\Filter\FilterInterface
  29. */
  30. private $callableMethodFilter;
  31. /**
  32. * Define if extract values will use camel case or name with underscore
  33. * @param bool|array $underscoreSeparatedKeys
  34. */
  35. public function __construct($underscoreSeparatedKeys = true)
  36. {
  37. parent::__construct();
  38. $this->setUnderscoreSeparatedKeys($underscoreSeparatedKeys);
  39. $this->callableMethodFilter = new OptionalParametersFilter();
  40. $this->filterComposite->addFilter("is", new IsFilter());
  41. $this->filterComposite->addFilter("has", new HasFilter());
  42. $this->filterComposite->addFilter("get", new GetFilter());
  43. $this->filterComposite->addFilter("parameter", new OptionalParametersFilter(), FilterComposite::CONDITION_AND);
  44. }
  45. /**
  46. * @param array|Traversable $options
  47. * @return ClassMethods
  48. * @throws Exception\InvalidArgumentException
  49. */
  50. public function setOptions($options)
  51. {
  52. if ($options instanceof Traversable) {
  53. $options = ArrayUtils::iteratorToArray($options);
  54. } elseif (!is_array($options)) {
  55. throw new Exception\InvalidArgumentException(
  56. 'The options parameter must be an array or a Traversable'
  57. );
  58. }
  59. if (isset($options['underscoreSeparatedKeys'])) {
  60. $this->setUnderscoreSeparatedKeys($options['underscoreSeparatedKeys']);
  61. }
  62. return $this;
  63. }
  64. /**
  65. * @param bool $underscoreSeparatedKeys
  66. * @return ClassMethods
  67. */
  68. public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys)
  69. {
  70. $this->underscoreSeparatedKeys = $underscoreSeparatedKeys;
  71. return $this;
  72. }
  73. /**
  74. * @return bool
  75. */
  76. public function getUnderscoreSeparatedKeys()
  77. {
  78. return $this->underscoreSeparatedKeys;
  79. }
  80. /**
  81. * Extract values from an object with class methods
  82. *
  83. * Extracts the getter/setter of the given $object.
  84. *
  85. * @param object $object
  86. * @return array
  87. * @throws Exception\BadMethodCallException for a non-object $object
  88. */
  89. public function extract($object)
  90. {
  91. if (!is_object($object)) {
  92. throw new Exception\BadMethodCallException(sprintf(
  93. '%s expects the provided $object to be a PHP object)', __METHOD__
  94. ));
  95. }
  96. $filter = null;
  97. if ($object instanceof FilterProviderInterface) {
  98. $filter = new FilterComposite(
  99. array($object->getFilter()),
  100. array(new MethodMatchFilter("getFilter"))
  101. );
  102. } else {
  103. $filter = $this->filterComposite;
  104. }
  105. $transform = function ($letters) {
  106. $letter = array_shift($letters);
  107. return '_' . strtolower($letter);
  108. };
  109. $attributes = array();
  110. $methods = get_class_methods($object);
  111. foreach ($methods as $method) {
  112. if (
  113. !$filter->filter(
  114. get_class($object) . '::' . $method
  115. )
  116. ) {
  117. continue;
  118. }
  119. if (!$this->callableMethodFilter->filter(get_class($object) . '::' . $method)) {
  120. continue;
  121. }
  122. $attribute = $method;
  123. if (preg_match('/^get/', $method)) {
  124. $attribute = substr($method, 3);
  125. if (!property_exists($object, $attribute)) {
  126. $attribute = lcfirst($attribute);
  127. }
  128. }
  129. if ($this->underscoreSeparatedKeys) {
  130. $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute);
  131. }
  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. $transform = function ($letters) {
  154. $letter = substr(array_shift($letters), 1, 1);
  155. return ucfirst($letter);
  156. };
  157. foreach ($data as $property => $value) {
  158. $method = 'set' . ucfirst($property);
  159. if ($this->underscoreSeparatedKeys) {
  160. $method = preg_replace_callback('/(_[a-z])/i', $transform, $method);
  161. }
  162. if (is_callable(array($object, $method))) {
  163. $value = $this->hydrateValue($property, $value, $data);
  164. $object->$method($value);
  165. }
  166. }
  167. return $object;
  168. }
  169. }