$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
}
}
+// 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:
?>
--- /dev/null
+<?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:
+?>