From: Marcus Boerger Date: Wed, 28 Apr 2004 21:52:51 +0000 (+0000) Subject: - More examples X-Git-Tag: RELEASE_0_1~339 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1721e5d44e9d8d69fd23207cf707188d01b01576;p=php - More examples --- diff --git a/ext/spl/examples/appenditerator.inc b/ext/spl/examples/appenditerator.inc index bd8e3c6fa4..48cc439ee3 100755 --- a/ext/spl/examples/appenditerator.inc +++ b/ext/spl/examples/appenditerator.inc @@ -18,6 +18,11 @@ class AppendIterator implements Iterator function append(Iterator $it) { $this->iterators->append($it); + if (!$this->valid()) + { + $this->iterators->seek($this->iterators->count() - 1); + $this->iterators->current()->rewind(); + } } function getInnerIterator() @@ -30,7 +35,7 @@ class AppendIterator implements Iterator $this->iterators->rewind(); if ($this->iterators->valid()) { - $this->iterators->rewind(); + $this->getInnerIterator()->rewind(); } } diff --git a/ext/spl/examples/norewinditerator.inc b/ext/spl/examples/norewinditerator.inc new file mode 100755 index 0000000000..c8d29b96ea --- /dev/null +++ b/ext/spl/examples/norewinditerator.inc @@ -0,0 +1,44 @@ +it = $it; + } + + function rewind() + { + // nothing to do + } + + function valid() + { + return $this->it->valid(); + } + + function current() + { + return $this->it->current(); + } + + function key() + { + return $this->it->key(); + } + + function next() + { + $this->it->next(); + } +} + +?> \ No newline at end of file diff --git a/ext/spl/examples/tests/examples.inc b/ext/spl/examples/tests/examples.inc index da7016e192..74b652adb5 100755 --- a/ext/spl/examples/tests/examples.inc +++ b/ext/spl/examples/tests/examples.inc @@ -15,6 +15,8 @@ class IncludeFiles extends ArrayIterator $classes = array( 'EmptyIterator', 'InfiniteIterator', + 'AppendIterator', + 'NoRewindIterator', ); foreach (new IncludeFiles(dirname(__FILE__). '/..', $classes) as $file) diff --git a/ext/spl/examples/tests/iterators_003.phpt b/ext/spl/examples/tests/iterators_003.phpt new file mode 100755 index 0000000000..f6d047de75 --- /dev/null +++ b/ext/spl/examples/tests/iterators_003.phpt @@ -0,0 +1,35 @@ +--TEST-- +SPL: NoRweindIterator +--FILE-- + 'A', 1 => 'B', 2 => 'C'))); + +echo $it->key() . '=>' . $it->current() . "\n"; + +echo "===Next===\n"; + +$it->next(); + +echo "===Foreach===\n"; + +foreach($it as $key=>$val) +{ + echo "$key=>$val\n"; +} + +?> +===DONE=== + +--EXPECTF-- +===Current=== +0=>A +===Next=== +===Foreach=== +1=>B +2=>C +===DONE=== diff --git a/ext/spl/examples/tests/iterators_004.phpt b/ext/spl/examples/tests/iterators_004.phpt new file mode 100755 index 0000000000..83827e29ba --- /dev/null +++ b/ext/spl/examples/tests/iterators_004.phpt @@ -0,0 +1,68 @@ +--TEST-- +SPL: AppendIterator +--FILE-- +$val) +{ + echo "$key=>$val\n"; +} + +echo "===Append===\n"; + +$it->append(new ArrayIterator(array(0 => 'A', 1 => 'B'))); + +foreach($it as $key=>$val) +{ + echo "$key=>$val\n"; +} + +echo "===Rewind===\n"; + +foreach($it as $key=>$val) +{ + echo "$key=>$val\n"; +} + +echo "===Append===\n"; + +$it->append(new ArrayIterator(array(2 => 'C', 3 => 'D'))); + +foreach(new NoRewindIterator($it) as $key=>$val) +{ + echo "$key=>$val\n"; +} + +echo "===Rewind===\n"; + +foreach($it as $key=>$val) +{ + echo "$key=>$val\n"; +} + +?> +===DONE=== + +--EXPECTF-- +===Empty=== +===Append=== +0=>A +1=>B +===Rewind=== +0=>A +1=>B +===Append=== +2=>C +3=>D +===Rewind=== +0=>A +1=>B +2=>C +3=>D +===DONE===