Completed
Push — master ( ae0103...9b98c5 )
by Joao
04:23 queued 56s
created

Row::getAsDom()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 16
cts 16
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 13
nc 2
nop 0
crap 5
1
<?php
2
3
namespace ByJG\AnyDataset\Dataset;
4
5
use ByJG\Serializer\BinderObject;
6
use ByJG\Serializer\DumpToArrayInterface;
7
use ByJG\Util\XmlUtil;
8
use UnexpectedValueException;
9
10
class Row extends BinderObject implements DumpToArrayInterface
11
{
12
13
    /**
14
     * \DOMNode represents a Row
15
     * @var \DOMElement
16
     */
17
    private $node = null;
18
    private $row = null;
19
    private $originalRow = null;
20
21
    /**
22
     * Row constructor
23
     * @param array()
24
     */
25 69
    public function __construct($instance = null)
26
    {
27 69
        if (is_null($instance)) {
28 54
            $this->row = array();
29 69
        } elseif (is_array($instance)) {
30 18
            $this->row = $instance;
31 18
        } else {
32 4
            $this->row = array();
33 4
            $this->bind($instance);
34
        }
35
36 69
        $this->acceptChanges();
37 69
    }
38
39
    /**
40
     * Add a string field to row
41
     * @param string $name
42
     * @param string $value
43
     */
44 52
    public function addField($name, $value)
45
    {
46 52
        if (!array_key_exists($name, $this->row)) {
47 52
            $this->row[$name] = $value;
48 52
        } elseif (is_array($this->row[$name])) {
49 14
            $this->row[$name][] = $value;
50 14
        } else {
51 15
            $this->row[$name] = array($this->row[$name], $value);
52
        }
53 52
        $this->informChanges();
54 52
    }
55
56
    /**
57
     * @param string $name - Field name
58
     * @return string
59
     * @desc et the string value from a field name
60
     */
61 36
    public function get($name)
62
    {
63 36
        if (!array_key_exists($name, $this->row)) {
64 4
            return null;
65
        }
66
67 36
        $result = $this->row[$name];
68 36
        if (is_array($result)) {
69 2
            return array_shift($result);
70
        } else {
71 35
            return $result;
72
        }
73
    }
74
75
    /**
76
     * Get array from a single field
77
     *
78
     * @param string $fieldName
79
     * @return array
80
     */
81 4
    public function getAsArray($fieldName)
82
    {
83 4
        if (!array_key_exists($fieldName, $this->row)) {
84
            return [];
85
        }
86
87 4
        $result = $this->row[$fieldName];
88
89 4
        if (empty($result)) {
90 1
            return [];
91
        }
92
93 4
        return (array)$result;
94
    }
95
96
    /**
97
     * Return all Field Names from current Row
98
     * @return array
99
     */
100 1
    public function getFieldNames()
101
    {
102 1
        return array_keys($this->row);
103
    }
104
105
    /**
106
     * Set a string value to existing field name
107
     * @param string $name
108
     * @param string $value
109
     */
110 9
    public function set($name, $value)
111
    {
112 9
        if (!array_key_exists($name, $this->row)) {
113 5
            $this->addField($name, $value);
114 5
        } else {
115 5
            $this->row[$name] = $value;
116
        }
117 9
        $this->informChanges();
118 9
    }
119
120
    /**
121
     * Remove specified field name from row.
122
     *
123
     * @param string $fieldName
124
     */
125 2
    public function removeField($fieldName)
126
    {
127 2
        if (array_key_exists($fieldName, $this->row)) {
128 2
            unset($this->row[$fieldName]);
129 2
            $this->informChanges();
130 2
        }
131 2
    }
132
133
    /**
134
     * Remove specified field name with specified value name from row.
135
     *
136
     * @param string $fieldName
137
     * @param $value
138
     */
139 1
    public function removeValue($fieldName, $value)
140
    {
141 1
        $result = $this->row[$fieldName];
142 1
        if (!is_array($result)) {
143 1
            if ($value == $result) {
144 1
                unset($this->row[$fieldName]);
145 1
                $this->informChanges();
146 1
            }
147 1
        } else {
148 1
            $qty = count($result);
149 1
            for ($i = 0; $i < $qty; $i++) {
150 1
                if ($result[$i] == $value) {
151 1
                    unset($result[$i]);
152 1
                    $this->informChanges();
153 1
                }
154 1
            }
155 1
            $this->row[$fieldName] = array_values($result);
156
        }
157 1
    }
158
159
    /**
160
     * Update a specific field and specific value with new value
161
     *
162
     * @param String $fieldName
163
     * @param String $oldvalue
164
     * @param String $newvalue
165
     */
166 1
    public function replaceValue($fieldName, $oldvalue, $newvalue)
167
    {
168 1
        $result = $this->row[$fieldName];
169 1
        if (!is_array($result)) {
170 1
            if ($oldvalue == $result) {
171 1
                $this->row[$fieldName] = $newvalue;
172 1
                $this->informChanges();
173 1
            }
174 1
        } else {
175 1
            for ($i = count($result) - 1; $i >= 0; $i--) {
176 1
                if ($result[$i] == $oldvalue) {
177 1
                    $this->row[$fieldName][$i] = $newvalue;
178 1
                    $this->informChanges();
179 1
                }
180 1
            }
181
        }
182 1
    }
183
184
    /**
185
     * Get the \DOMElement row objet
186
     * @return \DOMElement
187
     */
188 3
    public function getAsDom()
189
    {
190 3
        if (is_null($this->node)) {
191 3
            $this->node = XmlUtil::createXmlDocumentFromStr("<row></row>");
0 ignored issues
show
Documentation Bug introduced by
It seems like \ByJG\Util\XmlUtil::crea...tFromStr('<row></row>') of type object<DOMDocument> is incompatible with the declared type object<DOMElement> of property $node.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
192 3
            $root = $this->node->getElementsByTagName("row")->item(0);
193 3
            foreach ($this->row as $key => $value) {
194 3
                if (!is_array($value)) {
195 3
                    $field = XmlUtil::createChild($root, "field", $value);
196 3
                    XmlUtil::addAttribute($field, "name", $key);
197 3
                } else {
198 1
                    foreach ($value as $valueItem) {
199 1
                        $field = XmlUtil::createChild($root, "field", $valueItem);
200 1
                        XmlUtil::addAttribute($field, "name", $key);
201 1
                    }
202
                }
203 3
            }
204 3
        }
205 3
        return $this->node;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->node; of type DOMDocument|DOMElement adds the type DOMDocument to the return on line 205 which is incompatible with the return type documented by ByJG\AnyDataset\Dataset\Row::getAsDom of type DOMElement.
Loading history...
206
    }
207
208 30
    public function toArray()
209
    {
210 30
        return $this->row;
211
    }
212
213
    /**
214
     *
215
     * @return array
216
     */
217
    public function getAsJSON()
218
    {
219
        if (is_array($this->row)) {
220
            return json_decode(json_encode($this->row));
221
        } else {
222
            throw new UnexpectedValueException('I expected that getRawFormat is array() but ' . gettype($this->row) . ' was given');
223
        }
224
    }
225
226
    /**
227
     * @return array
228
     */
229 2
    public function getAsRaw()
230
    {
231 2
        return $this->originalRow;
232
    }
233
234
    /**
235
     *
236
     * @return bool
237
     */
238 1
    public function hasChanges()
239
    {
240 1
        return ($this->row != $this->originalRow);
241
    }
242
243
    /**
244
     *
245
     */
246 69
    public function acceptChanges()
247
    {
248 69
        $this->originalRow = $this->row;
249 69
    }
250
251
    /**
252
     *
253
     */
254 1
    public function rejectChanges()
255
    {
256 1
        $this->row = $this->originalRow;
257 1
    }
258
259 52
    protected function informChanges()
260
    {
261 52
        $this->node = null;
262 52
    }
263
264
    /**
265
     * Override Specific implementation of setPropValue to Row
266
     *
267
     * @param Row $obj
268
     * @param string $propName
269
     * @param string $value
270
     */
271 4
    protected function setPropValue($obj, $propName, $value)
272
    {
273 4
        $obj->set($propName, $value);
274 4
    }
275
276
277
}
278