X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FRowset.php;h=2527b029a1f629cad222161c05e73cf84540a216;hp=4cf062f6f149a1c1ffbd37d956bc01c3f5dc2d0e;hb=01bd45d05ce58796db7540d60671b8cff5d46bff;hpb=caa2499169b61c7dc6254886e73dcfc000737ed6 diff --git a/lib/pq/Gateway/Rowset.php b/lib/pq/Gateway/Rowset.php index 4cf062f..2527b02 100644 --- a/lib/pq/Gateway/Rowset.php +++ b/lib/pq/Gateway/Rowset.php @@ -2,35 +2,170 @@ namespace pq\Gateway; -class Rowset implements \IteratorAggregate +class Rowset implements \SeekableIterator, \Countable, \JsonSerializable { /** * @var \pq\Gateway\Table */ protected $table; + /** + * @var int + */ + protected $index = 0; + /** * @var array */ protected $rows; + /** + * @var string + */ + protected $row; + /** * @param \pq\Gateway\Table $table * @param \pq\Result $result */ - function __construct(Table $table, \pq\Result $result, $rowClass = "\\pq\\Gateway\\Row") { + function __construct(Table $table, \pq\Result $result, $row = "\\pq\\Gateway\\Row") { $this->table = $table; - while (($row = $result->fetchRow(\pq\Result::FETCH_ASSOC))) { - $this->rows[] = new $rowClass($this->table, $row); + $this->row = $row; + + $this->hydrate($result); + } + + /** + * Copy constructor + * @param \pq\Result $result + * @return \pq\Gateway\Rowset + */ + function __invoke(\pq\Result $result) { + $that = clone $this; + $that->hydrate($result); + return $that; + } + + /** + * + * @param \pq\Result $result + * @return array + */ + protected function hydrate(\pq\Result $result) { + $this->index = 0; + $this->rows = array(); + $row = $this->row; + + if (is_callable($row)) { + while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) { + $this->rows[] = $row($data); + } + } else { + while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) { + $this->rows[] = new $row($this->table, $data); + } + } + return $this; + } + + /** + * @return \pq\Gateway\Table + */ + function getTable() { + return $this->table; + } + + function create() { + array_map(function ($row) { + $row->create(); + }, $this->rows); + return $this; + } + + function update() { + array_map(function ($row) { + $row->update(); + }, $this->rows); + return $this; + } + + function delete() { + array_map(function ($row) { + $row->delete(); + }, $this->rows); + return $this; + } + + /** + * @implements JsonSerilaizable + */ + function jsonSerialize() { + return array_map(function($row) { + return $row->jsonSerialize(); + }, $this->rows); + } + + /** + * @implements \Iterator + */ + function rewind() { + $this->index = 0; + } + /** + * @implements \Iterator + */ + function next() { + ++$this->index; + } + /** + * @implements \Iterator + * @return bool + */ + function valid() { + return $this->index < count($this->rows); + } + /** + * @implements \Iterator + * @return \pq\Gateway\Row + */ + function current() { + return $this->rows[$this->index]; + } + /** + * @implements \Iterator + * @return int + */ + function key() { + return $this->index; + } + + /** + * @implements SeekableIterator + * @param mixed $pos + */ + function seek($pos) { + /* only index for now */ + $this->index = $pos; + + if (!$this->valid()) { + throw new \OutOfBoundsException("Invalid seek position ($pos)"); } } /** - * @implements \IteratorAggregate - * @return \pq\Gateway\ArrayIterator + * @implements \Countable + * @return int + */ + function count() { + return count($this->rows); + } + + /** + * Get the rows of this rowset + * @return array */ - function getIterator() { - return new \ArrayIterator($this->rows); + function getRows() { + return $this->rows; } /**