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