add support for one-dimensional arrays; type input parameters
[m6w6/pq-gateway] / lib / pq / Gateway / Cell.php
index 1955552c3858508672746b4f252e2c33fd3c3a7f..3c69a1294d015111db7629aee7234cb5e550dc45 100644 (file)
@@ -2,6 +2,108 @@
 
 namespace pq\Gateway;
 
-class Cell
+use \pq\Query\Expressible;
+
+class Cell extends Expressible implements \ArrayAccess
 {
+       /**
+        * @var \pq\Gateway\Row
+        */
+       protected $row;
+       
+       /**
+        * @var string
+        */
+       protected $name;
+       
+       /**
+        * @var bool
+        */
+       protected $dirty;
+       
+       /**
+        * @param \pq\Gateway\Row $row
+        * @param string $name
+        * @param mixed $data
+        * @param bool $dirty
+        */
+       function __construct(Row $row, $name, $data, $dirty = false) {
+               parent::__construct($data);
+               $this->row = $row;
+               $this->name = $name;
+               $this->dirty = $dirty;
+       }
+       
+       /**
+        * Check whether the cell has been modified
+        * @return bool
+        */
+       function isDirty() {
+               return (bool) $this->dirty;
+       }
+       
+       /**
+        * Set the value
+        * @param mixed $data
+        * @return \pq\Gateway\Cell
+        */
+       function set($data) {
+               if ($data instanceof Row) {
+                       $this->row->__set($data->getTable()->getName() . "_id", $data->id);
+                       $this->row->__unset($this->name);
+                       return $this;
+               }
+               if ($data instanceof Cell) {
+                       $data = $data->get();
+               }
+               parent::set($data);
+               $this->dirty = true;
+               return $this;
+       }
+       
+       /**
+        * Modify the value in this cell
+        * @param mixed $data
+        * @param string $op a specific operator
+        * @return \pq\Gateway\Cell
+        */
+       function mod($data, $op = null) {
+               parent::mod($data, $op);
+               $this->dirty = true;
+               return $this;
+       }
+       
+       function offsetGet($o) {
+               if (isset($this->data) && !is_array($this->data)) {
+                       throw new \UnexpectedValueException("Cell data is not an array");
+               }
+               return $this->data[$o];
+       }
+
+       function offsetSet($o, $v) {
+               if (isset($this->data) && !is_array($this->data)) {
+                       throw new \UnexpectedValueException("Cell data is not an array");
+               }
+               if (isset($o)) {
+                       $this->data[$o] = $v;
+               } else {
+                       $this->data[] = $v;
+               }
+               $this->dirty = true;
+       }
+
+       function offsetExists($o) {
+               if (isset($this->data) && !is_array($this->data)) {
+                       throw new \UnexpectedValueException("Cell data is not an array");
+               }
+               return isset($this->data[$o]);
+       }
+
+       function offsetUnset($o) {
+               if (isset($this->data) && !is_array($this->data)) {
+                       throw new \UnexpectedValueException("Cell data is not an array");
+               }
+               unset($this->data[$o]);
+               $this->dirty = true;
+       }
 }