MediaWiki  REL1_21
MappedIterator.php
Go to the documentation of this file.
00001 <?php
00029 class MappedIterator implements Iterator {
00031         protected $baseIterator;
00033         protected $vCallback;
00034 
00045     public function __construct( $iter, Closure $vCallback ) {
00046                 if ( is_array( $iter ) ) {
00047                         $this->baseIterator = new ArrayIterator( $iter );
00048                 } elseif ( $iter instanceof Iterator ) {
00049                         $this->baseIterator = $iter;
00050                 } else {
00051                         throw new MWException( "Invalid base iterator provided." );
00052                 }
00053                 $this->vCallback = $vCallback;
00054     }
00055 
00059         public function rewind() {
00060                 $this->baseIterator->rewind();
00061         }
00062 
00066         public function current() {
00067                 if ( !$this->baseIterator->valid() ) {
00068                         return null; // out of range
00069                 }
00070                 return call_user_func_array( $this->vCallback, array( $this->baseIterator->current() ) );
00071         }
00072 
00076         public function key() {
00077                 if ( !$this->baseIterator->valid() ) {
00078                         return null; // out of range
00079                 }
00080                 return $this->baseIterator->key();
00081         }
00082 
00086         public function next() {
00087                 $this->baseIterator->next();
00088         }
00089 
00093         public function valid() {
00094                 return $this->baseIterator->valid();
00095         }
00096 }