. */ 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 $rowCount total num of rows in $stmt * @param integer $tupleCount set to NULL to get all rows */ public function __construct(\PDOStatement $stmt, $rowCount, $tupleCount) { $this->rowCount = $rowCount; $this->tupleCount = $tupleCount; $this->stmt = $stmt; $this->stmt->setFetchMode(\PDO::FETCH_NUM); if (empty($this->tupleCount) || $this->rowCount < $this->tupleCount) { // get all rows $this->packageSize = 1; $this->tupleCount = $this->rowCount; } else { // sumarize $this->packageSize = floor($this->rowCount / $this->tupleCount); $this->tupleCount = floor($this->rowCount / $this->packageSize); if (fmod($this->rowCount, $this->packageSize) > 0) { $this->tupleCount++; } } } /** * Aggregate data * @return next aggregated tuple */ public function next() { if ( $this->packageSize == 1) { // return each row as single tuple $package = $this->stmt->fetch(); } else { // summarize rows $package = array(0, 0, 0); for ($i = 0; $i < $this->packageSize && $tuple = $this->stmt->fetch(); $i++) { $package[0] = $tuple[0]; $package[1] += $tuple[1]; $package[2] += $tuple[2]; $this->rowKey++; } } $this->key++; return $this->current = $package; } /** * Rewind the iterator * * @internal Should only be called once: PDOStatement hasn't a rewind() */ public function rewind() { $this->key = $this->rowKey = 0; return $this->next(); // fetch first tuple } public function valid() { return ($this->current[2] > 0); // current package contains at least 1 tuple } /** * 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; } } ?>