|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\Serialize\BinderObject; |
|
6
|
|
|
use ByJG\Serialize\DumpToArrayInterface; |
|
7
|
|
|
use ByJG\Util\XmlUtil; |
|
8
|
|
|
use DOMNode; |
|
9
|
|
|
use UnexpectedValueException; |
|
10
|
|
|
|
|
11
|
|
|
class SingleRow extends BinderObject implements DumpToArrayInterface |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* \DOMNode represents a SingleRow |
|
16
|
|
|
* @var DOMNode |
|
17
|
|
|
*/ |
|
18
|
|
|
private $_node = null; |
|
19
|
|
|
private $_row = null; |
|
20
|
|
|
private $_originalRow = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* SingleRow constructor |
|
24
|
|
|
* @param array() |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($instance = null) |
|
27
|
|
|
{ |
|
28
|
|
|
if (is_null($instance)) { |
|
29
|
|
|
$this->_row = array(); |
|
30
|
|
|
} else if (is_array($instance)) { |
|
31
|
|
|
$this->_row = $instance; |
|
32
|
|
|
} else { |
|
33
|
|
|
$this->_row = array(); |
|
34
|
|
|
$this->bind($instance); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->acceptChanges(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Add a string field to row |
|
42
|
|
|
* @param string $name |
|
43
|
|
|
* @param string $value |
|
44
|
|
|
*/ |
|
45
|
|
|
public function addField($name, $value) |
|
46
|
|
|
{ |
|
47
|
|
|
if (!array_key_exists($name, $this->_row)) { |
|
48
|
|
|
$this->_row[$name] = $value; |
|
49
|
|
|
} elseif (is_array($this->_row[$name])) { |
|
50
|
|
|
$this->_row[$name][] = $value; |
|
51
|
|
|
} else { |
|
52
|
|
|
$this->_row[$name] = array($this->_row[$name], $value); |
|
53
|
|
|
} |
|
54
|
|
|
$this->informChanges(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $name - Field name |
|
59
|
|
|
* @return string |
|
60
|
|
|
* @desc et the string value from a field name |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getField($name) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!array_key_exists($name, $this->_row)) { |
|
65
|
|
|
return NULL; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$result = $this->_row[$name]; |
|
69
|
|
|
if (is_array($result)) { |
|
70
|
|
|
return array_shift($result); |
|
71
|
|
|
} else { |
|
72
|
|
|
return $result; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get array from a single field |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $name |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getFieldArray($name) |
|
83
|
|
|
{ |
|
84
|
|
|
if (!array_key_exists($name, $this->_row)) { |
|
85
|
|
|
return array(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$result = $this->_row[$name]; |
|
89
|
|
|
if (empty($result)) { |
|
90
|
|
|
return array(); |
|
91
|
|
|
} elseif (is_array($result)) { |
|
92
|
|
|
return $result; |
|
93
|
|
|
} else { |
|
94
|
|
|
return array($result); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return all Field Names from current SingleRow |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getFieldNames() |
|
103
|
|
|
{ |
|
104
|
|
|
return array_keys($this->_row); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Set a string value to existing field name |
|
109
|
|
|
* @param string $name |
|
110
|
|
|
* @param string $value |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setField($name, $value) |
|
113
|
|
|
{ |
|
114
|
|
|
if (!array_key_exists($name, $this->_row)) { |
|
115
|
|
|
$this->addField($name, $value); |
|
116
|
|
|
} else { |
|
117
|
|
|
$this->_row[$name] = $value; |
|
118
|
|
|
} |
|
119
|
|
|
$this->informChanges(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Remove specified field name from row. |
|
124
|
|
|
* @param string $name |
|
125
|
|
|
*/ |
|
126
|
|
|
public function removeFieldName($name) |
|
127
|
|
|
{ |
|
128
|
|
|
if (array_key_exists($name, $this->_row)) { |
|
129
|
|
|
unset($this->_row[$name]); |
|
130
|
|
|
$this->informChanges(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Remove specified field name with specified value name from row. |
|
136
|
|
|
* @param string $name |
|
137
|
|
|
* @param $value |
|
138
|
|
|
*/ |
|
139
|
|
|
public function removeFieldNameValue($name, $value) |
|
140
|
|
|
{ |
|
141
|
|
|
$result = $this->_row[$name]; |
|
142
|
|
|
if (!is_array($result)) { |
|
143
|
|
|
if ($value == $result) { |
|
144
|
|
|
unset($this->_row[$name]); |
|
145
|
|
|
$this->informChanges(); |
|
146
|
|
|
} |
|
147
|
|
|
} else { |
|
148
|
|
|
$qty = count($result); |
|
149
|
|
|
for ($i = 0; $i < $qty; $i++) { |
|
150
|
|
|
if ($result[$i] == $value) { |
|
151
|
|
|
unset($result[$i]); |
|
152
|
|
|
$this->informChanges(); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
$this->_row[$name] = array_values($result); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Update a specific field and specific value with new value |
|
161
|
|
|
* |
|
162
|
|
|
* @param String $name |
|
163
|
|
|
* @param String $oldvalue |
|
164
|
|
|
* @param String $newvalue |
|
165
|
|
|
*/ |
|
166
|
|
|
public function setFieldValue($name, $oldvalue, $newvalue) |
|
167
|
|
|
{ |
|
168
|
|
|
$result = $this->_row[$name]; |
|
169
|
|
|
if (!is_array($result)) { |
|
170
|
|
|
if ($oldvalue == $result) { |
|
171
|
|
|
$this->_row[$name] = $newvalue; |
|
172
|
|
|
$this->informChanges(); |
|
173
|
|
|
} |
|
174
|
|
|
} else { |
|
175
|
|
|
for ($i = sizeof($result) - 1; $i >= 0; $i--) { |
|
176
|
|
|
if ($result[$i] == $oldvalue) { |
|
177
|
|
|
$this->_row[$name][$i] = $newvalue; |
|
178
|
|
|
$this->informChanges(); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get the \DOMNode row objet |
|
186
|
|
|
* @return DOMNode |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getDomObject() |
|
189
|
|
|
{ |
|
190
|
|
|
if (is_null($this->_node)) { |
|
191
|
|
|
$this->_node = XmlUtil::createXmlDocumentFromStr("<row />"); |
|
192
|
|
|
$root = $this->_node->getElementsByTagName("row")->item(0); |
|
193
|
|
|
foreach ($this->_row as $key => $value) { |
|
194
|
|
|
if (!is_array($value)) { |
|
195
|
|
|
$field = XmlUtil::createChild($root, "field", $value); |
|
196
|
|
|
XmlUtil::addAttribute($field, "name", $key); |
|
197
|
|
|
} else { |
|
198
|
|
|
foreach ($value as $valueItem) { |
|
199
|
|
|
$field = XmlUtil::createChild($root, "field", $valueItem); |
|
200
|
|
|
XmlUtil::addAttribute($field, "name", $key); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
return $this->_node; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function toArray() |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->_row; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* |
|
215
|
|
|
* @return array |
|
216
|
|
|
*/ |
|
217
|
|
|
public function getAsJSON() |
|
218
|
|
|
{ |
|
219
|
|
|
if (is_array($this->_row)) { |
|
220
|
|
|
return json_decode(json_encode($this->_row)); |
|
221
|
|
|
} else { |
|
222
|
|
|
throw new UnexpectedValueException('I expected that getRawFormat is array() but ' . gettype($this->_row) . ' was given'); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @return array |
|
228
|
|
|
*/ |
|
229
|
|
|
public function getOriginalRawFormat() |
|
230
|
|
|
{ |
|
231
|
|
|
return $this->_originalRow; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* |
|
236
|
|
|
* @return bool |
|
237
|
|
|
*/ |
|
238
|
|
|
public function hasChanges() |
|
239
|
|
|
{ |
|
240
|
|
|
return ($this->_row != $this->_originalRow); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* |
|
245
|
|
|
* @return bool |
|
246
|
|
|
*/ |
|
247
|
|
|
public function acceptChanges() |
|
248
|
|
|
{ |
|
249
|
|
|
$this->_originalRow = $this->_row; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* |
|
254
|
|
|
* @return bool |
|
255
|
|
|
*/ |
|
256
|
|
|
public function rejectChanges() |
|
257
|
|
|
{ |
|
258
|
|
|
$this->_row = $this->_originalRow; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
protected function informChanges() |
|
262
|
|
|
{ |
|
263
|
|
|
$this->_node = null; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Override Specific implementation of setPropValue to SingleRow |
|
268
|
|
|
* |
|
269
|
|
|
* @param SingleRow $obj |
|
270
|
|
|
* @param string $propName |
|
271
|
|
|
* @param string $value |
|
272
|
|
|
*/ |
|
273
|
|
|
protected function setPropValue($obj, $propName, $value) |
|
274
|
|
|
{ |
|
275
|
|
|
$obj->setField($propName, $value); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
} |
|
280
|
|
|
|