File indexing completed on 2024-12-29 05:28:02

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