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