Test Failed
Pull Request — master (#10)
by Joao
05:30
created

Row   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 282
Duplicated Lines 0 %

Test Coverage

Coverage 94.06%

Importance

Changes 0
Metric Value
wmc 38
eloc 84
c 0
b 0
f 0
dl 0
loc 282
rs 9.36
ccs 95
cts 101
cp 0.9406

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A toArray() 0 3 1
A addField() 0 12 3
A set() 0 10 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 19 5
A getAsArray() 0 15 3
A replaceValue() 0 15 5
A setPropValue() 0 3 1
A acceptChanges() 0 3 1
A removeField() 0 7 2
A getAsRaw() 0 3 1
A getHydratedFieldName() 0 7 2
A hasChanges() 0 3 1
A informChanges() 0 3 1
1
<?php
2
3
namespace ByJG\AnyDataset\Core;
4
5
use ByJG\Serializer\BinderObject;
6
use ByJG\Serializer\DumpToArrayInterface;
7
8
class Row extends BinderObject implements DumpToArrayInterface
9
{
10
11
    /**
12
     * \DOMNode represents a Row
13
     * @var \DOMElement
14
     */
15
    private $node = null;
16
    private $row = null;
17
    private $originalRow = null;
18
19
    protected $fieldNameCaseSensitive = true;
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
        $name = $this->getHydratedFieldName($name);
47 31
48 14
        if (!array_key_exists($name, $this->row)) {
49 14
            $this->row[$name] = $value;
50
        } elseif (is_array($this->row[$name])) {
51 14
            $this->row[$name][] = $value;
52
        } else {
53 31
            $this->row[$name] = array($this->row[$name], $value);
54 31
        }
55
        $this->informChanges();
56
    }
57
58
    /**
59
     * @param string $name - Field name
60
     * @return string
61 20
     * @desc et the string value from a field name
62
     */
63 20
    public function get($name)
64 5
    {
65
        $name = $this->getHydratedFieldName($name);
66
67 20
        if (!array_key_exists($name, $this->row)) {
68 20
            return null;
69 2
        }
70
71 19
        $result = $this->row[$name];
72
        if (is_array($result)) {
73
            return array_shift($result);
74
        } else {
75
            return $result;
76
        }
77
    }
78
79
    /**
80
     * Get array from a single field
81 3
     *
82
     * @param string $fieldName
83 3
     * @return array
84
     */
85
    public function getAsArray($fieldName)
86
    {
87 3
        $fieldName = $this->getHydratedFieldName($fieldName);
88
89 3
        if (!array_key_exists($fieldName, $this->row)) {
90 1
            return [];
91
        }
92
93 3
        $result = $this->row[$fieldName];
94
95
        if (empty($result)) {
96
            return [];
97
        }
98
99
        return (array)$result;
100 1
    }
101
102 1
    /**
103
     * Return all Field Names from current Row
104
     * @return array
105
     */
106
    public function getFieldNames()
107
    {
108
        return array_keys($this->row);
109
    }
110 9
111
    /**
112 9
     * Set a string value to existing field name
113 5
     * @param string $name
114
     * @param string $value
115 5
     */
116
    public function set($name, $value)
117 9
    {
118 9
        $name = $this->getHydratedFieldName($name);
119
120
        if (!array_key_exists($name, $this->row)) {
121
            $this->addField($name, $value);
122
        } else {
123
            $this->row[$name] = $value;
124
        }
125 2
        $this->informChanges();
126
    }
127 2
128 2
    /**
129 2
     * Remove specified field name from row.
130
     *
131 2
     * @param string $fieldName
132
     */
133
    public function removeField($fieldName)
134
    {
135
        $fieldName = $this->getHydratedFieldName($fieldName);
136
137
        if (array_key_exists($fieldName, $this->row)) {
138
            unset($this->row[$fieldName]);
139 1
            $this->informChanges();
140
        }
141 1
    }
142 1
143 1
    /**
144 1
     * Remove specified field name with specified value name from row.
145 1
     *
146
     * @param string $fieldName
147
     * @param $value
148 1
     */
149 1
    public function removeValue($fieldName, $value)
150 1
    {
151 1
        $fieldName = $this->getHydratedFieldName($fieldName);
152 1
153
        $result = $this->row[$fieldName];
154
        if (!is_array($result)) {
155 1
            if ($value == $result) {
156
                unset($this->row[$fieldName]);
157 1
                $this->informChanges();
158
            }
159
        } else {
160
            $qty = count($result);
161
            for ($i = 0; $i < $qty; $i++) {
162
                if ($result[$i] == $value) {
163
                    unset($result[$i]);
164
                    $this->informChanges();
165
                }
166 1
            }
167
            $this->row[$fieldName] = array_values($result);
168 1
        }
169 1
    }
170 1
171 1
    /**
172 1
     * Update a specific field and specific value with new value
173
     *
174
     * @param String $fieldName
175 1
     * @param String $oldvalue
176 1
     * @param String $newvalue
177 1
     */
178 1
    public function replaceValue($fieldName, $oldvalue, $newvalue)
179
    {
180
        $fieldName = $this->getHydratedFieldName($fieldName);
181
182 1
        $result = $this->row[$fieldName];
183
        if (!is_array($result)) {
184
            if ($oldvalue == $result) {
185
                $this->row[$fieldName] = $newvalue;
186
                $this->informChanges();
187
            }
188
        } else {
189
            for ($i = count($result) - 1; $i >= 0; $i--) {
190 4
                if ($result[$i] == $oldvalue) {
191
                    $this->row[$fieldName][$i] = $newvalue;
192 4
                    $this->informChanges();
193 4
                }
194 4
            }
195 4
        }
196 4
    }
197 4
198 4
    public function toArray()
199
    {
200 1
        return $this->row;
201 1
    }
202 1
203
    /**
204
     * @return array
205
     */
206
    public function getAsRaw()
207 4
    {
208
        return $this->originalRow;
209
    }
210 19
211
    /**
212 19
     *
213
     * @return bool
214
     */
215
    public function hasChanges()
216
    {
217
        return ($this->row != $this->originalRow);
218
    }
219
220
    /**
221
     *
222
     */
223
    public function acceptChanges()
224
    {
225
        $this->originalRow = $this->row;
226
    }
227
228
    /**
229
     *
230
     */
231
    public function rejectChanges()
232
    {
233 2
        $this->row = $this->originalRow;
234
    }
235 2
236
    protected function informChanges()
237
    {
238
        $this->node = null;
239
    }
240
241
    /**
242 1
     * Override Specific implementation of setPropValue to Row
243
     *
244 1
     * @param Row $obj
245
     * @param string $propName
246
     * @param string $value
247
     */
248
    protected function setPropValue($obj, $propName, $value)
249
    {
250 40
        $obj->set($propName, $value);
251
    }
252 40
253 40
    /**
254
     * @return bool
255
     */
256
    public function fieldExists($name)
257
    {
258 1
        return isset($this->row[$this->getHydratedFieldName($name)]);
259
    }
260 1
261 1
    /**
262
     * @return void
263 31
     */
264
    public function enableFieldNameCaseInSensitive() 
265 31
    {
266 31
        $this->row = array_change_key_case($this->row, CASE_LOWER);
267
        $this->originalRow = array_change_key_case($this->originalRow, CASE_LOWER);
268
        $this->fieldNameCaseSensitive = false;
269
    }
270
271
    /**
272
     * @return bool
273
     */
274
    public function isFieldNameCaseSensitive()
275 4
    {
276
        return $this->fieldNameCaseSensitive;
277 4
    }
278 4
279
    /**
280
     * @param string name
0 ignored issues
show
Bug introduced by
The type ByJG\AnyDataset\Core\name was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
281
     * @return string
282
     */
283
    protected function getHydratedFieldName($name)
284
    {
285
        if (!$this->isFieldNameCaseSensitive()) {
286
            return strtolower($name);
287
        }
288
289
        return $name;
290
    }
291
}
292