Passed
Branch master (ef264b)
by Pierrick
01:53
created

ObjectNode   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 92.68%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 37
c 2
b 0
f 1
dl 0
loc 91
ccs 38
cts 41
cp 0.9268
rs 10
wmc 22

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getNativeValue() 0 7 2
A count() 0 4 1
A offsetGet() 0 4 1
A __construct() 0 4 2
B merge() 0 17 7
A offsetExists() 0 4 1
A has() 0 3 1
A offsetUnset() 0 4 1
A offsetSet() 0 8 2
A resolve() 0 6 3
A getIterator() 0 4 1
1
<?php
2
/**
3
 * This file is part of NACL.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2019 Nuglif (2018) Inc.
9
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
10
 * @author    Pierrick Charron <[email protected]>
11
 * @author    Charle Demers <[email protected]>
12
 */
13
14
declare(strict_types=1);
15
16
namespace Nuglif\Nacl;
17
18
class ObjectNode extends Node implements \IteratorAggregate, \ArrayAccess, \Countable
19
{
20
    private $value    = [];
21
    private $isNative = true;
22
23 511
    public function __construct(array $values = [])
24
    {
25 511
        foreach ($values as $k => $v) {
26 386
            $this[$k] = $v;
27
        }
28 511
    }
29
30
    #[\ReturnTypeWillChange]
31 14
    public function count()
32
    {
33 14
        return count($this->value);
34
    }
35
36 14
    public function merge(ObjectNode $a2)
37
    {
38 14
        if (0 === count($this)) {
39 10
            return $a2;
40 12
        } elseif (0 === count($a2)) {
41 2
            return $this;
42
        }
43
44 10
        foreach ($a2 as $key => $value) {
45 10
            if (!isset($this[$key]) || !($this[$key] instanceof ObjectNode) || !($value instanceof ObjectNode)) {
46 10
                $this[$key] = $value;
47
            } else {
48 3
                $this[$key] = $this[$key]->merge($value);
49
            }
50
        }
51
52 10
        return $this;
53
    }
54
55
    #[\ReturnTypeWillChange]
56 10
    public function getIterator()
57
    {
58 10
        return new \ArrayIterator($this->value);
59
    }
60
61
    #[\ReturnTypeWillChange]
62 507
    public function offsetSet($offset, $value)
63
    {
64 507
        if ($value instanceof Node) {
65 359
            $value->setParent($this);
66 359
            $this->isNative = false;
67
        }
68 507
        $this->value[$offset] = $value;
69 507
    }
70
71
    #[\ReturnTypeWillChange]
72 15
    public function offsetGet($offset)
73
    {
74 15
        return $this->value[$offset];
75
    }
76
77 3
    public function has($offset)
78
    {
79 3
        return array_key_exists($offset, $this->value);
80
    }
81
82
    #[\ReturnTypeWillChange]
83 304
    public function offsetExists($offset)
84
    {
85 304
        return isset($this->value[$offset]);
86
    }
87
88
    #[\ReturnTypeWillChange]
89
    public function offsetUnset($offset)
90
    {
91
        unset($this->value[$offset]);
92
    }
93
94 510
    public function getNativeValue()
95
    {
96 510
        if (!$this->isNative) {
97 358
            $this->resolve();
98
        }
99
100 508
        return $this->value;
101
    }
102
103 358
    private function resolve()
104
    {
105 358
        foreach ($this->value as $k => $v) {
106 358
            $this->value[$k] = $v instanceof Node ? $v->getNativeValue() : $v;
107
        }
108 355
        $this->isNative = true;
109 355
    }
110
}
111