File indexing completed on 2024-12-22 05:36:31

0001 <?php
0002 /**
0003  * @category   Zend
0004  * @package    Zend_Cloud
0005  * @subpackage Infrastructure
0006  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0007  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0008  */
0009 
0010 // require_once 'Zend/Cloud/Infrastructure/Instance.php';
0011 
0012 /**
0013  * List of instances
0014  *
0015  * @package    Zend_Cloud
0016  * @subpackage Infrastructure
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  */
0020 class Zend_Cloud_Infrastructure_InstanceList implements Countable, Iterator, ArrayAccess
0021 {
0022     /**
0023      * @var array Array of Zend_Cloud_Infrastructure_Instance
0024      */
0025     protected $instances = array();
0026 
0027     /**
0028      * @var int Iterator key
0029      */
0030     protected $iteratorKey = 0;
0031 
0032     /**
0033      * @var Zend_Cloud_Infrastructure_Adapter
0034      */
0035     protected $adapter;
0036 
0037     /**
0038      * Constructor
0039      *
0040      * @param  Adapter $adapter
0041      * @param  array $instances
0042      * @return void
0043      */
0044     public function __construct($adapter, array $instances = null)
0045     {
0046         if (!($adapter instanceof Zend_Cloud_Infrastructure_Adapter)) {
0047             // require_once 'Zend/Cloud/Infrastructure/Exception.php';
0048             throw new Zend_Cloud_Infrastructure_Exception('You must pass a Zend_Cloud_Infrastructure_Adapter');
0049         }
0050         if (empty($instances)) {
0051             // require_once 'Zend/Cloud/Infrastructure/Exception.php';
0052             throw new Zend_Cloud_Infrastructure_Exception('You must pass an array of Instances');
0053         }
0054 
0055         $this->adapter = $adapter;
0056         $this->constructFromArray($instances);
0057     }
0058 
0059     /**
0060      * Transforms the Array to array of Instances
0061      *
0062      * @param  array $list
0063      * @return void
0064      */
0065     protected function constructFromArray(array $list)
0066     {
0067         foreach ($list as $instance) {
0068             $this->addInstance(new Zend_Cloud_Infrastructure_Instance($this->adapter,$instance));
0069         }
0070     }
0071 
0072     /**
0073      * Add an instance
0074      *
0075      * @param  Instance
0076      * @return InstanceList
0077      */
0078     protected function addInstance(Zend_Cloud_Infrastructure_Instance $instance)
0079     {
0080         $this->instances[] = $instance;
0081         return $this;
0082     }
0083 
0084     /**
0085      * Return number of instances
0086      *
0087      * Implement Countable::count()
0088      *
0089      * @return int
0090      */
0091     public function count()
0092     {
0093         return count($this->instances);
0094     }
0095 
0096     /**
0097      * Return the current element
0098      *
0099      * Implement Iterator::current()
0100      *
0101      * @return Instance
0102      */
0103     public function current()
0104     {
0105         return $this->instances[$this->iteratorKey];
0106     }
0107 
0108     /**
0109      * Return the key of the current element
0110      *
0111      * Implement Iterator::key()
0112      *
0113      * @return int
0114      */
0115     public function key()
0116     {
0117         return $this->iteratorKey;
0118     }
0119 
0120     /**
0121      * Move forward to next element
0122      *
0123      * Implement Iterator::next()
0124      *
0125      * @return void
0126      */
0127     public function next()
0128     {
0129         $this->iteratorKey++;
0130     }
0131 
0132     /**
0133      * Rewind the Iterator to the first element
0134      *
0135      * Implement Iterator::rewind()
0136      *
0137      * @return void
0138      */
0139     public function rewind()
0140     {
0141         $this->iteratorKey = 0;
0142     }
0143 
0144     /**
0145      * Check if there is a current element after calls to rewind() or next()
0146      *
0147      * Implement Iterator::valid()
0148      *
0149      * @return bool
0150      */
0151     public function valid()
0152     {
0153         $numItems = $this->count();
0154         if ($numItems > 0 && $this->iteratorKey < $numItems) {
0155             return true;
0156         }
0157         return false;
0158     }
0159 
0160     /**
0161      * Whether the offset exists
0162      *
0163      * Implement ArrayAccess::offsetExists()
0164      *
0165      * @param  int $offset
0166      * @return bool
0167      */
0168     public function offsetExists($offset)
0169     {
0170         return ($offset < $this->count());
0171     }
0172 
0173     /**
0174      * Return value at given offset
0175      *
0176      * Implement ArrayAccess::offsetGet()
0177      *
0178      * @param  int $offset
0179      * @return Instance
0180      * @throws Zend_Cloud_Infrastructure_Exception
0181      */
0182     public function offsetGet($offset)
0183     {
0184         if (!$this->offsetExists($offset)) {
0185             // require_once 'Zend/Cloud/Infrastructure/Exception.php';
0186             throw new Zend_Cloud_Infrastructure_Exception('Illegal index');
0187         }
0188         return $this->instances[$offset];
0189     }
0190 
0191     /**
0192      * Throws exception because all values are read-only
0193      *
0194      * Implement ArrayAccess::offsetSet()
0195      *
0196      * @param   int     $offset
0197      * @param   string  $value
0198      * @throws  Zend_Cloud_Infrastructure_Exception
0199      */
0200     public function offsetSet($offset, $value)
0201     {
0202         // require_once 'Zend/Cloud/Infrastructure/Exception.php';
0203         throw new Zend_Cloud_Infrastructure_Exception('You are trying to set read-only property');
0204     }
0205 
0206     /**
0207      * Throws exception because all values are read-only
0208      *
0209      * Implement ArrayAccess::offsetUnset()
0210      *
0211      * @param   int     $offset
0212      * @throws  Zend_Cloud_Infrastructure_Exception
0213      */
0214     public function offsetUnset($offset)
0215     {
0216         // require_once 'Zend/Cloud/Infrastructure/Exception.php';
0217         throw new Zend_Cloud_Infrastructure_Exception('You are trying to unset read-only property');
0218     }
0219 }