From ca879941898a8a7ec327ce5e61f4ebb973dabace Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 7 Jan 2009 13:48:01 +0000 Subject: [PATCH] - DibiResultIterator is back (from rev. 123) --- dibi/dibi.php | 1 + dibi/libs/DibiResult.php | 4 +- dibi/libs/DibiResultIterator.php | 136 +++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 dibi/libs/DibiResultIterator.php diff --git a/dibi/dibi.php b/dibi/dibi.php index ed78c64e..6cb591e9 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -66,6 +66,7 @@ require_once dirname(__FILE__) . '/libs/DibiObject.php'; require_once dirname(__FILE__) . '/libs/DibiException.php'; require_once dirname(__FILE__) . '/libs/DibiConnection.php'; require_once dirname(__FILE__) . '/libs/DibiResult.php'; +require_once dirname(__FILE__) . '/libs/DibiResultIterator.php'; require_once dirname(__FILE__) . '/libs/DibiTranslator.php'; require_once dirname(__FILE__) . '/libs/DibiVariable.php'; require_once dirname(__FILE__) . '/libs/DibiTableX.php'; diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index d97734a1..c8f87946 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -587,11 +587,11 @@ class DibiResult extends DibiObject implements IDataSource * Required by the IteratorAggregate interface. * @param int offset * @param int limit - * @return ArrayIterator + * @return DibiResultIterator */ final public function getIterator($offset = NULL, $limit = NULL) { - return new ArrayIterator($this->fetchAll($offset, $limit)); + return new DibiResultIterator($this, $offset, $limit); } diff --git a/dibi/libs/DibiResultIterator.php b/dibi/libs/DibiResultIterator.php new file mode 100644 index 00000000..a5e0ee95 --- /dev/null +++ b/dibi/libs/DibiResultIterator.php @@ -0,0 +1,136 @@ + + * $result = dibi::query('SELECT * FROM table'); + * foreach ($result as $row) { + * print_r($row); + * } + * unset($result); + * + * + * Optionally you can specify offset and limit: + * + * foreach ($result->getIterator(2, 3) as $row) { + * print_r($row); + * } + * + * + * @author David Grudl + * @copyright Copyright (c) 2005, 2009 David Grudl + * @package dibi + */ +class DibiResultIterator implements Iterator +{ + /** @var DibiResult */ + private $result; + + /** @var int */ + private $offset; + + /** @var int */ + private $limit; + + /** @var int */ + private $row; + + /** @var int */ + private $pointer; + + + /** + * @param DibiResult + * @param int offset + * @param int limit + */ + public function __construct(DibiResult $result, $offset = NULL, $limit = NULL) + { + $this->result = $result; + $this->offset = (int) $offset; + $this->limit = $limit === NULL ? -1 : (int) $limit; + } + + + + /** + * Rewinds the iterator to the first element. + * @return void + */ + public function rewind() + { + $this->pointer = 0; + $this->result->seek($this->offset); + $this->row = $this->result->fetch(); + } + + + + /** + * Returns the key of the current element. + * @return mixed + */ + public function key() + { + return $this->pointer; + } + + + + /** + * Returns the current element. + * @return mixed + */ + public function current() + { + return $this->row; + } + + + + /** + * Moves forward to next element. + * @return void + */ + public function next() + { + //$this->result->seek($this->offset + $this->pointer + 1); + $this->row = $this->result->fetch(); + $this->pointer++; + } + + + + /** + * Checks if there is a current element after calls to rewind() or next(). + * @return bool + */ + public function valid() + { + return !empty($this->row) && ($this->limit < 0 || $this->pointer < $this->limit); + } + + +}