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 = null) {
43 $that->hydrate($result);
49 * @param \pq\Result $result
52 protected function hydrate(\pq\Result
$result = null) {
54 $this->rows
= array();
57 $row = $this->getRowPrototype();
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 * Get the row prototype
89 function getRowPrototype() {
94 * @return \pq\Gateway\Table
101 * Create all rows of this rowset
103 * @return \pq\Gateway\Rowset
106 function create($txn = true) {
107 $txn = $txn ?
$this->table
->getConnection()->startTransaction() : false;
109 foreach ($this->rows
as $row) {
112 } catch (\Exception
$e) {
125 * Update all rows of this rowset
127 * @return \pq\Gateway\Rowset
130 function update($txn = true) {
131 $txn = $txn ?
$this->table
->getConnection()->startTransaction() : false;
133 foreach ($this->rows
as $row) {
136 } catch (\Exception
$e) {
149 * Delete all rows of this rowset
151 * @return \pq\Gateway\Rowset
154 function delete($txn = true) {
155 $txn = $txn ?
$this->table
->getConnection()->startTransaction() : false;
157 foreach ($this->rows
as $row) {
160 } catch (\Exception
$e) {
173 * @implements JsonSerilaizable
175 function jsonSerialize() {
176 return array_map(function($row) {
177 return $row->jsonSerialize();
182 * @implements \Iterator
189 * @implements \Iterator
196 * @implements \Iterator
200 return $this->index
< count($this->rows
);
204 * @implements \Iterator
205 * @return \pq\Gateway\Row
208 if (!$this->valid()) {
209 throw new \
OutOfBoundsException("Invalid row index {$this->index}");
211 return $this->rows
[$this->index
];
215 * @implements \Iterator
223 * @implements SeekableIterator
226 function seek($pos) {
227 /* only index for now */
230 if (!$this->valid()) {
231 throw new \
OutOfBoundsException("Invalid seek position ($pos)");
238 * @implements \Countable
242 return count($this->rows
);
246 * Get the rows of this rowset
254 * Apply a callback on each row of this rowset
255 * @param callable $cb
256 * @return \pq\Gateway\Rowset
258 function apply(callable
$cb) {
259 array_walk($this->rows
, $cb, $this);
265 * @param callable $cb
266 * @return \pq\Gateway\Rowset
268 function filter(callable
$cb) {
269 $rowset = clone $this;
271 $rowset->rows
= array_filter($this->rows
, $cb);
276 * Append a row to the rowset
277 * @param \pq\Gateway\Row $row
279 function append(Row
$row) {
280 $this->rows
[] = $row;