1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Core; |
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
|
|
|
* |
24
|
|
|
* @param Row|array|\stdClass|object $instance |
25
|
|
|
* @throws \ByJG\Serializer\Exception\InvalidArgumentException |
26
|
|
|
*/ |
27
|
40 |
|
public function __construct($instance = []) |
28
|
|
|
{ |
29
|
40 |
|
if (is_array($instance)) { |
30
|
40 |
|
$this->row = $instance; |
31
|
|
|
} else { |
32
|
4 |
|
$this->row = array(); |
33
|
4 |
|
$this->bind($instance); |
34
|
|
|
} |
35
|
|
|
|
36
|
40 |
|
$this->acceptChanges(); |
37
|
40 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Add a string field to row |
41
|
|
|
* @param string $name |
42
|
|
|
* @param string $value |
43
|
|
|
*/ |
44
|
31 |
|
public function addField($name, $value) |
45
|
|
|
{ |
46
|
31 |
|
if (!array_key_exists($name, $this->row)) { |
47
|
31 |
|
$this->row[$name] = $value; |
48
|
14 |
|
} elseif (is_array($this->row[$name])) { |
49
|
14 |
|
$this->row[$name][] = $value; |
50
|
|
|
} else { |
51
|
14 |
|
$this->row[$name] = array($this->row[$name], $value); |
52
|
|
|
} |
53
|
31 |
|
$this->informChanges(); |
54
|
31 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $name - Field name |
58
|
|
|
* @return string |
59
|
|
|
* @desc et the string value from a field name |
60
|
|
|
*/ |
61
|
20 |
|
public function get($name) |
62
|
|
|
{ |
63
|
20 |
|
if (!array_key_exists($name, $this->row)) { |
64
|
5 |
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
20 |
|
$result = $this->row[$name]; |
68
|
20 |
|
if (is_array($result)) { |
69
|
2 |
|
return array_shift($result); |
70
|
|
|
} else { |
71
|
19 |
|
return $result; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get array from a single field |
77
|
|
|
* |
78
|
|
|
* @param string $fieldName |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
3 |
|
public function getAsArray($fieldName) |
82
|
|
|
{ |
83
|
3 |
|
if (!array_key_exists($fieldName, $this->row)) { |
84
|
|
|
return []; |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
$result = $this->row[$fieldName]; |
88
|
|
|
|
89
|
3 |
|
if (empty($result)) { |
90
|
1 |
|
return []; |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
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
|
|
|
} 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
|
|
|
} |
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
|
|
|
} |
147
|
|
|
} 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
|
|
|
} |
154
|
|
|
} |
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
|
|
|
} |
174
|
|
|
} 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
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
1 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get the \DOMElement row objet |
186
|
|
|
* |
187
|
|
|
* @return \DOMElement |
188
|
|
|
* @throws \ByJG\Util\Exception\XmlUtilException |
189
|
|
|
*/ |
190
|
4 |
|
public function getAsDom() |
191
|
|
|
{ |
192
|
4 |
|
if (is_null($this->node)) { |
193
|
4 |
|
$this->node = XmlUtil::createXmlDocumentFromStr("<row></row>"); |
|
|
|
|
194
|
4 |
|
$root = $this->node->getElementsByTagName("row")->item(0); |
195
|
4 |
|
foreach ($this->row as $key => $value) { |
196
|
4 |
|
if (!is_array($value)) { |
197
|
4 |
|
$field = XmlUtil::createChild($root, "field", $value); |
198
|
4 |
|
XmlUtil::addAttribute($field, "name", $key); |
199
|
|
|
} else { |
200
|
1 |
|
foreach ($value as $valueItem) { |
201
|
1 |
|
$field = XmlUtil::createChild($root, "field", $valueItem); |
202
|
1 |
|
XmlUtil::addAttribute($field, "name", $key); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
4 |
|
return $this->node; |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
19 |
|
public function toArray() |
211
|
|
|
{ |
212
|
19 |
|
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
|
40 |
|
public function acceptChanges() |
251
|
|
|
{ |
252
|
40 |
|
$this->originalRow = $this->row; |
253
|
40 |
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* |
257
|
|
|
*/ |
258
|
1 |
|
public function rejectChanges() |
259
|
|
|
{ |
260
|
1 |
|
$this->row = $this->originalRow; |
261
|
1 |
|
} |
262
|
|
|
|
263
|
31 |
|
protected function informChanges() |
264
|
|
|
{ |
265
|
31 |
|
$this->node = null; |
266
|
31 |
|
} |
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..