storage test
[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 if ($this->data !== $data) {
54 parent::set($data);
55 $this->dirty = true;
56 }
57 return $this;
58 }
59
60 /**
61 * Modify the value in this cell
62 * @param mixed $data
63 * @param string $op a specific operator
64 * @return \pq\Gateway\Cell
65 */
66 function mod($data, $op = null) {
67 if (is_string($data)) {
68 $data = $this->row->getTable()->getConnection()->quote($data);
69 }
70 parent::mod($data, $op);
71 $this->dirty = true;
72 return $this;
73 }
74
75 function offsetGet($o) {
76 if (isset($this->data) && !is_array($this->data)) {
77 throw new \UnexpectedValueException("Cell data is not an array");
78 }
79 return $this->data[$o];
80 }
81
82 function offsetSet($o, $v) {
83 if (isset($this->data) && !is_array($this->data)) {
84 throw new \UnexpectedValueException("Cell data is not an array");
85 }
86 if (isset($o)) {
87 $this->data[$o] = $v;
88 } else {
89 $this->data[] = $v;
90 }
91 $this->dirty = true;
92 }
93
94 function offsetExists($o) {
95 if (isset($this->data) && !is_array($this->data)) {
96 throw new \UnexpectedValueException("Cell data is not an array");
97 }
98 return isset($this->data[$o]);
99 }
100
101 function offsetUnset($o) {
102 if (isset($this->data) && !is_array($this->data)) {
103 throw new \UnexpectedValueException("Cell data is not an array");
104 }
105 unset($this->data[$o]);
106 $this->dirty = true;
107 }
108 }