5 class Rowset
implements \SeekableIterator
, \Countable
, \JsonSerializable
8 * @var \pq\Gateway\Table
25 protected $row = "\\pq\\Gateway\\Row";
28 * @param \pq\Gateway\Table $table
29 * @param \pq\Result $result
31 function __construct(Table
$table, \pq\Result
$result = null) {
32 $this->table
= $table;
33 $this->hydrate($result);
38 * @param \pq\Result $result
39 * @return \pq\Gateway\Rowset
41 function __invoke(\pq\Result
$result) {
43 $that->hydrate($result);
49 * @param \pq\Result $result
52 protected function hydrate(\pq\Result
$result = null) {
54 $this->rows
= array();
59 if (is_callable($row)) {
60 while (($data = $result->fetchRow(\pq\Result
::FETCH_ASSOC
))) {
61 $this->rows
[] = $row($data);
64 while (($data = $result->fetchRow(\pq\Result
::FETCH_ASSOC
))) {
65 $this->rows
[] = new $row($this->table
, $data);
68 $this->rows
= $result->fetchAll(\pq\Result
::FETCH_OBJECT
);
76 * Set the row prototype
78 * @return \pq\Gateway\Table
80 function setRowPrototype($row) {
86 * @return \pq\Gateway\Table
93 array_map(function ($row) {
100 array_map(function ($row) {
107 array_map(function ($row) {
114 * @implements JsonSerilaizable
116 function jsonSerialize() {
117 return array_map(function($row) {
118 return $row->jsonSerialize();
123 * @implements \Iterator
129 * @implements \Iterator
135 * @implements \Iterator
139 return $this->index
< count($this->rows
);
142 * @implements \Iterator
143 * @return \pq\Gateway\Row
146 return $this->rows
[$this->index
];
149 * @implements \Iterator
157 * @implements SeekableIterator
160 function seek($pos) {
161 /* only index for now */
164 if (!$this->valid()) {
165 throw new \
OutOfBoundsException("Invalid seek position ($pos)");
170 * @implements \Countable
174 return count($this->rows
);
178 * Get the rows of this rowset
187 * @param callable $cb
188 * @return \pq\Gateway\Rowset
190 function filter(callable
$cb) {
191 $rowset = clone $this;
192 $rowset->rows
= array_filter($this->rows
, $cb);