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