vzlogger/backend/lib/Interpreter/Iterator/DataIterator.php

99 lines
2.1 KiB
PHP
Raw Normal View History

<?php
/**
* @package default
* @copyright Copyright (c) 2010, The volkszaehler.org project
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
2010-09-22 20:52:59 +02:00
namespace Volkszaehler\Interpreter\Iterator;
2010-09-28 15:46:30 +02:00
use Volkszaehler\Util;
2010-07-29 00:04:33 +02:00
use Doctrine\DBAL;
/**
* @author Steffen Vogel <info@steffenvogel.de>
2010-07-29 00:04:33 +02:00
* @package default
*/
class DataIterator implements \Iterator, \Countable {
protected $current;
2010-09-28 15:46:30 +02:00
protected $key; // key
2010-07-29 00:04:33 +02:00
protected $stmt; // PDOStatement
2010-09-22 02:32:03 +02:00
protected $size; // total readings in PDOStatement
2010-07-29 00:04:33 +02:00
/**
* Constructor
*
* @param \PDOStatement $stmt
2010-09-28 15:46:30 +02:00
* @param integer $size
2010-07-29 00:04:33 +02:00
*/
public function __construct(\PDOStatement $stmt, $size) {
$this->size = $size;
2010-07-29 00:04:33 +02:00
$this->stmt = $stmt;
$this->stmt->setFetchMode(\PDO::FETCH_NUM);
}
2010-07-29 00:04:33 +02:00
/**
* @return array with data
*/
public function current() {
return $this->current;
}
2010-07-29 00:04:33 +02:00
/**
* Fetch next row from database
*/
public function next() {
$this->key++;
2010-07-29 00:04:33 +02:00
$this->current = $this->stmt->fetch();
}
2010-07-29 00:04:33 +02:00
/**
* @return integer the nth data row
*/
public function key() {
return $this->key;
}
2010-07-29 00:04:33 +02:00
/**
* @return boolean do we have another row in the resultset?
*/
public function valid() {
2010-09-28 15:46:30 +02:00
return is_array($this->current);
}
/**
2010-07-29 00:04:33 +02:00
* Rewind the iterator
*
* Should only be called once
* PDOStatements doest support rewind()
*/
public function rewind() {
$this->key = 0;
2010-07-29 00:04:33 +02:00
$this->current = $this->stmt->fetch();
}
2010-07-29 00:04:33 +02:00
/**
* @return integer
*/
public function count() { return $this->size; }
}
?>