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