SPL-StandardPHPLibrary
multipleiterator.inc
Go to the documentation of this file.
1 <?php
19 class MultipleIterator implements Iterator
20 {
22  private $iterators;
23 
25  private $flags;
26 
28  const MIT_NEED_ANY = 0;
29 
31  const MIT_NEED_ALL = 1;
32 
34  const MIT_KEYS_NUMERIC = 0;
35 
37  const MIT_KEYS_ASSOC = 2;
38 
42  public function __construct($flags = self::MIT_NEED_ALL|self::MIT_KEYS_NUMERIC)
43  {
44  $this->iterators = new SplObjectStorage();
45  $this->flags = $flags;
46  }
47 
49  public function getFlags()
50  {
51  return $this->flags;
52  }
53 
55  public function setFlags($flags)
56  {
57  $this->flags = $flags;
58  }
59 
66  public function attachIterator(Iterator $iter, $inf = NULL)
67  {
68 
69  if (!is_null($inf))
70  {
71  if (!is_int($inf) && !is_string($inf))
72  {
73  throw new IllegalValueException('Inf must be NULL, integer or string');
74  }
75  foreach($this->iterators as $iter)
76  {
77  if ($inf == $this->iterators->getInfo())
78  {
79  throw new IllegalValueException('Key duplication error');
80  }
81  }
82  }
83  $this->iterators->attach($iter, $inf);
84  }
85 
87  public function detachIterator(Iterator $iter)
88  {
89  $this->iterators->detach($iter);
90  }
91 
95  public function containsIterator(Iterator $iter)
96  {
97  return $this->iterator->contains($iter);
98  }
99 
101  public function countIterators()
102  {
103  return $this->iterators->count();
104  }
105 
107  public function rewind()
108  {
109  foreach($this->iterators as $iter)
110  {
111  $iter->rewind();
112  }
113  }
114 
122  public function valid()
123  {
124  if (!sizeof($this->iterators)) {
125  return false;
126  }
127  // The following code is an optimized version that executes as few
128  // valid() calls as necessary and that only checks the flags once.
129  $expect = $this->flags & self::MIT_NEED_ALL ? true : false;
130  foreach($this->iterators as $iter)
131  {
132  if ($expect != $iter->valid())
133  {
134  return !$expect;
135  }
136  }
137  return $expect;
138  }
139 
143  public function next()
144  {
145  foreach($this->iterators as $iter)
146  {
147  $iter->next();
148  }
149  }
150 
157  public function current()
158  {
159  if (!sizeof($this->iterators))
160  {
161  return false;
162  }
163  $retval = array();
164  foreach($this->iterators as $iter)
165  {
166  if ($iter->valid())
167  {
168  if ($this->flags & self::MIT_KEYS_ASSOC)
169  {
170  $key = $this->iterators->getInfo();
171  if (is_null($key))
172  {
173  throw new IllegalValueException('Sub-Iterator is associated with NULL');
174  }
175  $retval[$key] = $iter->current();
176  }
177  else
178  {
179  $retval[] = $iter->current();
180  }
181  }
182  else if ($this->flags & self::MIT_NEED_ALL)
183  {
184  throw new RuntimeException('Called current() with non valid sub iterator');
185  }
186  else
187  {
188  $retval[] = NULL;
189  }
190  }
191  return $retval;
192  }
193 
199  public function key()
200  {
201  if (!sizeof($this->iterators))
202  {
203  return false;
204  }
205  $retval = array();
206  foreach($this->iterators as $iter)
207  {
208  if ($iter->valid())
209  {
210  $retval[] = $iter->key();
211  }
212  else if ($this->flags & self::MIT_NEED_ALL)
213  {
214  throw new LogicException('Called key() with non valid sub iterator');
215  }
216  else
217  {
218  $retval[] = NULL;
219  }
220  }
221  return $retval;
222  }
223 }
$flags
Flags: const MIT_*.
__construct($flags=self::MIT_NEED_ALL|self::MIT_KEYS_NUMERIC)
Construct a new empty MultipleIterator.
const MIT_NEED_ANY
do not require all sub iterators to be valid in iteration
const MIT_KEYS_ASSOC
keys are created from sub iterators associated infromation
$iterators
Inner Iterators.
const MIT_NEED_ALL
require all sub iterators to be valid in iteration
next()
Move all attached Iterator instances forward.
attachIterator(Iterator $iter, $inf=NULL)
Iterator that iterates over several iterators one after the other.
Object storage.
containsIterator(Iterator $iter)
const MIT_KEYS_NUMERIC
keys are created from sub iterators position
Basic iterator.
Definition: spl.php:549
Exception that represents error in the program logic.
Definition: spl.php:353
rewind()
Rewind all attached Iterator instances.
Exception thrown for errors that are only detectable at runtime.
Definition: spl.php:423
detachIterator(Iterator $iter)