SPL-StandardPHPLibrary
appenditerator.inc
Go to the documentation of this file.
1 <?php
2 
18 class AppendIterator implements OuterIterator
19 {
21  private $iterators;
22 
25  function __construct()
26  {
27  $this->iterators = new ArrayIterator();
28  }
29 
38  function append(Iterator $it)
39  {
40  $this->iterators->append($it);
41  }
42 
45  function getInnerIterator()
46  {
47  return $this->iterators->current();
48  }
49 
53  function rewind()
54  {
55  $this->iterators->rewind();
56  if ($this->iterators->valid())
57  {
58  $this->getInnerIterator()->rewind();
59  }
60  }
61 
64  function valid()
65  {
66  return $this->iterators->valid() && $this->getInnerIterator()->valid();
67  }
68 
71  function current()
72  {
73  /* Using $this->valid() would be exactly the same; it would omit
74  * the access to a non valid element in the inner iterator. Since
75  * the user didn't respect the valid() return value false this
76  * must be intended hence we go on. */
77  return $this->iterators->valid() ? $this->getInnerIterator()->current() : NULL;
78  }
79 
82  function key()
83  {
84  return $this->iterators->valid() ? $this->getInnerIterator()->key() : NULL;
85  }
86 
91  function next()
92  {
93  if (!$this->iterators->valid())
94  {
95  return; /* done all */
96  }
97  $this->getInnerIterator()->next();
98  if ($this->getInnerIterator()->valid())
99  {
100  return; /* found valid element in current inner iterator */
101  }
102  $this->iterators->next();
103  while ($this->iterators->valid())
104  {
105  $this->getInnerIterator()->rewind();
106  if ($this->getInnerIterator()->valid())
107  {
108  return; /* found element as first elemet in another iterator */
109  }
110  $this->iterators->next();
111  }
112  }
113 
116  function __call($func, $params)
117  {
118  return call_user_func_array(array($this->getInnerIterator(), $func), $params);
119  }
120 }
121 
122 ?>
append(Iterator $it)
Append an Iterator.
next()
Move to the next element.
Interface to access the current inner iteraor of iterator wrappers.
$it
Definition: class_tree.php:105
__construct()
Construct an empty AppendIterator.
rewind()
Rewind to the first element of the first inner Iterator.
An Array iterator.
Definition: spl.php:741
__call($func, $params)
Aggregates the inner iterator.
Basic iterator.
Definition: spl.php:549
Iterator that iterates over several iterators one after the other.