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