X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fpq%2FGateway%2FRow.php;h=dc07e3f208e200cd0c1b87290619587d25d41882;hb=0e684b0140154ebc8a304670c0349da840a16ad2;hp=981a58d9afad2db64c150420b32c23138478d1b6;hpb=caa2499169b61c7dc6254886e73dcfc000737ed6;p=m6w6%2Fpq-gateway diff --git a/lib/pq/Gateway/Row.php b/lib/pq/Gateway/Row.php index 981a58d..dc07e3f 100644 --- a/lib/pq/Gateway/Row.php +++ b/lib/pq/Gateway/Row.php @@ -2,7 +2,7 @@ namespace pq\Gateway; -class Row +class Row implements \JsonSerializable { /** * @var \pq\Gateway\Table @@ -22,34 +22,130 @@ class Row /** * @param \pq\Gateway\Table $table * @param array $data + * @param bool $prime whether to mark all columns as modified */ - function __construct(Table $table, array $data = null) { + function __construct(Table $table, array $data = null, $prime = false) { $this->table = $table; $this->data = $data; + + if ($prime) { + $this->prime(); + } + } + + /** + * Copy constructor + * @param array $data + * @return \pq\Gateway\Row + */ + function __invoke(array $data) { + $that = clone $this; + $that->data = $data; + return $that->prime(); + } + + /** + * @implements JsonSerializable + * @return array + */ + function jsonSerialize() { + return $this->data; + } + + /** + * @return \pq\Gateway\Table + */ + function getTable() { + return $this->table; + } + + /** + * @return array + */ + function getData() { + return $this->data; } + /** + * Fill modified cells + * @return \pq\Gateway\Row + */ + protected function prime() { + $this->mods = array(); + foreach ($this->data as $key => $val) { + $this->mods[$key] = new Cell($this, $key, $val); + } + return $this; + } + + /** + * Transform data array to where criteria + * @param array $data + * @return array + */ + protected function criteria() { + $where = array(); + array_walk($this->data, function($v, $k) use (&$where) { + $where["$k="] = $v; + }); + return $where; + } + + protected function changes() { + $changes = array(); + foreach ($this->mods as $name => $cell) { + $changes[$name] = $cell->get(); + } + return $changes; + } + + /** + * Get a cell + * @param string $p + * @return \pq\Gateway\Cell + */ function __get($p) { - if (!isset($this->mod[$p])) { - $this->mod[$p] = new Cell($this, $p); + if (!isset($this->mods[$p])) { + $this->mods[$p] = new Cell($this, $p, $this->data[$p]); } - return $this->mod[$p]; + return $this->mods[$p]; } + /** + * Set a cell value + * @param string $p + * @param mixed $v + */ + function __set($p, $v) { + $this->__get($p)->set(($v instanceof Cell) ? $v->get() : $v); + } + + /** + * Create this row in the database + * @return \pq\Gateway\Row + */ function create() { - $this->data = $this->table->create($this->mods)->getIterator()->current()->data; + $this->data = $this->table->create($this->changes())->current()->data; $this->mods = array(); return $this; } + /** + * Update this row in the database + * @return \pq\Gateway\Row + */ function update() { - $this->data = $this->table->update($this->data, $this->mods)->getIterator()->current()->data; + $this->data = $this->table->update($this->criteria(), $this->changes())->current()->data; $this->mods = array(); return $this; } + /** + * Delete this row in the database + * @return \pq\Gateway\Row + */ function delete() { - $this->data = $this->table->delete($this->data, "*")->getIterator()->current()->data; - $this->mods = array(); - return $this; + $this->data = $this->table->delete($this->criteria(), "*")->current()->data; + return $this->prime(); } }