1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Core; |
4
|
|
|
|
5
|
|
|
use ByJG\Serializer\SerializerObject; |
6
|
|
|
|
7
|
|
|
class Row |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* \DOMNode represents a Row |
12
|
|
|
* @var \DOMElement |
13
|
|
|
*/ |
14
|
|
|
private $node = null; |
15
|
|
|
private $row = null; |
16
|
|
|
private $originalRow = null; |
17
|
|
|
|
18
|
|
|
protected $fieldNameCaseSensitive = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Row constructor |
22
|
|
|
* |
23
|
|
|
* @param Row|array|\stdClass|object $instance |
24
|
|
|
* @throws \ByJG\Serializer\Exception\InvalidArgumentException |
25
|
|
|
*/ |
26
|
50 |
|
public function __construct($instance = []) |
27
|
|
|
{ |
28
|
50 |
|
if (is_array($instance)) { |
29
|
50 |
|
$this->row = $instance; |
30
|
|
|
} else { |
31
|
4 |
|
$this->row = SerializerObject::instance($instance)->serialize(); |
32
|
|
|
} |
33
|
|
|
|
34
|
50 |
|
$this->acceptChanges(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add a string field to row |
39
|
|
|
* @param string $name |
40
|
|
|
* @param string $value |
41
|
|
|
*/ |
42
|
32 |
|
public function addField($name, $value) |
43
|
|
|
{ |
44
|
32 |
|
$name = $this->getHydratedFieldName($name); |
45
|
|
|
|
46
|
32 |
|
if (!array_key_exists($name, $this->row)) { |
47
|
32 |
|
$this->row[$name] = $value; |
48
|
15 |
|
} elseif (is_array($this->row[$name])) { |
49
|
15 |
|
$this->row[$name][] = $value; |
50
|
|
|
} else { |
51
|
15 |
|
$this->row[$name] = array($this->row[$name], $value); |
52
|
|
|
} |
53
|
32 |
|
$this->informChanges(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $name - Field name |
58
|
|
|
* @return string |
59
|
|
|
* @desc et the string value from a field name |
60
|
|
|
*/ |
61
|
25 |
|
public function get($name) |
62
|
|
|
{ |
63
|
25 |
|
$name = $this->getHydratedFieldName($name); |
64
|
|
|
|
65
|
25 |
|
if (!array_key_exists($name, $this->row)) { |
66
|
6 |
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
25 |
|
$result = $this->row[$name]; |
70
|
25 |
|
if (is_array($result)) { |
71
|
2 |
|
return array_shift($result); |
72
|
|
|
} else { |
73
|
24 |
|
return $result; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get array from a single field |
79
|
|
|
* |
80
|
|
|
* @param string $fieldName |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
3 |
|
public function getAsArray($fieldName) |
84
|
|
|
{ |
85
|
3 |
|
$fieldName = $this->getHydratedFieldName($fieldName); |
86
|
|
|
|
87
|
3 |
|
if (!array_key_exists($fieldName, $this->row)) { |
88
|
|
|
return []; |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
$result = $this->row[$fieldName]; |
92
|
|
|
|
93
|
3 |
|
if (empty($result)) { |
94
|
1 |
|
return []; |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
return (array)$result; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return all Field Names from current Row |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
1 |
|
public function getFieldNames() |
105
|
|
|
{ |
106
|
1 |
|
return array_keys($this->row); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set a string value to existing field name |
111
|
|
|
* @param string $name |
112
|
|
|
* @param string $value |
113
|
|
|
*/ |
114
|
7 |
|
public function set($name, $value) |
115
|
|
|
{ |
116
|
7 |
|
$name = $this->getHydratedFieldName($name); |
117
|
|
|
|
118
|
7 |
|
if (!array_key_exists($name, $this->row)) { |
119
|
3 |
|
$this->addField($name, $value); |
120
|
|
|
} else { |
121
|
6 |
|
$this->row[$name] = $value; |
122
|
|
|
} |
123
|
7 |
|
$this->informChanges(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Remove specified field name from row. |
128
|
|
|
* |
129
|
|
|
* @param string $fieldName |
130
|
|
|
*/ |
131
|
3 |
|
public function removeField($fieldName) |
132
|
|
|
{ |
133
|
3 |
|
$fieldName = $this->getHydratedFieldName($fieldName); |
134
|
|
|
|
135
|
3 |
|
if (array_key_exists($fieldName, $this->row)) { |
136
|
3 |
|
unset($this->row[$fieldName]); |
137
|
3 |
|
$this->informChanges(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Remove specified field name with specified value name from row. |
143
|
|
|
* |
144
|
|
|
* @param string $fieldName |
145
|
|
|
* @param $value |
146
|
|
|
*/ |
147
|
1 |
|
public function removeValue($fieldName, $value) |
148
|
|
|
{ |
149
|
1 |
|
$fieldName = $this->getHydratedFieldName($fieldName); |
150
|
|
|
|
151
|
1 |
|
$result = $this->row[$fieldName]; |
152
|
1 |
|
if (!is_array($result)) { |
153
|
1 |
|
if ($value == $result) { |
154
|
1 |
|
unset($this->row[$fieldName]); |
155
|
1 |
|
$this->informChanges(); |
156
|
|
|
} |
157
|
|
|
} else { |
158
|
1 |
|
$qty = count($result); |
159
|
1 |
|
for ($i = 0; $i < $qty; $i++) { |
160
|
1 |
|
if ($result[$i] == $value) { |
161
|
1 |
|
unset($result[$i]); |
162
|
1 |
|
$this->informChanges(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
1 |
|
$this->row[$fieldName] = array_values($result); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Update a specific field and specific value with new value |
171
|
|
|
* |
172
|
|
|
* @param String $fieldName |
173
|
|
|
* @param String $oldvalue |
174
|
|
|
* @param String $newvalue |
175
|
|
|
*/ |
176
|
1 |
|
public function replaceValue($fieldName, $oldvalue, $newvalue) |
177
|
|
|
{ |
178
|
1 |
|
$fieldName = $this->getHydratedFieldName($fieldName); |
179
|
|
|
|
180
|
1 |
|
$result = $this->row[$fieldName]; |
181
|
1 |
|
if (!is_array($result)) { |
182
|
1 |
|
if ($oldvalue == $result) { |
183
|
1 |
|
$this->row[$fieldName] = $newvalue; |
184
|
1 |
|
$this->informChanges(); |
185
|
|
|
} |
186
|
|
|
} else { |
187
|
1 |
|
for ($i = count($result) - 1; $i >= 0; $i--) { |
188
|
1 |
|
if ($result[$i] == $oldvalue) { |
189
|
1 |
|
$this->row[$fieldName][$i] = $newvalue; |
190
|
1 |
|
$this->informChanges(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
28 |
|
public function toArray($fields = []) |
197
|
|
|
{ |
198
|
28 |
|
if (empty($fields)) { |
199
|
26 |
|
return $this->row; |
200
|
|
|
} |
201
|
|
|
|
202
|
2 |
|
$fieldAssoc = array_combine($fields, array_fill(0, count($fields), null)); |
203
|
2 |
|
return array_intersect_key(array_merge($fieldAssoc, $this->row), $fieldAssoc); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
2 |
|
public function getAsRaw() |
210
|
|
|
{ |
211
|
2 |
|
return $this->originalRow; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
1 |
|
public function hasChanges() |
219
|
|
|
{ |
220
|
1 |
|
return ($this->row != $this->originalRow); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* |
225
|
|
|
*/ |
226
|
50 |
|
public function acceptChanges() |
227
|
|
|
{ |
228
|
50 |
|
$this->originalRow = $this->row; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* |
233
|
|
|
*/ |
234
|
1 |
|
public function rejectChanges() |
235
|
|
|
{ |
236
|
1 |
|
$this->row = $this->originalRow; |
237
|
|
|
} |
238
|
|
|
|
239
|
32 |
|
protected function informChanges() |
240
|
|
|
{ |
241
|
32 |
|
$this->node = null; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Override Specific implementation of setPropValue to Row |
246
|
|
|
* |
247
|
|
|
* @param Row $obj |
248
|
|
|
* @param string $propName |
249
|
|
|
* @param string $value |
250
|
|
|
*/ |
251
|
|
|
protected function setPropValue($obj, $propName, $value) |
252
|
|
|
{ |
253
|
|
|
$obj->set($propName, $value); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
1 |
|
public function fieldExists($name) |
260
|
|
|
{ |
261
|
1 |
|
return isset($this->row[$this->getHydratedFieldName($name)]); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return void |
266
|
|
|
*/ |
267
|
2 |
|
public function enableFieldNameCaseInSensitive() |
268
|
|
|
{ |
269
|
2 |
|
$this->row = array_change_key_case($this->row, CASE_LOWER); |
270
|
2 |
|
$this->originalRow = array_change_key_case($this->originalRow, CASE_LOWER); |
271
|
2 |
|
$this->fieldNameCaseSensitive = false; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @return bool |
276
|
|
|
*/ |
277
|
47 |
|
public function isFieldNameCaseSensitive() |
278
|
|
|
{ |
279
|
47 |
|
return $this->fieldNameCaseSensitive; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param string name |
|
|
|
|
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
47 |
|
protected function getHydratedFieldName($name) |
287
|
|
|
{ |
288
|
47 |
|
if (!$this->isFieldNameCaseSensitive()) { |
289
|
2 |
|
return strtolower($name); |
290
|
|
|
} |
291
|
|
|
|
292
|
47 |
|
return $name; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths