5c0efa4ac19a30da22c3bb7627d8ffb4bd58ada5
[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 = $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 /**
83 * Fill modified cells
84 * @return \pq\Gateway\Row
85 */
86 protected function prime() {
87 $this->cell = array();
88 foreach ($this->data as $key => $val) {
89 $this->cell[$key] = new Cell($this, $key, $val, true);
90 }
91 return $this;
92 }
93
94 /**
95 * Transform data array to where criteria
96 * @return array
97 */
98 protected function criteria() {
99 $where = array();
100 foreach($this->data as $k => $v) {
101 $where["$k="] = $v;
102 }
103 return $where;
104 }
105
106 /**
107 * Get an array of changed properties
108 * @return array
109 */
110 protected function changes() {
111 $changes = array();
112 foreach ($this->cell as $name => $cell) {
113 if ($cell->isDirty()) {
114 $changes[$name] = $cell->get();
115 }
116 }
117 return $changes;
118 }
119
120 /**
121 * Get a cell
122 * @param string $p
123 * @return \pq\Gateway\Cell
124 */
125 function __get($p) {
126 if (!isset($this->cell[$p])) {
127 $this->cell[$p] = new Cell($this, $p, $this->data[$p]);
128 }
129 return $this->cell[$p];
130 }
131
132 /**
133 * Set a cell value
134 * @param string $p
135 * @param mixed $v
136 */
137 function __set($p, $v) {
138 $this->__get($p)->set(($v instanceof Cell) ? $v->get() : $v);
139 }
140
141 /**
142 * Create this row in the database
143 * @return \pq\Gateway\Row
144 */
145 function create() {
146 $this->data = $this->table->create($this->changes())->current()->data;
147 $this->cell = array();
148 return $this;
149 }
150
151 /**
152 * Update this row in the database
153 * @return \pq\Gateway\Row
154 */
155 function update() {
156 $this->data = $this->table->update($this->criteria(), $this->changes())->current()->data;
157 $this->cell = array();
158 return $this;
159 }
160
161 /**
162 * Delete this row in the database
163 * @return \pq\Gateway\Row
164 */
165 function delete() {
166 $this->data = $this->table->delete($this->criteria(), "*")->current()->data;
167 return $this->prime();
168 }
169 }