3c69a1294d015111db7629aee7234cb5e550dc45
[m6w6/pq-gateway] / lib / pq / Gateway / Cell.php
1 <?php
2
3 namespace pq\Gateway;
4
5 use \pq\Query\Expressible;
6
7 class Cell extends Expressible implements \ArrayAccess
8 {
9 /**
10 * @var \pq\Gateway\Row
11 */
12 protected $row;
13
14 /**
15 * @var string
16 */
17 protected $name;
18
19 /**
20 * @var bool
21 */
22 protected $dirty;
23
24 /**
25 * @param \pq\Gateway\Row $row
26 * @param string $name
27 * @param mixed $data
28 * @param bool $dirty
29 */
30 function __construct(Row $row, $name, $data, $dirty = false) {
31 parent::__construct($data);
32 $this->row = $row;
33 $this->name = $name;
34 $this->dirty = $dirty;
35 }
36
37 /**
38 * Check whether the cell has been modified
39 * @return bool
40 */
41 function isDirty() {
42 return (bool) $this->dirty;
43 }
44
45 /**
46 * Set the value
47 * @param mixed $data
48 * @return \pq\Gateway\Cell
49 */
50 function set($data) {
51 if ($data instanceof Row) {
52 $this->row->__set($data->getTable()->getName() . "_id", $data->id);
53 $this->row->__unset($this->name);
54 return $this;
55 }
56 if ($data instanceof Cell) {
57 $data = $data->get();
58 }
59 parent::set($data);
60 $this->dirty = true;
61 return $this;
62 }
63
64 /**
65 * Modify the value in this cell
66 * @param mixed $data
67 * @param string $op a specific operator
68 * @return \pq\Gateway\Cell
69 */
70 function mod($data, $op = null) {
71 parent::mod($data, $op);
72 $this->dirty = true;
73 return $this;
74 }
75
76 function offsetGet($o) {
77 if (isset($this->data) && !is_array($this->data)) {
78 throw new \UnexpectedValueException("Cell data is not an array");
79 }
80 return $this->data[$o];
81 }
82
83 function offsetSet($o, $v) {
84 if (isset($this->data) && !is_array($this->data)) {
85 throw new \UnexpectedValueException("Cell data is not an array");
86 }
87 if (isset($o)) {
88 $this->data[$o] = $v;
89 } else {
90 $this->data[] = $v;
91 }
92 $this->dirty = true;
93 }
94
95 function offsetExists($o) {
96 if (isset($this->data) && !is_array($this->data)) {
97 throw new \UnexpectedValueException("Cell data is not an array");
98 }
99 return isset($this->data[$o]);
100 }
101
102 function offsetUnset($o) {
103 if (isset($this->data) && !is_array($this->data)) {
104 throw new \UnexpectedValueException("Cell data is not an array");
105 }
106 unset($this->data[$o]);
107 $this->dirty = true;
108 }
109 }