ContainerList.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 Files
  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. require_once 'Zend/Service/Rackspace/Files/Container.php';
  22. require_once 'Zend/Service/Rackspace/Files.php';
  23. /**
  24. * List of servers retrived from the Rackspace web service
  25. *
  26. * @uses ArrayAccess
  27. * @uses Countable
  28. * @uses Iterator
  29. * @uses OutOfBoundsException
  30. * @uses Zend_Service_Rackspace_Files_Container
  31. * @category Zend
  32. * @package Zend_Service_Rackspace
  33. * @subpackage Files
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Service_Rackspace_Files_ContainerList implements Countable, Iterator, ArrayAccess
  38. {
  39. /**
  40. * @var array Array of Zend_Service_Rackspace_Files_Container
  41. */
  42. protected $objects = array();
  43. /**
  44. * @var int Iterator key
  45. */
  46. protected $iteratorKey = 0;
  47. /**
  48. * @var RackspaceFiles
  49. */
  50. protected $service;
  51. /**
  52. * Constructor
  53. *
  54. * @param array $list
  55. * @return boolean
  56. */
  57. public function __construct($service,$list = array())
  58. {
  59. if (!($service instanceof Zend_Service_Rackspace_Files ) || !is_array($list)) {
  60. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  61. throw new Zend_Service_Rackspace_Files_Exception("You must pass a Zend_Service_Rackspace_Files_Exception object and an array");
  62. }
  63. $this->service= $service;
  64. $this->_constructFromArray($list);
  65. }
  66. /**
  67. * Transforms the Array to array of container
  68. *
  69. * @param array $list
  70. * @return void
  71. */
  72. private function _constructFromArray(array $list)
  73. {
  74. foreach ($list as $container) {
  75. $this->_addObject(new Zend_Service_Rackspace_Files_Container($this->service,$container));
  76. }
  77. }
  78. /**
  79. * Add an object
  80. *
  81. * @param Zend_Service_Rackspace_Files_Container $obj
  82. * @return Zend_Service_Rackspace_Files_ContainerList
  83. */
  84. protected function _addObject (Zend_Service_Rackspace_Files_Container $obj)
  85. {
  86. $this->objects[] = $obj;
  87. return $this;
  88. }
  89. /**
  90. * Return number of servers
  91. *
  92. * Implement Countable::count()
  93. *
  94. * @return int
  95. */
  96. public function count()
  97. {
  98. return count($this->objects);
  99. }
  100. /**
  101. * Return the current element
  102. *
  103. * Implement Iterator::current()
  104. *
  105. * @return Zend_Service_Rackspace_Files_Container
  106. */
  107. public function current()
  108. {
  109. return $this->objects[$this->iteratorKey];
  110. }
  111. /**
  112. * Return the key of the current element
  113. *
  114. * Implement Iterator::key()
  115. *
  116. * @return int
  117. */
  118. public function key()
  119. {
  120. return $this->iteratorKey;
  121. }
  122. /**
  123. * Move forward to next element
  124. *
  125. * Implement Iterator::next()
  126. *
  127. * @return void
  128. */
  129. public function next()
  130. {
  131. $this->iteratorKey += 1;
  132. }
  133. /**
  134. * Rewind the Iterator to the first element
  135. *
  136. * Implement Iterator::rewind()
  137. *
  138. * @return void
  139. */
  140. public function rewind()
  141. {
  142. $this->iteratorKey = 0;
  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. } else {
  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. * Return value at given offset
  174. *
  175. * Implement ArrayAccess::offsetGet()
  176. *
  177. * @param int $offset
  178. * @throws Zend_Service_Rackspace_Files_Exception
  179. * @return Zend_Service_Rackspace_Files_Container
  180. */
  181. public function offsetGet($offset)
  182. {
  183. if ($this->offsetExists($offset)) {
  184. return $this->objects[$offset];
  185. } else {
  186. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  187. throw new Zend_Service_Rackspace_Files_Exception('Illegal index');
  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_Service_Rackspace_Files_Exception
  198. */
  199. public function offsetSet($offset, $value)
  200. {
  201. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  202. throw new Zend_Service_Rackspace_Files_Exception('You are trying to set read-only property');
  203. }
  204. /**
  205. * Throws exception because all values are read-only
  206. *
  207. * Implement ArrayAccess::offsetUnset()
  208. *
  209. * @param int $offset
  210. * @throws Zend_Service_Rackspace_Files_Exception
  211. */
  212. public function offsetUnset($offset)
  213. {
  214. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  215. throw new Zend_Service_Rackspace_Files_Exception('You are trying to unset read-only property');
  216. }
  217. }