manage properties where they belong
[m6w6/pq-gateway] / lib / pq / Gateway / Cell.php
index efa5fcbb3cd1154ecd99321bdddaf9e093a7a5c2..ab2a46fceac95a3a97056fe78bdc4679c20ca2da 100644 (file)
@@ -4,18 +4,13 @@ namespace pq\Gateway;
 
 use \pq\Query\Expressible;
 
-class Cell extends Expressible
+class Cell extends Expressible implements \ArrayAccess
 {
        /**
         * @var \pq\Gateway\Row
         */
        protected $row;
        
-       /**
-        * @var string
-        */
-       protected $name;
-       
        /**
         * @var bool
         */
@@ -28,9 +23,8 @@ class Cell extends Expressible
         * @param bool $dirty
         */
        function __construct(Row $row, $name, $data, $dirty = false) {
-               parent::__construct($data);
+               parent::__construct($name, $data);
                $this->row = $row;
-               $this->name = $name;
                $this->dirty = $dirty;
        }
        
@@ -39,7 +33,7 @@ class Cell extends Expressible
         * @return bool
         */
        function isDirty() {
-               return $this->dirty;
+               return (bool) $this->dirty;
        }
        
        /**
@@ -48,6 +42,14 @@ class Cell extends Expressible
         * @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;
@@ -60,9 +62,45 @@ class Cell extends Expressible
         * @return \pq\Gateway\Cell
         */
        function mod($data, $op = null) {
+               if (is_string($data)) {
+                       $data = $this->row->getTable()->getConnection()->quote($data);
+               }
                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;
+       }
 }