]> granicus.if.org Git - php/commitdiff
- More examples
authorMarcus Boerger <helly@php.net>
Wed, 28 Apr 2004 21:52:51 +0000 (21:52 +0000)
committerMarcus Boerger <helly@php.net>
Wed, 28 Apr 2004 21:52:51 +0000 (21:52 +0000)
ext/spl/examples/appenditerator.inc
ext/spl/examples/norewinditerator.inc [new file with mode: 0755]
ext/spl/examples/tests/examples.inc
ext/spl/examples/tests/iterators_003.phpt [new file with mode: 0755]
ext/spl/examples/tests/iterators_004.phpt [new file with mode: 0755]

index bd8e3c6fa4027fef9278a321f6726c4e6b26ff69..48cc439ee38a055592e949483636a07404e777b7 100755 (executable)
@@ -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 (executable)
index 0000000..c8d29b9
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+/**
+ * @brief   An Iterator that doesn't call rewind
+ * @author  Marcus Boerger
+ * @version 1.0
+ *
+ */
+class NoRewindIterator implements Iterator
+{
+       protected $it;
+       
+       function __construct(Iterator $it)
+       {
+               $this->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
index da7016e192638c9e53cb45a346a902d0146b29c3..74b652adb521f33c2cf245fb32db049eff0a204c 100755 (executable)
@@ -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 (executable)
index 0000000..f6d047d
--- /dev/null
@@ -0,0 +1,35 @@
+--TEST--
+SPL: NoRweindIterator
+--FILE--
+<?php
+
+require_once('examples.inc');
+
+echo "===Current===\n";
+
+$it = new NoRewindIterator(new ArrayIterator(array(0 => '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===
+<?php exit(0); ?>
+--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 (executable)
index 0000000..83827e2
--- /dev/null
@@ -0,0 +1,68 @@
+--TEST--
+SPL: AppendIterator
+--FILE--
+<?php
+
+require_once('examples.inc');
+
+echo "===Empty===\n";
+
+$it = new AppendIterator;
+
+foreach($it as $key=>$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===
+<?php exit(0); ?>
+--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===