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
|
|
|
protected $fieldNameCaseSensitive = true; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Row constructor |
25
|
|
|
* |
26
|
|
|
* @param array() |
27
|
|
|
* @throws \ByJG\Serializer\Exception\InvalidArgumentException |
28
|
|
|
*/ |
29
|
37 |
|
public function __construct($instance = null) |
30
|
|
|
{ |
31
|
37 |
|
if (is_null($instance)) { |
32
|
35 |
|
$this->row = array(); |
33
|
11 |
|
} elseif (is_array($instance)) { |
34
|
7 |
|
$this->row = $instance; |
35
|
|
|
} else { |
36
|
4 |
|
$this->row = array(); |
37
|
4 |
|
$this->bind($instance); |
38
|
|
|
} |
39
|
|
|
|
40
|
37 |
|
$this->acceptChanges(); |
41
|
37 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add a string field to row |
45
|
|
|
* @param string $name |
46
|
|
|
* @param string $value |
47
|
|
|
*/ |
48
|
32 |
|
public function addField($name, $value) |
49
|
|
|
{ |
50
|
32 |
|
$name = $this->getHydratedFieldName($name); |
51
|
|
|
|
52
|
32 |
|
if (!array_key_exists($name, $this->row)) { |
53
|
32 |
|
$this->row[$name] = $value; |
54
|
14 |
|
} elseif (is_array($this->row[$name])) { |
55
|
14 |
|
$this->row[$name][] = $value; |
56
|
|
|
} else { |
57
|
14 |
|
$this->row[$name] = array($this->row[$name], $value); |
58
|
|
|
} |
59
|
32 |
|
$this->informChanges(); |
60
|
32 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $name - Field name |
64
|
|
|
* @return string |
65
|
|
|
* @desc et the string value from a field name |
66
|
|
|
*/ |
67
|
17 |
|
public function get($name) |
68
|
|
|
{ |
69
|
17 |
|
$name = $this->getHydratedFieldName($name); |
70
|
|
|
|
71
|
17 |
|
if (!array_key_exists($name, $this->row)) { |
72
|
5 |
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
17 |
|
$result = $this->row[$name]; |
76
|
17 |
|
if (is_array($result)) { |
77
|
2 |
|
return array_shift($result); |
78
|
|
|
} else { |
79
|
16 |
|
return $result; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get array from a single field |
85
|
|
|
* |
86
|
|
|
* @param string $fieldName |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
3 |
|
public function getAsArray($fieldName) |
90
|
|
|
{ |
91
|
3 |
|
$fieldName = $this->getHydratedFieldName($fieldName); |
92
|
|
|
|
93
|
3 |
|
if (!array_key_exists($fieldName, $this->row)) { |
94
|
|
|
return []; |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
$result = $this->row[$fieldName]; |
98
|
|
|
|
99
|
3 |
|
if (empty($result)) { |
100
|
1 |
|
return []; |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
return (array)$result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return all Field Names from current Row |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
1 |
|
public function getFieldNames() |
111
|
|
|
{ |
112
|
1 |
|
return array_keys($this->row); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set a string value to existing field name |
117
|
|
|
* @param string $name |
118
|
|
|
* @param string $value |
119
|
|
|
*/ |
120
|
10 |
|
public function set($name, $value) |
121
|
|
|
{ |
122
|
10 |
|
$name = $this->getHydratedFieldName($name); |
123
|
|
|
|
124
|
10 |
|
if (!array_key_exists($name, $this->row)) { |
125
|
6 |
|
$this->addField($name, $value); |
126
|
|
|
} else { |
127
|
6 |
|
$this->row[$name] = $value; |
128
|
|
|
} |
129
|
10 |
|
$this->informChanges(); |
130
|
10 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Remove specified field name from row. |
134
|
|
|
* |
135
|
|
|
* @param string $fieldName |
136
|
|
|
*/ |
137
|
3 |
|
public function removeField($fieldName) |
138
|
|
|
{ |
139
|
3 |
|
$fieldName = $this->getHydratedFieldName($fieldName); |
140
|
|
|
|
141
|
3 |
|
if (array_key_exists($fieldName, $this->row)) { |
142
|
3 |
|
unset($this->row[$fieldName]); |
143
|
3 |
|
$this->informChanges(); |
144
|
|
|
} |
145
|
3 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Remove specified field name with specified value name from row. |
149
|
|
|
* |
150
|
|
|
* @param string $fieldName |
151
|
|
|
* @param $value |
152
|
|
|
*/ |
153
|
1 |
|
public function removeValue($fieldName, $value) |
154
|
|
|
{ |
155
|
1 |
|
$fieldName = $this->getHydratedFieldName($fieldName); |
156
|
|
|
|
157
|
1 |
|
$result = $this->row[$fieldName]; |
158
|
1 |
|
if (!is_array($result)) { |
159
|
1 |
|
if ($value == $result) { |
160
|
1 |
|
unset($this->row[$fieldName]); |
161
|
1 |
|
$this->informChanges(); |
162
|
|
|
} |
163
|
|
|
} else { |
164
|
1 |
|
$qty = count($result); |
165
|
1 |
|
for ($i = 0; $i < $qty; $i++) { |
166
|
1 |
|
if ($result[$i] == $value) { |
167
|
1 |
|
unset($result[$i]); |
168
|
1 |
|
$this->informChanges(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
1 |
|
$this->row[$fieldName] = array_values($result); |
172
|
|
|
} |
173
|
1 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Update a specific field and specific value with new value |
177
|
|
|
* |
178
|
|
|
* @param String $fieldName |
179
|
|
|
* @param String $oldvalue |
180
|
|
|
* @param String $newvalue |
181
|
|
|
*/ |
182
|
1 |
|
public function replaceValue($fieldName, $oldvalue, $newvalue) |
183
|
|
|
{ |
184
|
1 |
|
$fieldName = $this->getHydratedFieldName($fieldName); |
185
|
|
|
|
186
|
1 |
|
$result = $this->row[$fieldName]; |
187
|
1 |
|
if (!is_array($result)) { |
188
|
1 |
|
if ($oldvalue == $result) { |
189
|
1 |
|
$this->row[$fieldName] = $newvalue; |
190
|
1 |
|
$this->informChanges(); |
191
|
|
|
} |
192
|
|
|
} else { |
193
|
1 |
|
for ($i = count($result) - 1; $i >= 0; $i--) { |
194
|
1 |
|
if ($result[$i] == $oldvalue) { |
195
|
1 |
|
$this->row[$fieldName][$i] = $newvalue; |
196
|
1 |
|
$this->informChanges(); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
1 |
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get the \DOMElement row objet |
204
|
|
|
* |
205
|
|
|
* @return \DOMElement |
206
|
|
|
* @throws \ByJG\Util\Exception\XmlUtilException |
207
|
|
|
*/ |
208
|
4 |
|
public function getAsDom() |
209
|
|
|
{ |
210
|
4 |
|
if (is_null($this->node)) { |
211
|
4 |
|
$this->node = XmlUtil::createXmlDocumentFromStr("<row></row>"); |
|
|
|
|
212
|
4 |
|
$root = $this->node->getElementsByTagName("row")->item(0); |
213
|
4 |
|
foreach ($this->row as $key => $value) { |
214
|
4 |
|
if (!is_array($value)) { |
215
|
4 |
|
$field = XmlUtil::createChild($root, "field", $value); |
216
|
4 |
|
XmlUtil::addAttribute($field, "name", $key); |
217
|
|
|
} else { |
218
|
1 |
|
foreach ($value as $valueItem) { |
219
|
1 |
|
$field = XmlUtil::createChild($root, "field", $valueItem); |
220
|
1 |
|
XmlUtil::addAttribute($field, "name", $key); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
4 |
|
return $this->node; |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
18 |
|
public function toArray() |
229
|
|
|
{ |
230
|
18 |
|
return $this->row; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
|
|
public function getAsJSON() |
238
|
|
|
{ |
239
|
|
|
if (is_array($this->row)) { |
|
|
|
|
240
|
|
|
return json_decode(json_encode($this->row)); |
241
|
|
|
} else { |
242
|
|
|
throw new UnexpectedValueException( |
243
|
|
|
'I expected that getRawFormat is array() but ' . gettype($this->row) . ' was given' |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
2 |
|
public function getAsRaw() |
252
|
|
|
{ |
253
|
2 |
|
return $this->originalRow; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* |
258
|
|
|
* @return bool |
259
|
|
|
*/ |
260
|
1 |
|
public function hasChanges() |
261
|
|
|
{ |
262
|
1 |
|
return ($this->row != $this->originalRow); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* |
267
|
|
|
*/ |
268
|
37 |
|
public function acceptChanges() |
269
|
|
|
{ |
270
|
37 |
|
$this->originalRow = $this->row; |
271
|
37 |
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* |
275
|
|
|
*/ |
276
|
1 |
|
public function rejectChanges() |
277
|
|
|
{ |
278
|
1 |
|
$this->row = $this->originalRow; |
279
|
1 |
|
} |
280
|
|
|
|
281
|
32 |
|
protected function informChanges() |
282
|
|
|
{ |
283
|
32 |
|
$this->node = null; |
284
|
32 |
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Override Specific implementation of setPropValue to Row |
288
|
|
|
* |
289
|
|
|
* @param Row $obj |
290
|
|
|
* @param string $propName |
291
|
|
|
* @param string $value |
292
|
|
|
*/ |
293
|
4 |
|
protected function setPropValue($obj, $propName, $value) |
294
|
|
|
{ |
295
|
4 |
|
$obj->set($propName, $value); |
296
|
4 |
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return bool |
300
|
|
|
*/ |
301
|
1 |
|
public function fieldExists($name) |
302
|
|
|
{ |
303
|
1 |
|
return isset($this->row[$this->getHydratedFieldName($name)]); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return void |
308
|
|
|
*/ |
309
|
2 |
|
public function enableFieldNameCaseInSensitive() |
310
|
|
|
{ |
311
|
2 |
|
$this->row = array_change_key_case($this->row, CASE_LOWER); |
312
|
2 |
|
$this->originalRow = array_change_key_case($this->originalRow, CASE_LOWER); |
313
|
2 |
|
$this->fieldNameCaseSensitive = false; |
314
|
2 |
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @return bool |
318
|
|
|
*/ |
319
|
36 |
|
public function isFieldNameCaseSensitive() |
320
|
|
|
{ |
321
|
36 |
|
return $this->fieldNameCaseSensitive; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @params name Fieldname |
326
|
|
|
* @return string |
327
|
|
|
*/ |
328
|
36 |
|
protected function getHydratedFieldName($name) |
329
|
|
|
{ |
330
|
36 |
|
if (!$this->isFieldNameCaseSensitive()) { |
331
|
2 |
|
return strtolower($name); |
332
|
|
|
} |
333
|
|
|
|
334
|
36 |
|
return $name; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
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..