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