Files
bsrdb/src/Db/OdbcResultSet.php
2023-11-24 16:04:47 +01:00

102 lines
2.1 KiB
PHP

<?php
namespace Bsr\Db;
class OdbcResultSet implements \Iterator, \ArrayAccess, \Countable
{
public $length;
private $results;
private $error = false;
private $num_fields;
private $num_rows;
private $cursor_index;
public function __construct($resultset)
{
$this->results = $resultset;
$this->num_fields = is_array(current($resultset)) ? count(current($resultset)) : count($resultset);
$this->num_rows = count($this->results);
$this->length = $this->num_rows;
$this->cursor_index = 0;
}
public function get_num_rows()
{
return $this->num_rows;
}
public function is_error()
{
return ($this->error ? true : false);
}
public function get_error()
{
return $this->error;
}
public function get_row()
{
return $this->current();
}
public function to_array()
{
return $this->results;
}
// Implementing Countable
public function count(): int {
return $this->get_num_rows();
}
// Implementing ArrayAccess
public function offsetExists(mixed $offset): bool
{
return !$this->error && $offset < $this->length && $offset >= 0;
}
public function offsetGet(mixed $offset): mixed
{
return $this->offsetExists($offset) ? $this->results[$offset] : false;
}
public function offsetSet(mixed $offset, mixed $value): void
{
if($this->offsetExists($offset)) {
$this->results[$offset] = $value;
}
}
public function offsetUnset(mixed $offset): void
{
throw new \RuntimeException("Unsetting elements is not supported.");
}
// Implementing Iterator
public function current(): mixed
{
return $this->offsetGet($this->cursor_index);
}
public function key(): mixed
{
return $this->cursor_index;
}
public function next(): void
{
++$this->cursor_index;
}
public function rewind(): void
{
$this->cursor_index = 0;
}
public function valid(): bool
{
return $this->offsetExists($this->cursor_index);
}
}