SPL-StandardPHPLibrary
dualiterator.inc
Go to the documentation of this file.
00001 <?php
00002 
00017 class DualIterator implements Iterator
00018 {
00019     const CURRENT_LHS   = 0x01;
00020     const CURRENT_RHS   = 0x02;
00021     const CURRENT_ARRAY = 0x03;
00022     const CURRENT_0     = 0x00;
00023 
00024     const KEY_LHS   = 0x10;
00025     const KEY_RHS   = 0x20;
00026     const KEY_0     = 0x00;
00027     
00028     const DEFAULT_FLAGS = 0x13;
00029     
00030     private $lhs;
00031     private $rhs;
00032     private $flags;
00033 
00040     function __construct(Iterator $lhs, Iterator $rhs, 
00041                     $flags = 0x13 /*DualIterator::DEFAULT_FLAGS*/)
00042     {
00043         $this->lhs   = $lhs;
00044         $this->rhs   = $rhs;
00045         $this->flags = $flags;
00046     }
00047 
00050     function getLHS()
00051     {
00052         return $this->lhs;
00053     }
00054 
00057     function getRHS()
00058     {
00059         return $this->rhs;
00060     }
00061 
00064     function setFlags($flags)
00065     {
00066         $this->flags = $flags;
00067     }
00068 
00071     function getFlags()
00072     {
00073         return $this->flags;
00074     }
00075 
00078     function rewind()
00079     {
00080         $this->lhs->rewind();
00081         $this->rhs->rewind();   
00082     }
00083 
00086     function valid()
00087     {
00088         return $this->lhs->valid() && $this->rhs->valid();  
00089     }
00090 
00093     function current()
00094     {
00095         switch($this->flags & 0x0F)
00096         {
00097         default:
00098         case self::CURRENT_ARRAY:
00099             return array($this->lhs->current(), $this->rhs->current());
00100         case self::CURRENT_LHS:
00101             return $this->lhs->current();
00102         case self::CURRENT_RHS:
00103             return $this->rhs->current();
00104         case self::CURRENT_0:
00105             return NULL;
00106         }
00107     }
00108 
00111     function key()
00112     {
00113         switch($this->flags & 0xF0)
00114         {
00115         default:
00116         case self::KEY_LHS:
00117             return $this->lhs->key();
00118         case self::KEY_RHS:
00119             return $this->rhs->key();
00120         case self::KEY_0:
00121             return NULL;
00122         }
00123     }
00124 
00127     function next()
00128     {
00129         $this->lhs->next();
00130         $this->rhs->next();
00131     }
00132 
00136     function areIdentical()
00137     {
00138         return $this->valid()
00139              ? $this->lhs->current() === $this->rhs->current()
00140             && $this->lhs->key()     === $this->rhs->key()
00141              : $this->lhs->valid()   ==  $this->rhs->valid();
00142     }
00143 
00147     function areEqual()
00148     {
00149         return $this->valid()
00150              ? $this->lhs->current() ==  $this->rhs->current()
00151             && $this->lhs->key()     ==  $this->rhs->key()
00152              : $this->lhs->valid()   ==  $this->rhs->valid();
00153     }
00154 
00165     static function compareIterators(Iterator $lhs, Iterator $rhs, 
00166                                      $identical = false)
00167     {
00168         if ($lhs instanceof RecursiveIterator)
00169         {
00170             if ($rhs instanceof RecursiveIterator)
00171             {
00172                 $it = new RecursiveDualIterator($lhs, $rhs, 
00173                                 self::CURRENT_0 | self::KEY_0);
00174                 $it = new RecursiveCompareDualIterator($it);
00175             }
00176             else
00177             {
00178                 return false;
00179             }
00180         }
00181         else
00182         {
00183             $it = new DualIterator($lhs, $rhs, self::CURRENT_0 | self::KEY_0);
00184         }
00185 
00186         if ($identical)
00187         {
00188             foreach($it as $n)
00189             {
00190                 if (!$it->areIdentical())
00191                 {
00192                     return false;
00193                 }
00194             }
00195         }
00196         else
00197         {
00198             foreach($it as $n)
00199             {
00200                 if (!$it->areEqual())
00201                 {
00202                     return false;
00203                 }
00204             }
00205         }
00206         return $identical ? $it->areIdentical() : $it->areEqual();
00207     }
00208 }
00209 
00210 ?>