phplibrary/src/Database/StatementIterator.php

48 lines
928 B
PHP

<?php
namespace ctiso\Database;
use PDO;
class StatementIterator implements \Iterator
{
private $result;
private $pos = 0;
private $row_count;
public function __construct($rs/*: PDOStatement*/) {
$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) {
$this->pos = $index;
}
function count() {
return $this->row_count;
}
}