PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/184.168.182.1/wp-content/plugins/updraftplus/oc/rs/lib/OpenCloud/Common/Collection/ResourceIterator.php

https://gitlab.com/endomorphosis/falkenstein
PHP | 217 lines | 105 code | 31 blank | 81 comment | 5 complexity | e4a20accafef80bfb3de2d34b342b815 MD5 | raw file
  1. <?php
  2. /**
  3. * PHP OpenCloud library.
  4. *
  5. * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
  6. * @license https://www.apache.org/licenses/LICENSE-2.0
  7. * @author Jamie Hannaford <jamie.hannaford@rackspace.com>
  8. */
  9. namespace OpenCloud\Common\Collection;
  10. use Iterator;
  11. use OpenCloud\Common\Log\Logger;
  12. use OpenCloud\Common\Exceptions\InvalidArgumentError;
  13. class ResourceIterator extends ArrayCollection implements Iterator
  14. {
  15. /**
  16. * @var int Internal pointer of the iterator - reveals its current position.
  17. */
  18. protected $position;
  19. /**
  20. * @var object The parent object which resource models are instantiated from. The parent needs to have appropriate
  21. * methods to instantiate the particular object.
  22. */
  23. protected $resourceParent;
  24. /**
  25. * @var array The options for this iterator.
  26. */
  27. protected $options;
  28. /**
  29. * @var array Fallback defaults if options are not explicitly set or provided.
  30. */
  31. protected $defaults = array();
  32. /**
  33. * @var array Required options
  34. */
  35. protected $required = array();
  36. public static function factory($parent, array $options = array(), array $data = array())
  37. {
  38. $iterator = new static($data);
  39. $iterator->setResourceParent($parent)
  40. ->setElements($data)
  41. ->setOptions($iterator->parseOptions($options))
  42. ->rewind();
  43. return $iterator;
  44. }
  45. protected function parseOptions(array $options)
  46. {
  47. $options = $options + $this->defaults;
  48. if ($missing = array_diff($this->required, array_keys($options))) {
  49. throw new InvalidArgumentError(sprintf('%s is a required option', implode(',', $missing)));
  50. }
  51. return $options;
  52. }
  53. /**
  54. * @param $parent
  55. * @return $this
  56. */
  57. public function setResourceParent($parent)
  58. {
  59. $this->resourceParent = $parent;
  60. return $this;
  61. }
  62. /**
  63. * @param array $options
  64. * @return $this
  65. */
  66. public function setOptions(array $options)
  67. {
  68. $this->options = $options;
  69. return $this;
  70. }
  71. /**
  72. * Set a particular option.
  73. *
  74. * @param $key
  75. * @param $value
  76. * @return $this
  77. */
  78. public function setOption($key, $value)
  79. {
  80. $this->options[$key] = $value;
  81. return $this;
  82. }
  83. /**
  84. * @param $key
  85. * @return null
  86. */
  87. public function getOption($key)
  88. {
  89. return (isset($this->options[$key])) ? $this->options[$key] : null;
  90. }
  91. /**
  92. * This method is called after self::rewind() and self::next() to check if the current position is valid.
  93. *
  94. * @return bool
  95. */
  96. public function valid()
  97. {
  98. return $this->offsetExists($this->position) && $this->position < $this->limit;
  99. }
  100. /**
  101. * Increment the current pointer by 1, and also update the current marker.
  102. */
  103. public function next()
  104. {
  105. $this->position++;
  106. return $this->current();
  107. }
  108. /**
  109. * Reset the pointer and current marker.
  110. */
  111. public function rewind()
  112. {
  113. $this->position = 0;
  114. }
  115. /**
  116. * @return mixed
  117. */
  118. public function current()
  119. {
  120. return $this->constructResource($this->currentElement());
  121. }
  122. /**
  123. * @return mixed
  124. */
  125. public function currentElement()
  126. {
  127. return $this->offsetGet($this->key());
  128. }
  129. /**
  130. * Using a standard object, this method populates a resource model with all the object data. It does this using a
  131. * whatever method the parent object has for resource creation.
  132. *
  133. * @param $object Standard object
  134. * @return mixed
  135. * @throws \OpenCloud\Common\Exceptions\CollectionException
  136. */
  137. public function constructResource($object)
  138. {
  139. $className = $this->getOption('resourceClass');
  140. if (substr_count($className, '\\')) {
  141. $array = explode('\\', $className);
  142. $className = end($array);
  143. }
  144. $parent = $this->resourceParent;
  145. $getter = sprintf('get%s', ucfirst($className));
  146. if (method_exists($parent, $className)) {
  147. // $parent->server($data)
  148. return call_user_func(array($parent, $className), $object);
  149. } elseif (method_exists($parent, $getter)) {
  150. // $parent->getServer($data)
  151. return call_user_func(array($parent, $getter), $object);
  152. } elseif (method_exists($parent, 'resource')) {
  153. // $parent->resource('Server', $data)
  154. return $parent->resource($className, $object);
  155. } else {
  156. return $object;
  157. }
  158. }
  159. /**
  160. * Return the current position/internal pointer.
  161. *
  162. * @return int|mixed
  163. */
  164. public function key()
  165. {
  166. return $this->position;
  167. }
  168. public function getElement($offset)
  169. {
  170. return (!$this->offsetExists($offset)) ? false : $this->constructResource($this->offsetGet($offset));
  171. }
  172. /**
  173. * @deprecated
  174. */
  175. public function first()
  176. {
  177. Logger::newInstance()->deprecated(__METHOD__, 'getElement');
  178. return $this->getElement(0);
  179. }
  180. /**
  181. * @todo Implement
  182. */
  183. public function sort()
  184. {
  185. }
  186. }