File indexing completed on 2025-01-26 05:25:27
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Service_Rackspace 0017 * @subpackage Servers 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 */ 0021 0022 // require_once 'Zend/Service/Rackspace/Servers.php'; 0023 // require_once 'Zend/Service/Rackspace/Servers/Server.php'; 0024 0025 /** 0026 * List of servers of Rackspace 0027 * 0028 * @uses ArrayAccess 0029 * @uses Countable 0030 * @uses Iterator 0031 * @uses OutOfBoundsException 0032 * @uses Zend_Service_Rackspace_Servers 0033 * @category Zend 0034 * @package Zend_Service_Rackspace 0035 * @subpackage Servers 0036 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0037 * @license http://framework.zend.com/license/new-bsd New BSD License 0038 */ 0039 class Zend_Service_Rackspace_Servers_ServerList implements Countable, Iterator, ArrayAccess 0040 { 0041 /** 0042 * @var array of Zend_Service_Rackspace_Servers_Server 0043 */ 0044 protected $servers = array(); 0045 /** 0046 * @var int Iterator key 0047 */ 0048 protected $iteratorKey = 0; 0049 /** 0050 * @var Zend_Service_Rackspace_Servers 0051 */ 0052 protected $service; 0053 /** 0054 * Construct 0055 * 0056 * @param Zend_Service_Rackspace_Servers $service 0057 * @param array $list 0058 * @return void 0059 */ 0060 public function __construct($service,$list = array()) 0061 { 0062 if (!($service instanceof Zend_Service_Rackspace_Servers) || !is_array($list)) { 0063 // require_once 'Zend/Service/Rackspace/Servers/Exception.php'; 0064 throw new Zend_Service_Rackspace_Servers_Exception("You must pass a Zend_Service_Rackspace_Servers object and an array"); 0065 } 0066 $this->service= $service; 0067 $this->constructFromArray($list); 0068 } 0069 /** 0070 * Transforms the array to array of Server 0071 * 0072 * @param array $list 0073 * @return void 0074 */ 0075 private function constructFromArray(array $list) 0076 { 0077 foreach ($list as $server) { 0078 $this->addServer(new Zend_Service_Rackspace_Servers_Server($this->service,$server)); 0079 } 0080 } 0081 /** 0082 * Add a server 0083 * 0084 * @param Zend_Service_Rackspace_Servers_Server $server 0085 * @return Zend_Service_Rackspace_Servers_ServerList 0086 */ 0087 protected function addServer (Zend_Service_Rackspace_Servers_Server $server) 0088 { 0089 $this->servers[] = $server; 0090 return $this; 0091 } 0092 /** 0093 * To Array 0094 * 0095 * @return array 0096 */ 0097 public function toArray() 0098 { 0099 $array= array(); 0100 foreach ($this->servers as $server) { 0101 $array[]= $server->toArray(); 0102 } 0103 return $array; 0104 } 0105 /** 0106 * Return number of servers 0107 * 0108 * Implement Countable::count() 0109 * 0110 * @return int 0111 */ 0112 public function count() 0113 { 0114 return count($this->servers); 0115 } 0116 /** 0117 * Return the current element 0118 * 0119 * Implement Iterator::current() 0120 * 0121 * @return Zend_Service_Rackspace_Servers_Server 0122 */ 0123 public function current() 0124 { 0125 return $this->servers[$this->iteratorKey]; 0126 } 0127 /** 0128 * Return the key of the current element 0129 * 0130 * Implement Iterator::key() 0131 * 0132 * @return int 0133 */ 0134 public function key() 0135 { 0136 return $this->iteratorKey; 0137 } 0138 /** 0139 * Move forward to next element 0140 * 0141 * Implement Iterator::next() 0142 * 0143 * @return void 0144 */ 0145 public function next() 0146 { 0147 $this->iteratorKey += 1; 0148 } 0149 /** 0150 * Rewind the Iterator to the first element 0151 * 0152 * Implement Iterator::rewind() 0153 * 0154 * @return void 0155 */ 0156 public function rewind() 0157 { 0158 $this->iteratorKey = 0; 0159 } 0160 /** 0161 * Check if there is a current element after calls to rewind() or next() 0162 * 0163 * Implement Iterator::valid() 0164 * 0165 * @return bool 0166 */ 0167 public function valid() 0168 { 0169 $numItems = $this->count(); 0170 if ($numItems > 0 && $this->iteratorKey < $numItems) { 0171 return true; 0172 } else { 0173 return false; 0174 } 0175 } 0176 /** 0177 * Whether the offset exists 0178 * 0179 * Implement ArrayAccess::offsetExists() 0180 * 0181 * @param int $offset 0182 * @return bool 0183 */ 0184 public function offsetExists($offset) 0185 { 0186 return ($offset < $this->count()); 0187 } 0188 /** 0189 * Return value at given offset 0190 * 0191 * Implement ArrayAccess::offsetGet() 0192 * 0193 * @param int $offset 0194 * @throws Zend_Service_Rackspace_Servers_Exception 0195 * @return Zend_Service_Rackspace_Servers_Server 0196 */ 0197 public function offsetGet($offset) 0198 { 0199 if ($this->offsetExists($offset)) { 0200 return $this->servers[$offset]; 0201 } else { 0202 // require_once 'Zend/Service/Rackspace/Servers/Exception.php'; 0203 throw new Zend_Service_Rackspace_Servers_Exception('Illegal index'); 0204 } 0205 } 0206 0207 /** 0208 * Throws exception because all values are read-only 0209 * 0210 * Implement ArrayAccess::offsetSet() 0211 * 0212 * @param int $offset 0213 * @param string $value 0214 * @throws Zend_Service_Rackspace_Servers_Exception 0215 */ 0216 public function offsetSet($offset, $value) 0217 { 0218 // require_once 'Zend/Service/Rackspace/Servers/Exception.php'; 0219 throw new Zend_Service_Rackspace_Servers_Exception('You are trying to set read-only property'); 0220 } 0221 0222 /** 0223 * Throws exception because all values are read-only 0224 * 0225 * Implement ArrayAccess::offsetUnset() 0226 * 0227 * @param int $offset 0228 * @throws Zend_Service_Rackspace_Servers_Exception 0229 */ 0230 public function offsetUnset($offset) 0231 { 0232 // require_once 'Zend/Service/Rackspace/Servers/Exception.php'; 0233 throw new Zend_Service_Rackspace_Servers_Exception('You are trying to unset read-only property'); 0234 } 0235 }