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_ARRAY = 0x30;
00027 const KEY_0 = 0x00;
00028
00029 const DEFAULT_FLAGS = 0x33;
00030
00031 private $lhs;
00032 private $rhs;
00033 private $flags;
00034
00041 function __construct(Iterator $lhs, Iterator $rhs,
00042 $flags = 0x33 )
00043 {
00044 $this->lhs = $lhs;
00045 $this->rhs = $rhs;
00046 $this->flags = $flags;
00047 }
00048
00051 function getLHS()
00052 {
00053 return $this->lhs;
00054 }
00055
00058 function getRHS()
00059 {
00060 return $this->rhs;
00061 }
00062
00065 function setFlags($flags)
00066 {
00067 $this->flags = $flags;
00068 }
00069
00072 function getFlags()
00073 {
00074 return $this->flags;
00075 }
00076
00079 function rewind()
00080 {
00081 $this->lhs->rewind();
00082 $this->rhs->rewind();
00083 }
00084
00087 function valid()
00088 {
00089 return $this->lhs->valid() && $this->rhs->valid();
00090 }
00091
00094 function current()
00095 {
00096 switch($this->flags & 0x0F)
00097 {
00098 default:
00099 case self::CURRENT_ARRAY:
00100 return array($this->lhs->current(), $this->rhs->current());
00101 case self::CURRENT_LHS:
00102 return $this->lhs->current();
00103 case self::CURRENT_RHS:
00104 return $this->rhs->current();
00105 case self::CURRENT_0:
00106 return NULL;
00107 }
00108 }
00109
00112 function key()
00113 {
00114 switch($this->flags & 0xF0)
00115 {
00116 default:
00117 case self::CURRENT_ARRAY:
00118 return array($this->lhs->key(), $this->rhs->key());
00119 case self::CURRENT_LHS:
00120 return $this->lhs->key();
00121 case self::CURRENT_RHS:
00122 return $this->rhs->key();
00123 case self::CURRENT_0:
00124 return NULL;
00125 }
00126 }
00127
00130 function next()
00131 {
00132 $this->lhs->next();
00133 $this->rhs->next();
00134 }
00135
00139 function areIdentical()
00140 {
00141 return $this->valid()
00142 ? $this->lhs->current() === $this->rhs->current()
00143 && $this->lhs->key() === $this->rhs->key()
00144 : $this->lhs->valid() == $this->rhs->valid();
00145 }
00146
00150 function areEqual()
00151 {
00152 return $this->valid()
00153 ? $this->lhs->current() == $this->rhs->current()
00154 && $this->lhs->key() == $this->rhs->key()
00155 : $this->lhs->valid() == $this->rhs->valid();
00156 }
00157
00168 static function compareIterators(Iterator $lhs, Iterator $rhs,
00169 $identical = false)
00170 {
00171 if ($lhs instanceof RecursiveIterator)
00172 {
00173 if ($rhs instanceof RecursiveIterator)
00174 {
00175 $it = new RecursiveDualIterator($lhs, $rhs,
00176 self::CURRENT_0 | self::KEY_0);
00177 $it = new RecursiveCompareDualIterator($it);
00178 }
00179 else
00180 {
00181 return false;
00182 }
00183 }
00184 else
00185 {
00186 $it = new DualIterator($lhs, $rhs, self::CURRENT_0 | self::KEY_0);
00187 }
00188
00189 if ($identical)
00190 {
00191 foreach($it as $n)
00192 {
00193 if (!$it->areIdentical())
00194 {
00195 return false;
00196 }
00197 }
00198 }
00199 else
00200 {
00201 foreach($it as $n)
00202 {
00203 if (!$it->areEqual())
00204 {
00205 return false;
00206 }
00207 }
00208 }
00209 return $identical ? $it->areIdentical() : $it->areEqual();
00210 }
00211 }
00212
00213 ?>