add support for one-dimensional arrays; type input parameters
[m6w6/pq-gateway] / lib / pq / Gateway / Cell.php
index f29fd1f5499cdf4f0ed2042378683d2d3eed880a..3c69a1294d015111db7629aee7234cb5e550dc45 100644 (file)
@@ -2,9 +2,9 @@
 
 namespace pq\Gateway;
 
-use \pq\Query\Expr;
+use \pq\Query\Expressible;
 
-class Cell
+class Cell extends Expressible implements \ArrayAccess
 {
        /**
         * @var \pq\Gateway\Row
@@ -16,11 +16,6 @@ class Cell
         */
        protected $name;
        
-       /**
-        * @var mixed
-        */
-       protected $data;
-       
        /**
         * @var bool
         */
@@ -33,42 +28,37 @@ class Cell
         * @param bool $dirty
         */
        function __construct(Row $row, $name, $data, $dirty = false) {
+               parent::__construct($data);
                $this->row = $row;
                $this->name = $name;
-               $this->data = $data;
                $this->dirty = $dirty;
        }
        
-       /**
-        * Get value as string
-        * @return string
-        */
-       function __toString() {
-               return (string) $this->data;
-       }
-       
-       /**
-        * Test whether the value is an unevaluated expression
-        * @return bool
-        */
-       function isExpr() {
-               return $this->data instanceof Expr;
-       }
-       
        /**
         * Check whether the cell has been modified
         * @return bool
         */
        function isDirty() {
-               return $this->dirty;
+               return (bool) $this->dirty;
        }
        
        /**
-        * Get value
-        * @return mixed
+        * Set the value
+        * @param mixed $data
+        * @return \pq\Gateway\Cell
         */
-       function get() {
-               return $this->data;
+       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;
        }
        
        /**
@@ -78,32 +68,42 @@ class Cell
         * @return \pq\Gateway\Cell
         */
        function mod($data, $op = null) {
-               if (!($this->data instanceof Expr)) {
-                       $this->data = new Expr($this->name);
+               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 ($data instanceof Expr) {
-                       $this->data->add($data);
-               } elseif (!isset($op) && is_numeric($data)) {
-                       $this->data->add(new Expr("+ $data"));
+               if (isset($o)) {
+                       $this->data[$o] = $v;
                } else {
-                       $data = $this->row->getTable()->getConnection()->quote($data);
-                       $this->data->add(new Expr("%s %s"), isset($op) ? $op : "||", $data);
+                       $this->data[] = $v;
                }
-               
                $this->dirty = true;
-               
-               return $this;
        }
-       
-       /**
-        * Set the value in this cell
-        * @param mixed $data
-        * @return \pq\Gateway\Cell
-        */
-       function set($data) {
-               $this->data = $data;
+
+       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;
-               return $this;
        }
 }