SPL-StandardPHPLibrary
appenditerator.inc
Go to the documentation of this file.
00001 <?php
00002 
00018 class AppendIterator implements OuterIterator
00019 {
00021     private $iterators;
00022 
00025     function __construct()
00026     {
00027         $this->iterators = new ArrayIterator();
00028     }
00029 
00038     function append(Iterator $it)
00039     {
00040         $this->iterators->append($it);
00041     }
00042 
00045     function getInnerIterator()
00046     {
00047         return $this->iterators->current();
00048     }
00049 
00053     function rewind()
00054     {
00055         $this->iterators->rewind();
00056         if ($this->iterators->valid())
00057         {
00058             $this->getInnerIterator()->rewind();
00059         }
00060     }
00061 
00064     function valid()
00065     {
00066         return $this->iterators->valid() && $this->getInnerIterator()->valid();
00067     }
00068 
00071     function current()
00072     {
00073         /* Using $this->valid() would be exactly the same; it would omit
00074          * the access to a non valid element in the inner iterator. Since
00075          * the user didn't respect the valid() return value false this
00076          * must be intended hence we go on. */
00077         return $this->iterators->valid() ? $this->getInnerIterator()->current() : NULL;
00078     }
00079 
00082     function key()
00083     {
00084         return $this->iterators->valid() ? $this->getInnerIterator()->key() : NULL;
00085     }
00086 
00091     function next()
00092     {
00093         if (!$this->iterators->valid())
00094         {
00095             return; /* done all */
00096         }
00097         $this->getInnerIterator()->next();
00098         if ($this->getInnerIterator()->valid())
00099         {
00100             return; /* found valid element in current inner iterator */
00101         }
00102         $this->iterators->next();
00103         while ($this->iterators->valid())
00104         {
00105             $this->getInnerIterator()->rewind();
00106             if ($this->getInnerIterator()->valid())
00107             {
00108                 return; /* found element as first elemet in another iterator */
00109             }
00110             $this->iterators->next();
00111         }
00112     }
00113 
00116     function __call($func, $params)
00117     {
00118         return call_user_func_array(array($this->getInnerIterator(), $func), $params);
00119     }
00120 }
00121 
00122 ?>