InstanceList.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend_Cloud
  5. * @subpackage Infrastructure
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. require_once 'Zend/Cloud/Infrastructure/Instance.php';
  10. /**
  11. * List of instances
  12. *
  13. * @package Zend_Cloud
  14. * @subpackage Infrastructure
  15. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  16. * @license http://framework.zend.com/license/new-bsd New BSD License
  17. */
  18. class Zend_Cloud_Infrastructure_InstanceList implements Countable, Iterator, ArrayAccess
  19. {
  20. /**
  21. * @var array Array of Zend_Cloud_Infrastructure_Instance
  22. */
  23. protected $instances = array();
  24. /**
  25. * @var int Iterator key
  26. */
  27. protected $iteratorKey = 0;
  28. /**
  29. * @var Zend_Cloud_Infrastructure_Adapter
  30. */
  31. protected $adapter;
  32. /**
  33. * Constructor
  34. *
  35. * @param Adapter $adapter
  36. * @param array $instances
  37. * @return void
  38. */
  39. public function __construct($adapter, array $instances = null)
  40. {
  41. if (!($adapter instanceof Zend_Cloud_Infrastructure_Adapter)) {
  42. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  43. throw new Zend_Cloud_Infrastructure_Exception('You must pass a Zend_Cloud_Infrastructure_Adapter');
  44. }
  45. if (empty($instances)) {
  46. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  47. throw new Zend_Cloud_Infrastructure_Exception('You must pass an array of Instances');
  48. }
  49. $this->adapter = $adapter;
  50. $this->constructFromArray($instances);
  51. }
  52. /**
  53. * Transforms the Array to array of Instances
  54. *
  55. * @param array $list
  56. * @return void
  57. */
  58. protected function constructFromArray(array $list)
  59. {
  60. foreach ($list as $instance) {
  61. $this->addInstance(new Zend_Cloud_Infrastructure_Instance($this->adapter,$instance));
  62. }
  63. }
  64. /**
  65. * Add an instance
  66. *
  67. * @param Instance
  68. * @return InstanceList
  69. */
  70. protected function addInstance(Zend_Cloud_Infrastructure_Instance $instance)
  71. {
  72. $this->instances[] = $instance;
  73. return $this;
  74. }
  75. /**
  76. * Return number of instances
  77. *
  78. * Implement Countable::count()
  79. *
  80. * @return int
  81. */
  82. public function count()
  83. {
  84. return count($this->instances);
  85. }
  86. /**
  87. * Return the current element
  88. *
  89. * Implement Iterator::current()
  90. *
  91. * @return Instance
  92. */
  93. public function current()
  94. {
  95. return $this->instances[$this->iteratorKey];
  96. }
  97. /**
  98. * Return the key of the current element
  99. *
  100. * Implement Iterator::key()
  101. *
  102. * @return int
  103. */
  104. public function key()
  105. {
  106. return $this->iteratorKey;
  107. }
  108. /**
  109. * Move forward to next element
  110. *
  111. * Implement Iterator::next()
  112. *
  113. * @return void
  114. */
  115. public function next()
  116. {
  117. $this->iteratorKey++;
  118. }
  119. /**
  120. * Rewind the Iterator to the first element
  121. *
  122. * Implement Iterator::rewind()
  123. *
  124. * @return void
  125. */
  126. public function rewind()
  127. {
  128. $this->iteratorKey = 0;
  129. }
  130. /**
  131. * Check if there is a current element after calls to rewind() or next()
  132. *
  133. * Implement Iterator::valid()
  134. *
  135. * @return bool
  136. */
  137. public function valid()
  138. {
  139. $numItems = $this->count();
  140. if ($numItems > 0 && $this->iteratorKey < $numItems) {
  141. return true;
  142. }
  143. return false;
  144. }
  145. /**
  146. * Whether the offset exists
  147. *
  148. * Implement ArrayAccess::offsetExists()
  149. *
  150. * @param int $offset
  151. * @return bool
  152. */
  153. public function offsetExists($offset)
  154. {
  155. return ($offset < $this->count());
  156. }
  157. /**
  158. * Return value at given offset
  159. *
  160. * Implement ArrayAccess::offsetGet()
  161. *
  162. * @param int $offset
  163. * @return Instance
  164. * @throws Zend_Cloud_Infrastructure_Exception
  165. */
  166. public function offsetGet($offset)
  167. {
  168. if (!$this->offsetExists($offset)) {
  169. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  170. throw new Zend_Cloud_Infrastructure_Exception('Illegal index');
  171. }
  172. return $this->instances[$offset];
  173. }
  174. /**
  175. * Throws exception because all values are read-only
  176. *
  177. * Implement ArrayAccess::offsetSet()
  178. *
  179. * @param int $offset
  180. * @param string $value
  181. * @throws Zend_Cloud_Infrastructure_Exception
  182. */
  183. public function offsetSet($offset, $value)
  184. {
  185. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  186. throw new Zend_Cloud_Infrastructure_Exception('You are trying to set read-only property');
  187. }
  188. /**
  189. * Throws exception because all values are read-only
  190. *
  191. * Implement ArrayAccess::offsetUnset()
  192. *
  193. * @param int $offset
  194. * @throws Zend_Cloud_Infrastructure_Exception
  195. */
  196. public function offsetUnset($offset)
  197. {
  198. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  199. throw new Zend_Cloud_Infrastructure_Exception('You are trying to unset read-only property');
  200. }
  201. }