web/lib/Zend/Service/Rackspace/Servers/SharedIpGroupList.php
changeset 808 6b6c2214f778
child 1230 68c69c656a2c
equal deleted inserted replaced
807:877f952ae2bd 808:6b6c2214f778
       
     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 Servers
       
    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 
       
    22 require_once 'Zend/Service/Rackspace/Servers.php';
       
    23 require_once 'Zend/Service/Rackspace/Servers/SharedIpGroup.php';
       
    24 
       
    25 /**
       
    26  * List of shared Ip group of Rackspace
       
    27  *
       
    28  * @uses       ArrayAccess
       
    29  * @uses       Countable
       
    30  * @uses       Iterator
       
    31  * @uses       Zend_Service_Rackspace_Servers
       
    32  * @category   Zend
       
    33  * @package    Zend_Service_Rackspace
       
    34  * @subpackage Servers
       
    35  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 class Zend_Service_Rackspace_Servers_SharedIpGroupList implements Countable, Iterator, ArrayAccess
       
    39 {
       
    40     /**
       
    41      * @var array of Zend_Service_Rackspace_Servers_SharedIpGroup
       
    42      */
       
    43     protected $shared = array();
       
    44     /**
       
    45      * @var int Iterator key
       
    46      */
       
    47     protected $iteratorKey = 0;
       
    48     /**
       
    49      * @var Zend_Service_Rackspace_Servers
       
    50      */
       
    51     protected $service;
       
    52     /**
       
    53      * Construct
       
    54      *
       
    55      * @param  Zend_Service_Rackspace_Servers $service
       
    56      * @param  array $list
       
    57      * @return void
       
    58      */
       
    59     public function __construct($service,$list = array())
       
    60     {
       
    61         if (!($service instanceof Zend_Service_Rackspace_Servers) || !is_array($list)) {
       
    62             require_once 'Zend/Service/Rackspace/Servers/Exception.php';
       
    63             throw new Zend_Service_Rackspace_Servers_Exception("You must pass a Zend_Service_Rackspace_Servers object and an array");
       
    64         }
       
    65         $this->service= $service;
       
    66         $this->constructFromArray($list);
       
    67     }
       
    68     /**
       
    69      * Transforms the array to array of Shared Ip Group
       
    70      *
       
    71      * @param  array $list
       
    72      * @return void
       
    73      */
       
    74     private function constructFromArray(array $list)
       
    75     {
       
    76         foreach ($list as $share) {
       
    77             $this->addSharedIpGroup(new Zend_Service_Rackspace_Servers_SharedIpGroup($this->service,$share));
       
    78         }
       
    79     }
       
    80     /**
       
    81      * Add a shared Ip group
       
    82      *
       
    83      * @param  Zend_Service_Rackspace_Servers_SharedIpGroup $shared
       
    84      * @return Zend_Service_Rackspace_Servers_SharedIpGroupList
       
    85      */
       
    86     protected function addSharedIpGroup (Zend_Service_Rackspace_Servers_SharedIpGroup $share)
       
    87     {
       
    88         $this->shared[] = $share;
       
    89         return $this;
       
    90     }
       
    91     /**
       
    92      * To Array
       
    93      * 
       
    94      * @return array 
       
    95      */
       
    96     public function toArray()
       
    97     {
       
    98         $array= array();
       
    99         foreach ($this->shared as $share) {
       
   100             $array[]= $share->toArray();
       
   101         }
       
   102         return $array;
       
   103     }
       
   104     /**
       
   105      * Return number of shared Ip Groups
       
   106      *
       
   107      * Implement Countable::count()
       
   108      *
       
   109      * @return int
       
   110      */
       
   111     public function count()
       
   112     {
       
   113         return count($this->shared);
       
   114     }
       
   115     /**
       
   116      * Return the current element
       
   117      *
       
   118      * Implement Iterator::current()
       
   119      *
       
   120      * @return Zend_Service_Rackspace_Servers_SharedIpGroup
       
   121      */
       
   122     public function current()
       
   123     {
       
   124         return $this->shared[$this->iteratorKey];
       
   125     }
       
   126     /**
       
   127      * Return the key of the current element
       
   128      *
       
   129      * Implement Iterator::key()
       
   130      *
       
   131      * @return int
       
   132      */
       
   133     public function key()
       
   134     {
       
   135         return $this->iteratorKey;
       
   136     }
       
   137     /**
       
   138      * Move forward to next element
       
   139      *
       
   140      * Implement Iterator::next()
       
   141      *
       
   142      * @return void
       
   143      */
       
   144     public function next()
       
   145     {
       
   146         $this->iteratorKey += 1;
       
   147     }
       
   148     /**
       
   149      * Rewind the Iterator to the first element
       
   150      *
       
   151      * Implement Iterator::rewind()
       
   152      *
       
   153      * @return void
       
   154      */
       
   155     public function rewind()
       
   156     {
       
   157         $this->iteratorKey = 0;
       
   158     }
       
   159     /**
       
   160      * Check if there is a current element after calls to rewind() or next()
       
   161      *
       
   162      * Implement Iterator::valid()
       
   163      *
       
   164      * @return boolean
       
   165      */
       
   166     public function valid()
       
   167     {
       
   168         $numItems = $this->count();
       
   169         if ($numItems > 0 && $this->iteratorKey < $numItems) {
       
   170             return true;
       
   171         } else {
       
   172             return false;
       
   173         }
       
   174     }
       
   175     /**
       
   176      * Whether the offset exists
       
   177      *
       
   178      * Implement ArrayAccess::offsetExists()
       
   179      *
       
   180      * @param   int     $offset
       
   181      * @return  boolean
       
   182      */
       
   183     public function offsetExists($offset)
       
   184     {
       
   185         return ($offset < $this->count());
       
   186     }
       
   187     /**
       
   188      * Return value at given offset
       
   189      *
       
   190      * Implement ArrayAccess::offsetGet()
       
   191      *
       
   192      * @param   int  $offset
       
   193      * @throws  Zend_Service_Rackspace_Servers_Exception
       
   194      * @return  Zend_Service_Rackspace_Servers_SharedIpGroup
       
   195      */
       
   196     public function offsetGet($offset)
       
   197     {
       
   198         if ($this->offsetExists($offset)) {
       
   199             return $this->shared[$offset];
       
   200         } else {
       
   201             require_once 'Zend/Service/Rackspace/Servers/Exception.php';
       
   202             throw new Zend_Service_Rackspace_Servers_Exception('Illegal index');
       
   203         }
       
   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_Servers_Exception
       
   214      */
       
   215     public function offsetSet($offset, $value)
       
   216     {
       
   217         require_once 'Zend/Service/Rackspace/Servers/Exception.php';
       
   218         throw new Zend_Service_Rackspace_Servers_Exception('You are trying to set read-only property');
       
   219     }
       
   220 
       
   221     /**
       
   222      * Throws exception because all values are read-only
       
   223      *
       
   224      * Implement ArrayAccess::offsetUnset()
       
   225      *
       
   226      * @param  int $offset
       
   227      * @throws Zend_Service_Rackspace_Servers_Exception
       
   228      */
       
   229     public function offsetUnset($offset)
       
   230     {
       
   231         require_once 'Zend/Service/Rackspace/Servers/Exception.php';
       
   232         throw new Zend_Service_Rackspace_Servers_Exception('You are trying to unset read-only property');
       
   233     }
       
   234 }