. */ namespace Volkszaehler\Interpreter; use Volkszaehler\Util; use Doctrine\DBAL; /** * @author Steffen Vogel * @package default */ class DataIterator implements \Iterator, \Countable { protected $stmt; // PDO statement protected $current; // the current data protected $key; // key private $rowKey; // internal key for PDO statement protected $rowCount; // num of readings in PDOStatement protected $tupleCount; // num of requested tuples protected $packageSize; // num of rows we aggregate in each tuple /** * Constructor * * @param \PDOStatement $stmt * @param integer $size * @param integer $tuples */ public function __construct(\PDOStatement $stmt, $rowCount, $tupleCount) { $this->rowCount = $rowCount; $this->tupleCount = $tupleCount; $this->stmt = $stmt; $this->stmt->setFetchMode(\PDO::FETCH_NUM); if ($this->rowCount > $this->tupleCount) { $this->packageSize = floor($this->rowCount / $this->tupleCount); $this->tupleCount = floor($this->rowCount / $this->packageSize) + 1; } else { $this->packageSize = 1; } } /** * Aggregate data */ public function next() { $package = array(0, 0, 0); for ($i = 0; $i < $this->packageSize && $this->valid(); $i++) { $tuple = $this->stmt->fetch(); $package[0] = $tuple[0]; $package[1] += $tuple[1]; $package[2] += $tuple[2]; $this->rowKey++; } $this->key++; return $this->current = $package; } /** * Rewind the iterator * * Should only be called once * PDOStatement hasn't a rewind() */ public function rewind() { $this->key = $this->rowKey = 0; } public function valid() { return $this->rowKey < $this->rowCount; } /** * Getter & setter */ public function getPackageSize() { return $this->packageSize; } public function count() { return $this->tupleCount; } public function key() { return $this->key; } public function current() { return $this->current; } } ?>