flush
[m6w6/pq-gateway] / lib / pq / Gateway / Row.php
index 981a58d9afad2db64c150420b32c23138478d1b6..885c95abd5a166b51cb35e6281aa72280e6a67f7 100644 (file)
@@ -2,7 +2,7 @@
 
 namespace pq\Gateway;
 
-class Row
+class Row implements \JsonSerializable
 {
        /**
         * @var \pq\Gateway\Table
@@ -22,34 +22,122 @@ 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;
+       }
+       
+       /**
+        * 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->mods)->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->mods)->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();
        }
 }