]> granicus.if.org Git - php/commitdiff
- More examples
authorMarcus Boerger <helly@php.net>
Tue, 27 Apr 2004 18:15:00 +0000 (18:15 +0000)
committerMarcus Boerger <helly@php.net>
Tue, 27 Apr 2004 18:15:00 +0000 (18:15 +0000)
ext/spl/examples/emptyiterator.inc [new file with mode: 0755]
ext/spl/examples/infiniteiterator.inc [new file with mode: 0755]

diff --git a/ext/spl/examples/emptyiterator.inc b/ext/spl/examples/emptyiterator.inc
new file mode 100755 (executable)
index 0000000..34b5118
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+/**
+ * @brief   An empty Iterator
+ * @author  Marcus Boerger
+ * @version 1.0
+ *
+ */
+class EmptyIterator implements Iterator
+{
+       function rewind()
+       {
+               // nothing to do
+       }
+
+       function valid()
+       {
+               return false;
+       }
+
+       function current()
+       {
+               throw new Exception('Accessing the value of an EmptyIterator');
+       }
+
+       function key()
+       {
+               throw new Exception('Accessing the key of an EmptyIterator');
+       }
+
+       function next()
+       {
+               // nothing to do
+       }
+}
+
+?>
\ No newline at end of file
diff --git a/ext/spl/examples/infiniteiterator.inc b/ext/spl/examples/infiniteiterator.inc
new file mode 100755 (executable)
index 0000000..d648875
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * @brief   An infinite Iterator
+ * @author  Marcus Boerger
+ * @version 1.0
+ *
+ * This Iterator takes another Iterator and infinitvely iterates it by
+ * rewinding it when its end is reached.
+ *
+ * \note Even an InfiniteIterator stops if its inner Iterator is empty.
+ *
+ \verbatim
+ $it       = new ArrayIterator(array(1,2,3));
+ $infinite = new InfiniteIterator($it);
+ $limit    = new LimitIterator($infinite, 0, 5);
+ foreach($limit as $val=>$key)
+ {
+       echo "$val=>$key\n";
+ }
+ \endverbatim
+ */
+class InfiniteIterator implements Iterator
+{
+       private $it;
+
+       function __construct(Iterator $it)
+       {
+               $this->it = $it;
+       }
+
+       function getInnerIterator()
+       {
+               return $this->it;
+       }
+
+       function rewind()
+       {
+               $this->it->rewind();
+       }
+
+       function valid()
+       {
+               return $this->it->valid();
+       }
+
+       function current()
+       {
+               return $this->it->current();
+       }
+
+       function key()
+       {
+               return $this->it->key();
+       }
+
+       function next()
+       {
+               $this->it->next();
+               if (!$this->it->valid())
+               {
+                       $this->it->rewind();
+               }
+       }
+}
+
+?>
\ No newline at end of file