Passed
Pull Request — master (#10)
by Joao
01:36
created

Row::getAsArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0175
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 45
    public function __construct($instance = [])
28
    {
29 45
        if (is_array($instance)) {
30 45
            $this->row = $instance;
31
        } else {
32 4
            $this->row = array();
33 4
            $this->bind($instance);
34
        }
35
36 45
        $this->acceptChanges();
37
    }
38
39
    /**
40
     * Add a string field to row
41
     * @param string $name
42
     * @param string $value
43
     */
44 35
    public function addField($name, $value)
45
    {
46 35
        $name = $this->getHydratedFieldName($name);
47
48 35
        if (!array_key_exists($name, $this->row)) {
49 35
            $this->row[$name] = $value;
50 15
        } elseif (is_array($this->row[$name])) {
51 15
            $this->row[$name][] = $value;
52
        } else {
53 15
            $this->row[$name] = array($this->row[$name], $value);
54
        }
55 35
        $this->informChanges();
56
    }
57
58
    /**
59
     * @param string $name - Field name
60
     * @return string
61
     * @desc et the string value from a field name
62
     */
63 22
    public function get($name)
64
    {
65 22
        $name = $this->getHydratedFieldName($name);
66
67 22
        if (!array_key_exists($name, $this->row)) {
68 6
            return null;
69
        }
70
71 22
        $result = $this->row[$name];
72 22
        if (is_array($result)) {
73 2
            return array_shift($result);
74
        } else {
75 21
            return $result;
76
        }
77
    }
78
79
    /**
80
     * Get array from a single field
81
     *
82
     * @param string $fieldName
83
     * @return array
84
     */
85 3
    public function getAsArray($fieldName)
86
    {
87 3
        $fieldName = $this->getHydratedFieldName($fieldName);
88
89 3
        if (!array_key_exists($fieldName, $this->row)) {
90
            return [];
91
        }
92
93 3
        $result = $this->row[$fieldName];
94
95 3
        if (empty($result)) {
96 1
            return [];
97
        }
98
99 3
        return (array)$result;
100
    }
101
102
    /**
103
     * Return all Field Names from current Row
104
     * @return array
105
     */
106 1
    public function getFieldNames()
107
    {
108 1
        return array_keys($this->row);
109
    }
110
111
    /**
112
     * Set a string value to existing field name
113
     * @param string $name
114
     * @param string $value
115
     */
116 10
    public function set($name, $value)
117
    {
118 10
        $name = $this->getHydratedFieldName($name);
119
120 10
        if (!array_key_exists($name, $this->row)) {
121 6
            $this->addField($name, $value);
122
        } else {
123 6
            $this->row[$name] = $value;
124
        }
125 10
        $this->informChanges();
126
    }
127
128
    /**
129
     * Remove specified field name from row.
130
     *
131
     * @param string $fieldName
132
     */
133 3
    public function removeField($fieldName)
134
    {
135 3
        $fieldName = $this->getHydratedFieldName($fieldName);
136
137 3
        if (array_key_exists($fieldName, $this->row)) {
138 3
            unset($this->row[$fieldName]);
139 3
            $this->informChanges();
140
        }
141
    }
142
143
    /**
144
     * Remove specified field name with specified value name from row.
145
     *
146
     * @param string $fieldName
147
     * @param $value
148
     */
149 1
    public function removeValue($fieldName, $value)
150
    {
151 1
        $fieldName = $this->getHydratedFieldName($fieldName);
152
153 1
        $result = $this->row[$fieldName];
154 1
        if (!is_array($result)) {
155 1
            if ($value == $result) {
156 1
                unset($this->row[$fieldName]);
157 1
                $this->informChanges();
158
            }
159
        } else {
160 1
            $qty = count($result);
161 1
            for ($i = 0; $i < $qty; $i++) {
162 1
                if ($result[$i] == $value) {
163 1
                    unset($result[$i]);
164 1
                    $this->informChanges();
165
                }
166
            }
167 1
            $this->row[$fieldName] = array_values($result);
168
        }
169
    }
170
171
    /**
172
     * Update a specific field and specific value with new value
173
     *
174
     * @param String $fieldName
175
     * @param String $oldvalue
176
     * @param String $newvalue
177
     */
178 1
    public function replaceValue($fieldName, $oldvalue, $newvalue)
179
    {
180 1
        $fieldName = $this->getHydratedFieldName($fieldName);
181
182 1
        $result = $this->row[$fieldName];
183 1
        if (!is_array($result)) {
184 1
            if ($oldvalue == $result) {
185 1
                $this->row[$fieldName] = $newvalue;
186 1
                $this->informChanges();
187
            }
188
        } else {
189 1
            for ($i = count($result) - 1; $i >= 0; $i--) {
190 1
                if ($result[$i] == $oldvalue) {
191 1
                    $this->row[$fieldName][$i] = $newvalue;
192 1
                    $this->informChanges();
193
                }
194
            }
195
        }
196
    }
197
198 24
    public function toArray()
199
    {
200 24
        return $this->row;
201
    }
202
203
    /**
204
     * @return array
205
     */
206 2
    public function getAsRaw()
207
    {
208 2
        return $this->originalRow;
209
    }
210
211
    /**
212
     *
213
     * @return bool
214
     */
215 1
    public function hasChanges()
216
    {
217 1
        return ($this->row != $this->originalRow);
218
    }
219
220
    /**
221
     *
222
     */
223 45
    public function acceptChanges()
224
    {
225 45
        $this->originalRow = $this->row;
226
    }
227
228
    /**
229
     *
230
     */
231 1
    public function rejectChanges()
232
    {
233 1
        $this->row = $this->originalRow;
234
    }
235
236 35
    protected function informChanges()
237
    {
238 35
        $this->node = null;
239
    }
240
241
    /**
242
     * Override Specific implementation of setPropValue to Row
243
     *
244
     * @param Row $obj
245
     * @param string $propName
246
     * @param string $value
247
     */
248 4
    protected function setPropValue($obj, $propName, $value)
249
    {
250 4
        $obj->set($propName, $value);
251
    }
252
253
    /**
254
     * @return bool
255
     */
256 1
    public function fieldExists($name)
257
    {
258 1
        return isset($this->row[$this->getHydratedFieldName($name)]);
259
    }
260
261
    /**
262
     * @return void
263
     */
264 2
    public function enableFieldNameCaseInSensitive() 
265
    {
266 2
        $this->row = array_change_key_case($this->row, CASE_LOWER);
267 2
        $this->originalRow = array_change_key_case($this->originalRow, CASE_LOWER);
268 2
        $this->fieldNameCaseSensitive = false;
269
    }
270
271
    /**
272
     * @return bool
273
     */
274 44
    public function isFieldNameCaseSensitive()
275
    {
276 44
        return $this->fieldNameCaseSensitive;
277
    }
278
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 44
    protected function getHydratedFieldName($name)
284
    {
285 44
        if (!$this->isFieldNameCaseSensitive()) {
286 2
            return strtolower($name);
287
        }
288
289 44
        return $name;
290
    }
291
}
292