SPL-StandardPHPLibrary
dualiterator.inc
Go to the documentation of this file.
1 <?php
2 
17 class DualIterator implements Iterator
18 {
19  const CURRENT_LHS = 0x01;
20  const CURRENT_RHS = 0x02;
21  const CURRENT_ARRAY = 0x03;
22  const CURRENT_0 = 0x00;
23 
24  const KEY_LHS = 0x10;
25  const KEY_RHS = 0x20;
26  const KEY_0 = 0x00;
27 
28  const DEFAULT_FLAGS = 0x13;
29 
30  private $lhs;
31  private $rhs;
32  private $flags;
33 
41  $flags = 0x13 /*DualIterator::DEFAULT_FLAGS*/)
42  {
43  $this->lhs = $lhs;
44  $this->rhs = $rhs;
45  $this->flags = $flags;
46  }
47 
50  function getLHS()
51  {
52  return $this->lhs;
53  }
54 
57  function getRHS()
58  {
59  return $this->rhs;
60  }
61 
64  function setFlags($flags)
65  {
66  $this->flags = $flags;
67  }
68 
71  function getFlags()
72  {
73  return $this->flags;
74  }
75 
78  function rewind()
79  {
80  $this->lhs->rewind();
81  $this->rhs->rewind();
82  }
83 
86  function valid()
87  {
88  return $this->lhs->valid() && $this->rhs->valid();
89  }
90 
93  function current()
94  {
95  switch($this->flags & 0x0F)
96  {
97  default:
98  case self::CURRENT_ARRAY:
99  return array($this->lhs->current(), $this->rhs->current());
100  case self::CURRENT_LHS:
101  return $this->lhs->current();
102  case self::CURRENT_RHS:
103  return $this->rhs->current();
104  case self::CURRENT_0:
105  return NULL;
106  }
107  }
108 
111  function key()
112  {
113  switch($this->flags & 0xF0)
114  {
115  default:
116  case self::KEY_LHS:
117  return $this->lhs->key();
118  case self::KEY_RHS:
119  return $this->rhs->key();
120  case self::KEY_0:
121  return NULL;
122  }
123  }
124 
127  function next()
128  {
129  $this->lhs->next();
130  $this->rhs->next();
131  }
132 
136  function areIdentical()
137  {
138  return $this->valid()
139  ? $this->lhs->current() === $this->rhs->current()
140  && $this->lhs->key() === $this->rhs->key()
141  : $this->lhs->valid() == $this->rhs->valid();
142  }
143 
147  function areEqual()
148  {
149  return $this->valid()
150  ? $this->lhs->current() == $this->rhs->current()
151  && $this->lhs->key() == $this->rhs->key()
152  : $this->lhs->valid() == $this->rhs->valid();
153  }
154 
166  $identical = false)
167  {
168  if ($lhs instanceof RecursiveIterator)
169  {
170  if ($rhs instanceof RecursiveIterator)
171  {
172  $it = new RecursiveDualIterator($lhs, $rhs,
173  self::CURRENT_0 | self::KEY_0);
175  }
176  else
177  {
178  return false;
179  }
180  }
181  else
182  {
183  $it = new DualIterator($lhs, $rhs, self::CURRENT_0 | self::KEY_0);
184  }
185 
186  if ($identical)
187  {
188  foreach($it as $n)
189  {
190  if (!$it->areIdentical())
191  {
192  return false;
193  }
194  }
195  }
196  else
197  {
198  foreach($it as $n)
199  {
200  if (!$it->areEqual())
201  {
202  return false;
203  }
204  }
205  }
206  return $identical ? $it->areIdentical() : $it->areEqual();
207  }
208 }
209 
210 ?>
__construct(Iterator $lhs, Iterator $rhs, $flags=0x13)
construct iterator from two iterators
Recursive comparison iterator for a RecursiveDualIterator.
$it
Definition: class_tree.php:105
setFlags($flags)
Interface for recursive iteration with RecursiveIteratorIterator.
const CURRENT_ARRAY
const DEFAULT_FLAGS
Basic iterator.
Definition: spl.php:549
rewind()
rewind both inner iterators
Synchronous iteration over two recursive iterators.
const CURRENT_RHS
static compareIterators(Iterator $lhs, Iterator $rhs, $identical=false)
Compare two iterators.
next()
move both inner iterators forward
const CURRENT_LHS
Synchronous iteration over two iterators.