2009-08-14 00:05:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2010-09-14 18:40:41 +02:00
|
|
|
* This file is part of the "dibi" - smart database abstraction layer.
|
2012-01-02 20:24:16 +01:00
|
|
|
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
2009-08-14 00:05:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External result set iterator.
|
|
|
|
*
|
|
|
|
* This can be returned by DibiResult::getIterator() method or using foreach
|
|
|
|
* <code>
|
|
|
|
* $result = dibi::query('SELECT * FROM table');
|
|
|
|
* foreach ($result as $row) {
|
|
|
|
* print_r($row);
|
|
|
|
* }
|
|
|
|
* unset($result);
|
|
|
|
* </code>
|
|
|
|
*
|
2010-09-14 18:40:41 +02:00
|
|
|
* @author David Grudl
|
2012-01-03 04:50:11 +01:00
|
|
|
* @package dibi
|
2009-08-14 00:05:20 +02:00
|
|
|
*/
|
|
|
|
class DibiResultIterator implements Iterator, Countable
|
|
|
|
{
|
|
|
|
/** @var DibiResult */
|
|
|
|
private $result;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $row;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $pointer;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DibiResult
|
|
|
|
*/
|
2010-08-03 16:59:31 +02:00
|
|
|
public function __construct(DibiResult $result)
|
2009-08-14 00:05:20 +02:00
|
|
|
{
|
|
|
|
$this->result = $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rewinds the iterator to the first element.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function rewind()
|
|
|
|
{
|
|
|
|
$this->pointer = 0;
|
2010-08-03 16:59:31 +02:00
|
|
|
$this->result->seek(0);
|
2009-08-14 00:05:20 +02:00
|
|
|
$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->row = $this->result->fetch();
|
|
|
|
$this->pointer++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if there is a current element after calls to rewind() or next().
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function valid()
|
|
|
|
{
|
2010-08-03 16:59:31 +02:00
|
|
|
return !empty($this->row);
|
2009-08-14 00:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Required by the Countable interface.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return $this->result->getRowCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|