phplibrary/src/Database/StatementIterator.php

54 lines
1 KiB
PHP

<?php
namespace ctiso\Database;
use PDO;
class StatementIterator implements \Iterator
{
/** @var PDOStatement */
private $result;
/** @var int */
private $pos = 0;
/** @var int */
private $row_count;
/**
* @param PDOStatement $rs
*/
public function __construct($rs) {
$this->result = $rs;
$this->row_count = $rs->getRecordCount();
}
function rewind(): void{
$this->pos = 0;
}
function valid(): bool {
return ($this->pos < $this->row_count);
}
function key(): mixed {
return $this->pos;
}
function current(): mixed{
if (!isset($this->result->cache[$this->pos])) {
$this->result->cache[$this->pos] = $this->result->fetch(PDO::FETCH_ASSOC);
}
return $this->result->cache[$this->pos];
}
function next(): void {
$this->pos++;
}
function seek($index): void {
$this->pos = $index;
}
function count(): int {
return $this->row_count;
}
}