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

Row::fieldExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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