Source for file Iterator.class.php

Documentation is available at Iterator.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Iterator
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Iterator Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Iterator
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Iterator Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Iterator");
  34.  
  35. class Gumbo_Iterator implements Gumbo_Interface_Iterator {
  36.     
  37.     /** @var array $_list array of elements */
  38.     private $_list = array ();
  39.     /** @var array $_keys array of keys */
  40.     private $_keys = array ();
  41.     
  42.     /** @var int $_current current incremental element of loop */
  43.     private $_current = 0;
  44.     
  45.     
  46.     
  47.     /**
  48.      * Constructor
  49.      * 
  50.      * The constructor requires an array to be given.  The object will
  51.      * save the keys, keeping array references on the original array.  The
  52.      * supplied array can contain any type of values.
  53.      * 
  54.      * @param array|Gumbo_Interface_List$list array to loop through
  55.      * @throws Gumbo_Exception
  56.      */
  57.     public function __construct ($list{
  58.         try {
  59.             // verify precondition
  60.             if (!is_array ($list&& !is_object ($list)) {
  61.                 throw new Gumbo_Exception ("Invalid Argument 'list:arr' => {$list}:gettype ($list));
  62.             }
  63.             if (is_object ($list&& !($list instanceof Gumbo_Interface_List)) {
  64.                 throw new Gumbo_Exception ("Invalid Argument 'list:Gumbo_Interface_List' => {$list}:getType ($list));
  65.             }
  66.             
  67.             if (is_array ($list)) {
  68.                 $this->_keys = array_keys ($list);
  69.                 $this->_list = $list;
  70.             else {
  71.                 $this->_keys = $list->keys ();
  72.                 $this->_list = $list->getAll ();
  73.             }
  74.         catch (Gumbo_Exception $e{
  75.             $e->setFunction (__METHOD__);
  76.             gumbo_trigger ($e);
  77.         }
  78.     }
  79.     
  80.     
  81.     
  82.     /** ACTION METHODS **/
  83.     /**
  84.      * Returns the current element
  85.      * @return mixed 
  86.      */
  87.     public function current ({
  88.         if (!$this->valid ()) {
  89.             return null;
  90.         }
  91.         return $this->_list [$this->key ()];
  92.     }
  93.     
  94.     /**
  95.      * Returns the current key value
  96.      * @return int 
  97.      */
  98.     public function key ({
  99.         if (!isset ($this->_keys [$this->_current])) {
  100.             return null;
  101.         }
  102.         return $this->_keys [$this->_current];
  103.     }
  104.     
  105.     /**
  106.      * Moves the target to the next element
  107.      */
  108.     public function next ({
  109.         $this->_current++;
  110.     }
  111.     
  112.     /**
  113.      * Resets the Iterator
  114.      */
  115.     public function rewind ({
  116.         $this->_current = 0;
  117.     }
  118.     
  119.     /**
  120.      * Checks if the current element is valid
  121.      * @return bool 
  122.      */
  123.     public function valid ({
  124.         if (count ($this->_list<= 0{
  125.             return false;
  126.         }
  127.         if (is_null ($this->key ())) {
  128.             return false;
  129.         }
  130.         return isset ($this->_list [$this->key ()]);
  131.     }
  132.     
  133.     
  134.     
  135.     /** ACCESSOR METHODS **/
  136.     /**
  137.      * Returns the array of keys in the object
  138.      * @return array 
  139.      */
  140.     public function getKeys ({
  141.         return $this->_keys;
  142.     }
  143.     
  144.     /**
  145.      * Returns the full array list
  146.      * @return array 
  147.      */
  148.     public function getList ({
  149.         return $this->_list;
  150.     }
  151.     
  152.     /**
  153.      * Returns the current incremental element number
  154.      * @return int 
  155.      */
  156.     public function getCurrent ({
  157.         return $this->_current;
  158.     }
  159.     
  160.     /**
  161.      * Returns the total number of elements in the Iterator
  162.      * @return int 
  163.      */
  164.     public function getTotal ({
  165.         return count ($this->getList ());
  166.     }
  167.     
  168. }
  169.  
  170. ?>