Row   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 290
Duplicated Lines 0 %

Test Coverage

Coverage 96.51%

Importance

Changes 0
Metric Value
wmc 38
eloc 77
dl 0
loc 290
ccs 83
cts 86
cp 0.9651
rs 9.36
c 0
b 0
f 0

19 Methods

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