ObjectList.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/Object.php';
  22. require_once 'Zend/Service/Rackspace/Files.php';
  23. /**
  24. * List of servers retrived from the GoGrid web service
  25. *
  26. * @uses ArrayAccess
  27. * @uses Countable
  28. * @uses Iterator
  29. * @uses OutOfBoundsException
  30. * @uses Zend_Service_Rackspace_Files
  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_ObjectList implements Countable, Iterator, ArrayAccess
  38. {
  39. /**
  40. * @var array of Zend_Service_Rackspace_Files_Object
  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. * The container name of the object list
  53. *
  54. * @var string
  55. */
  56. protected $container;
  57. /**
  58. * Construct
  59. *
  60. * @param array $list
  61. * @return boolean
  62. */
  63. public function __construct($service,$list,$container)
  64. {
  65. if (!($service instanceof Zend_Service_Rackspace_Files)) {
  66. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  67. throw new Zend_Service_Rackspace_Files_Exception("You must pass a Zend_Service_Rackspace_Files object");
  68. }
  69. if (empty($list)) {
  70. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  71. throw new Zend_Service_Rackspace_Files_Exception("You must pass an array of data objects");
  72. }
  73. if (empty($container)) {
  74. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  75. throw new Zend_Service_Rackspace_Files_Exception("You must pass the container of the object list");
  76. }
  77. $this->service= $service;
  78. $this->container= $container;
  79. $this->_constructFromArray($list);
  80. }
  81. /**
  82. * Transforms the Array to array of container
  83. *
  84. * @param array $list
  85. * @return void
  86. */
  87. private function _constructFromArray(array $list)
  88. {
  89. foreach ($list as $obj) {
  90. $obj['container']= $this->container;
  91. $this->_addObject(new Zend_Service_Rackspace_Files_Object($this->service,$obj));
  92. }
  93. }
  94. /**
  95. * Add an object
  96. *
  97. * @param Zend_Service_Rackspace_Files_Object $obj
  98. * @return Zend_Service_Rackspace_Files_ObjectList
  99. */
  100. protected function _addObject (Zend_Service_Rackspace_Files_Object $obj)
  101. {
  102. $this->objects[] = $obj;
  103. return $this;
  104. }
  105. /**
  106. * Return number of servers
  107. *
  108. * Implement Countable::count()
  109. *
  110. * @return int
  111. */
  112. public function count()
  113. {
  114. return count($this->objects);
  115. }
  116. /**
  117. * Return the current element
  118. *
  119. * Implement Iterator::current()
  120. *
  121. * @return Zend_Service_Rackspace_Files_Object
  122. */
  123. public function current()
  124. {
  125. return $this->objects[$this->iteratorKey];
  126. }
  127. /**
  128. * Return the key of the current element
  129. *
  130. * Implement Iterator::key()
  131. *
  132. * @return int
  133. */
  134. public function key()
  135. {
  136. return $this->iteratorKey;
  137. }
  138. /**
  139. * Move forward to next element
  140. *
  141. * Implement Iterator::next()
  142. *
  143. * @return void
  144. */
  145. public function next()
  146. {
  147. $this->iteratorKey += 1;
  148. }
  149. /**
  150. * Rewind the Iterator to the first element
  151. *
  152. * Implement Iterator::rewind()
  153. *
  154. * @return void
  155. */
  156. public function rewind()
  157. {
  158. $this->iteratorKey = 0;
  159. }
  160. /**
  161. * Check if there is a current element after calls to rewind() or next()
  162. *
  163. * Implement Iterator::valid()
  164. *
  165. * @return bool
  166. */
  167. public function valid()
  168. {
  169. $numItems = $this->count();
  170. if ($numItems > 0 && $this->iteratorKey < $numItems) {
  171. return true;
  172. } else {
  173. return false;
  174. }
  175. }
  176. /**
  177. * Whether the offset exists
  178. *
  179. * Implement ArrayAccess::offsetExists()
  180. *
  181. * @param int $offset
  182. * @return bool
  183. */
  184. public function offsetExists($offset)
  185. {
  186. return ($offset < $this->count());
  187. }
  188. /**
  189. * Return value at given offset
  190. *
  191. * Implement ArrayAccess::offsetGet()
  192. *
  193. * @param int $offset
  194. * @throws Zend_Service_Rackspace_Files_Exception
  195. * @return Zend_Service_Rackspace_Files_Object
  196. */
  197. public function offsetGet($offset)
  198. {
  199. if ($this->offsetExists($offset)) {
  200. return $this->objects[$offset];
  201. } else {
  202. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  203. throw new Zend_Service_Rackspace_Files_Exception('Illegal index');
  204. }
  205. }
  206. /**
  207. * Throws exception because all values are read-only
  208. *
  209. * Implement ArrayAccess::offsetSet()
  210. *
  211. * @param int $offset
  212. * @param string $value
  213. * @throws Zend_Service_Rackspace_Files_Exception
  214. */
  215. public function offsetSet($offset, $value)
  216. {
  217. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  218. throw new Zend_Service_Rackspace_Files_Exception('You are trying to set read-only property');
  219. }
  220. /**
  221. * Throws exception because all values are read-only
  222. *
  223. * Implement ArrayAccess::offsetUnset()
  224. *
  225. * @param int $offset
  226. * @throws Zend_Service_Rackspace_Files_Exception
  227. */
  228. public function offsetUnset($offset)
  229. {
  230. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  231. throw new Zend_Service_Rackspace_Files_Exception('You are trying to unset read-only property');
  232. }
  233. }