summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcus Boerger <helly@php.net>2004-04-26 22:01:12 +0000
committerMarcus Boerger <helly@php.net>2004-04-26 22:01:12 +0000
commitb5262204622b69d44eaa2bda9012a144cd52088e (patch)
tree818f465fdbeec7899b85f3e292e99217fc13b00b
parent5ffeb236f404159fc13b3243dc4adac82616d45a (diff)
downloadphp-git-b5262204622b69d44eaa2bda9012a144cd52088e.tar.gz
Complete implementation
-rwxr-xr-xext/spl/examples/appenditerator.inc32
1 files changed, 26 insertions, 6 deletions
diff --git a/ext/spl/examples/appenditerator.inc b/ext/spl/examples/appenditerator.inc
index 3e04019a60..bd8e3c6fa4 100755
--- a/ext/spl/examples/appenditerator.inc
+++ b/ext/spl/examples/appenditerator.inc
@@ -28,6 +28,10 @@ class AppendIterator implements Iterator
function rewind()
{
$this->iterators->rewind();
+ if ($this->iterators->valid())
+ {
+ $this->iterators->rewind();
+ }
}
function valid()
@@ -37,20 +41,36 @@ class AppendIterator implements Iterator
function current()
{
- return $this->getInnerIterator()->current();
+ /* Using $this->valid() would be exactly the same; it would omit
+ * the access to a non valid element in the inner iterator. Since
+ * the user didn't respect the valid() return value false this
+ * must be intended hence we go on. */
+ return $this->iterators->valid() ? $this->getInnerIterator()->current() : NULL;
}
function key()
{
- return $this->getInnerIterator()->key();
+ return $this->iterators->valid() ? $this->getInnerIterator()->key() : NULL;
}
function next()
{
- while($this->iterators->valid()) {
- $this->getInnerIterator()->next();
- if ($this->valid()) {
- return;
+ if (!$this->iterators->valid())
+ {
+ return; /* done all */
+ }
+ $this->getInnerIterator()->next();
+ if ($this->getInnerIterator()->valid())
+ {
+ return; /* found valid element in current inner iterator */
+ }
+ $this->iterators->next();
+ while ($this->iterators->valid())
+ {
+ $this->getInnerIterator()->rewind();
+ if ($this->getInnerIterator()->valid())
+ {
+ return; /* found element as first elemet in another iterator */
}
$this->iterators->next();
}