Add PlIteratorUtils::foreachIterator() to build a PHP's Iterator from a
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 28 Sep 2010 08:11:52 +0000 (10:11 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 28 Sep 2010 08:11:52 +0000 (10:11 +0200)
PlIterator.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/pliteratorutils.php
ut/spliteratortest.php [new file with mode: 0644]

index c9bf046..875aac7 100644 (file)
@@ -135,6 +135,13 @@ class PlIteratorUtils
         $callback = new _GetObjectPropertyCallback($property);
         return array($callback, 'get');
     }
+
+    /** Returns a wrapper around the PlIterator suitable for foreach() iterations
+     */
+    public static function foreachIterator(PlIterator $iterator)
+    {
+        return new SPLIterator($iterator);
+    }
 }
 
 /** Empty iterator
@@ -803,5 +810,48 @@ class _GetObjectPropertyCallback
     }
 }
 
+// Wrapper class to build a SPL iterator from a PlIterator
+class SPLIterator implements Iterator
+{
+    private $it;
+    private $pos;
+    private $value;
+
+    public function __construct(PlIterator $it)
+    {
+        $this->it = $it;
+        $this->pos = 0;
+        $this->value = $this->it->next();
+    }
+
+    public function rewind()
+    {
+        if ($this->pos != 0) {
+            throw new Exception("Rewind not supported on this iterator");
+        }
+    }
+
+    public function current()
+    {
+        return $this->value;
+    }
+
+    public function key()
+    {
+        return $this->pos;
+    }
+
+    public function next()
+    {
+        ++$this->pos;
+        $this->value = $this->it->next();
+    }
+
+    public function valid()
+    {
+        return !!$this->value;
+    }
+}
+
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>
diff --git a/ut/spliteratortest.php b/ut/spliteratortest.php
new file mode 100644 (file)
index 0000000..87f9c89
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+require_once dirname(__FILE__) . '/../include/test.inc.php';
+
+class SPLIteratorTest extends PlTestCase
+{
+    public function testSimple()
+    {
+        $values = array(1, 2, 3, 4);
+        $it = PlIteratorUtils::fromArray($values, 1, true);
+        $it = PlIteratorUtils::foreachIterator($it);
+
+        $get = array();
+        foreach ($it as $key => $value) {
+            $this->assertSame($values[$key], $value);
+            $get[] = $value;
+        }
+        $this->assertSame($values, $get);
+    }
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>