ImageList.php 4.9 KB

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