e408430c736293fa3750ce810f31600500db1169
[m6w6/pq-gateway] / lib / pq / Gateway / Row.php
1 <?php
2
3 namespace pq\Gateway;
4
5 class Row implements \JsonSerializable
6 {
7 /**
8 * @var \pq\Gateway\Table
9 */
10 protected $table;
11
12 /**
13 * @var array
14 */
15 protected $data;
16
17 /**
18 * @var array
19 */
20 protected $cell = array();
21
22 /**
23 * @param \pq\Gateway\Table $table
24 * @param array $data
25 * @param bool $prime whether to mark all columns as modified
26 */
27 function __construct(Table $table, array $data = null, $prime = false) {
28 $this->table = $table;
29 $this->data = (array) $data;
30
31 if ($prime) {
32 $this->prime();
33 }
34 }
35
36 /**
37 * Copy constructor
38 * @param array $data
39 * @return \pq\Gateway\Row
40 */
41 function __invoke(array $data) {
42 $that = clone $this;
43 $that->data = $data;
44 return $that->prime();
45 }
46
47 /**
48 * @implements JsonSerializable
49 * @return array
50 */
51 function jsonSerialize() {
52 return $this->data;
53 }
54
55 /**
56 * @return \pq\Gateway\Table
57 */
58 function getTable() {
59 return $this->table;
60 }
61
62 /**
63 * @return array
64 */
65 function getData() {
66 return $this->data;
67 }
68
69 /**
70 * Check whether the row contains modifications
71 * @return boolean
72 */
73 function isDirty() {
74 foreach ($this->cell as $cell) {
75 if ($cell->isDirty()) {
76 return true;
77 }
78 }
79 return false;
80 }
81
82 function refresh() {
83 $this->data = $this->table->find($this->criteria(), null, 1, 0)->current()->data;
84 $this->cell = array();
85 return $this;
86 }
87
88 /**
89 * Fill modified cells
90 * @return \pq\Gateway\Row
91 */
92 protected function prime() {
93 $this->cell = array();
94 foreach ($this->data as $key => $val) {
95 $this->cell[$key] = new Cell($this, $key, $val, true);
96 }
97 return $this;
98 }
99
100 /**
101 * Transform data array to where criteria
102 * @return array
103 */
104 protected function criteria() {
105 $where = array();
106 foreach($this->data as $k => $v) {
107 $where["$k="] = $v;
108 }
109 return $where;
110 }
111
112 /**
113 * Get an array of changed properties
114 * @return array
115 */
116 protected function changes() {
117 $changes = array();
118 foreach ($this->cell as $name => $cell) {
119 if ($cell->isDirty()) {
120 $changes[$name] = $cell->get();
121 }
122 }
123 return $changes;
124 }
125
126 /**
127 * Get a cell
128 * @param string $p
129 * @return \pq\Gateway\Cell
130 */
131 function __get($p) {
132 if (!isset($this->cell[$p])) {
133 $this->cell[$p] = new Cell($this, $p, isset($this->data[$p]) ? $this->data[$p] : null);
134 }
135 return $this->cell[$p];
136 }
137
138 /**
139 * Set a cell value
140 * @param string $p
141 * @param mixed $v
142 */
143 function __set($p, $v) {
144 $this->__get($p)->set(($v instanceof Cell) ? $v->get() : $v);
145 }
146
147 /**
148 * Create this row in the database
149 * @return \pq\Gateway\Row
150 */
151 function create() {
152 $this->data = $this->table->create($this->changes())->current()->data;
153 $this->cell = array();
154 return $this;
155 }
156
157 /**
158 * Update this row in the database
159 * @return \pq\Gateway\Row
160 */
161 function update() {
162 $this->data = $this->table->update($this->criteria(), $this->changes())->current()->data;
163 $this->cell = array();
164 return $this;
165 }
166
167 /**
168 * Delete this row in the database
169 * @return \pq\Gateway\Row
170 */
171 function delete() {
172 $this->data = $this->table->delete($this->criteria(), "*")->current()->data;
173 return $this->prime();
174 }
175 }