X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fpliteratorutils.php;h=875aac70cd4707747e4d1a3c5f71d61d69db66fc;hb=4c603b95edc398a28dd8ea01d756ca14f01f95e5;hp=c9bf046b9364c24f78b3f66f9c15f371f970d0b3;hpb=a797e4f7daf5fbcc9b12f64c298e9aa4df0f0acc;p=platal.git diff --git a/classes/pliteratorutils.php b/classes/pliteratorutils.php index c9bf046..875aac7 100644 --- a/classes/pliteratorutils.php +++ b/classes/pliteratorutils.php @@ -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: ?>