Back to index

php5  5.3.10
limititerator.inc
Go to the documentation of this file.
00001 <?php
00002 
00024 class LimitIterator implements OuterIterator
00025 {
00026        private $it;
00027        private $offset;
00028        private $count;
00029        private $pos;
00030 
00037        function __construct(Iterator $it, $offset = 0, $count = -1)
00038        {
00039               if ($offset < 0) {
00040                      throw new exception('Parameter offset must be > 0');
00041               }
00042               if ($count < 0 && $count != -1) {
00043                      throw new exception('Parameter count must either be -1 or a value greater than or equal to 0');
00044               }
00045               $this->it     = $it;
00046               $this->offset = $offset;
00047               $this->count  = $count;
00048               $this->pos    = 0;
00049        }
00050        
00056        function seek($position) {
00057               if ($position < $this->offset) {
00058                      throw new exception('Cannot seek to '.$position.' which is below offset '.$this->offset);
00059               }
00060               if ($position > $this->offset + $this->count && $this->count != -1) {
00061                      throw new exception('Cannot seek to '.$position.' which is behind offset '.$this->offset.' plus count '.$this->count);
00062               }
00063               if ($this->it instanceof SeekableIterator) {
00064                      $this->it->seek($position);
00065                      $this->pos = $position;
00066               } else {
00067                      while($this->pos < $position && $this->it->valid()) {
00068                             $this->next();
00069                      }
00070               }
00071        }
00072 
00075        function rewind()
00076        {
00077               $this->it->rewind();
00078               $this->pos = 0;
00079               $this->seek($this->offset);
00080        }
00081        
00084        function valid() {
00085               return ($this->count == -1 || $this->pos < $this->offset + $this->count)
00086                       && $this->it->valid();
00087        }
00088        
00091        function key() {
00092               return $this->it->key();
00093        }
00094 
00097        function current() {
00098               return $this->it->current();
00099        }
00100 
00103        function next() {
00104               $this->it->next();
00105               $this->pos++;
00106        }
00107 
00111        function getPosition() {
00112               return $this->pos;
00113        }
00114 
00118        function getInnerIterator()
00119        {
00120               return $this->it;
00121        }
00122 
00128        function __call($func, $params)
00129        {
00130               return call_user_func_array(array($this->it, $func), $params);
00131        }
00132 }
00133 
00134 ?>