X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FRow.php;h=249337334d68e48deee130f672d02786e54e20a3;hb=0e2eb1f13ef60ce9a8709354136c42f7d87b2345;hp=a8b879370f347b4ad494edd78d99a25d7b079328;hpb=4879955d1b86d606dc24401f26ebde9be7612fbf;p=m6w6%2Fpq-gateway diff --git a/lib/pq/Gateway/Row.php b/lib/pq/Gateway/Row.php index a8b8793..2493373 100644 --- a/lib/pq/Gateway/Row.php +++ b/lib/pq/Gateway/Row.php @@ -26,7 +26,7 @@ class Row implements \JsonSerializable */ function __construct(Table $table, array $data = null, $prime = false) { $this->table = $table; - $this->data = $data; + $this->data = (array) $data; if ($prime) { $this->prime(); @@ -43,13 +43,40 @@ class Row implements \JsonSerializable $that->data = $data; return $that->prime(); } + + /** + * Export current state as an array + * @return array + * @throws \UnexpectedValueException if a cell has been modified by an expression + */ + function export() { + $export = array_merge($this->data, $this->cell); + foreach ($export as &$val) { + if ($val instanceof Cell) { + if ($val->isExpr()) { + throw new \UnexpectedValueException("Cannot export an SQL expression"); + } + $val = $val->get(); + } + } + return $export; + } + /** + * Export current state with security sensitive data removed. You should override that, just + * calls export() by default. + * @return array + */ + function exportPublic() { + return $this->export(); + } + /** * @implements JsonSerializable * @return array */ function jsonSerialize() { - return $this->data; + return $this->exportPublic(); } /** @@ -124,13 +151,16 @@ class Row implements \JsonSerializable } /** - * Get a cell + * Get a cell or parent rows * @param string $p - * @return \pq\Gateway\Cell + * @return \pq\Gateway\Cell|\pq\Gateway\Rowset */ function __get($p) { + if ($this->table->hasRelation($p)) { + return $this->table->by($this, $p); + } if (!isset($this->cell[$p])) { - $this->cell[$p] = new Cell($this, $p, $this->data[$p]); + $this->cell[$p] = new Cell($this, $p, isset($this->data[$p]) ? $this->data[$p] : null); } return $this->cell[$p]; } @@ -141,7 +171,38 @@ class Row implements \JsonSerializable * @param mixed $v */ function __set($p, $v) { - $this->__get($p)->set(($v instanceof Cell) ? $v->get() : $v); + $this->__get($p)->set($v); + } + + /** + * Unset a cell value + * @param string $p + */ + function __unset($p) { + unset($this->data[$p]); + unset($this->cell[$p]); + } + + /** + * Check if a cell isset + * @param string $p + * @return bool + */ + function __isset($p) { + return isset($this->data[$p]) || isset($this->cell[$p]); + } + + /** + * Get child rows of this row by foreign key + * @see \pq\Gateway\Table::of() + * @param string $foreign + * @param array $args [order, limit, offset] + * @return \pq\Gateway\Rowset + */ + function __call($foreign, array $args) { + array_unshift($args, $this); + $table = forward_static_call(array(get_class($this->getTable()), "resolve"), $foreign); + return call_user_func_array(array($table, "of"), $args); } /**