SharedIpGroupList.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_Rackspace
  17. * @subpackage Servers
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'Zend/Service/Rackspace/Servers.php';
  22. require_once 'Zend/Service/Rackspace/Servers/SharedIpGroup.php';
  23. /**
  24. * List of shared Ip group of Rackspace
  25. *
  26. * @uses ArrayAccess
  27. * @uses Countable
  28. * @uses Iterator
  29. * @uses Zend_Service_Rackspace_Servers
  30. * @category Zend
  31. * @package Zend_Service_Rackspace
  32. * @subpackage Servers
  33. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Service_Rackspace_Servers_SharedIpGroupList implements Countable, Iterator, ArrayAccess
  37. {
  38. /**
  39. * @var array of Zend_Service_Rackspace_Servers_SharedIpGroup
  40. */
  41. protected $shared = array();
  42. /**
  43. * @var int Iterator key
  44. */
  45. protected $iteratorKey = 0;
  46. /**
  47. * @var Zend_Service_Rackspace_Servers
  48. */
  49. protected $service;
  50. /**
  51. * Construct
  52. *
  53. * @param Zend_Service_Rackspace_Servers $service
  54. * @param array $list
  55. * @return void
  56. */
  57. public function __construct($service,$list = array())
  58. {
  59. if (!($service instanceof Zend_Service_Rackspace_Servers) || !is_array($list)) {
  60. require_once 'Zend/Service/Rackspace/Servers/Exception.php';
  61. throw new Zend_Service_Rackspace_Servers_Exception("You must pass a Zend_Service_Rackspace_Servers object and an array");
  62. }
  63. $this->service= $service;
  64. $this->constructFromArray($list);
  65. }
  66. /**
  67. * Transforms the array to array of Shared Ip Group
  68. *
  69. * @param array $list
  70. * @return void
  71. */
  72. private function constructFromArray(array $list)
  73. {
  74. foreach ($list as $share) {
  75. $this->addSharedIpGroup(new Zend_Service_Rackspace_Servers_SharedIpGroup($this->service,$share));
  76. }
  77. }
  78. /**
  79. * Add a shared Ip group
  80. *
  81. * @param Zend_Service_Rackspace_Servers_SharedIpGroup $shared
  82. * @return Zend_Service_Rackspace_Servers_SharedIpGroupList
  83. */
  84. protected function addSharedIpGroup (Zend_Service_Rackspace_Servers_SharedIpGroup $share)
  85. {
  86. $this->shared[] = $share;
  87. return $this;
  88. }
  89. /**
  90. * To Array
  91. *
  92. * @return array
  93. */
  94. public function toArray()
  95. {
  96. $array= array();
  97. foreach ($this->shared as $share) {
  98. $array[]= $share->toArray();
  99. }
  100. return $array;
  101. }
  102. /**
  103. * Return number of shared Ip Groups
  104. *
  105. * Implement Countable::count()
  106. *
  107. * @return int
  108. */
  109. public function count()
  110. {
  111. return count($this->shared);
  112. }
  113. /**
  114. * Return the current element
  115. *
  116. * Implement Iterator::current()
  117. *
  118. * @return Zend_Service_Rackspace_Servers_SharedIpGroup
  119. */
  120. public function current()
  121. {
  122. return $this->shared[$this->iteratorKey];
  123. }
  124. /**
  125. * Return the key of the current element
  126. *
  127. * Implement Iterator::key()
  128. *
  129. * @return int
  130. */
  131. public function key()
  132. {
  133. return $this->iteratorKey;
  134. }
  135. /**
  136. * Move forward to next element
  137. *
  138. * Implement Iterator::next()
  139. *
  140. * @return void
  141. */
  142. public function next()
  143. {
  144. $this->iteratorKey += 1;
  145. }
  146. /**
  147. * Rewind the Iterator to the first element
  148. *
  149. * Implement Iterator::rewind()
  150. *
  151. * @return void
  152. */
  153. public function rewind()
  154. {
  155. $this->iteratorKey = 0;
  156. }
  157. /**
  158. * Check if there is a current element after calls to rewind() or next()
  159. *
  160. * Implement Iterator::valid()
  161. *
  162. * @return boolean
  163. */
  164. public function valid()
  165. {
  166. $numItems = $this->count();
  167. if ($numItems > 0 && $this->iteratorKey < $numItems) {
  168. return true;
  169. } else {
  170. return false;
  171. }
  172. }
  173. /**
  174. * Whether the offset exists
  175. *
  176. * Implement ArrayAccess::offsetExists()
  177. *
  178. * @param int $offset
  179. * @return boolean
  180. */
  181. public function offsetExists($offset)
  182. {
  183. return ($offset < $this->count());
  184. }
  185. /**
  186. * Return value at given offset
  187. *
  188. * Implement ArrayAccess::offsetGet()
  189. *
  190. * @param int $offset
  191. * @throws Zend_Service_Rackspace_Servers_Exception
  192. * @return Zend_Service_Rackspace_Servers_SharedIpGroup
  193. */
  194. public function offsetGet($offset)
  195. {
  196. if ($this->offsetExists($offset)) {
  197. return $this->shared[$offset];
  198. } else {
  199. require_once 'Zend/Service/Rackspace/Servers/Exception.php';
  200. throw new Zend_Service_Rackspace_Servers_Exception('Illegal index');
  201. }
  202. }
  203. /**
  204. * Throws exception because all values are read-only
  205. *
  206. * Implement ArrayAccess::offsetSet()
  207. *
  208. * @param int $offset
  209. * @param string $value
  210. * @throws Zend_Service_Rackspace_Servers_Exception
  211. */
  212. public function offsetSet($offset, $value)
  213. {
  214. require_once 'Zend/Service/Rackspace/Servers/Exception.php';
  215. throw new Zend_Service_Rackspace_Servers_Exception('You are trying to set read-only property');
  216. }
  217. /**
  218. * Throws exception because all values are read-only
  219. *
  220. * Implement ArrayAccess::offsetUnset()
  221. *
  222. * @param int $offset
  223. * @throws Zend_Service_Rackspace_Servers_Exception
  224. */
  225. public function offsetUnset($offset)
  226. {
  227. require_once 'Zend/Service/Rackspace/Servers/Exception.php';
  228. throw new Zend_Service_Rackspace_Servers_Exception('You are trying to unset read-only property');
  229. }
  230. }