Completed
Push — master ( 9afc27...035c9e )
by Chris
04:26
created

ObjectNode   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 34.15%

Importance

Changes 0
Metric Value
wmc 21
dl 0
loc 123
ccs 14
cts 41
cp 0.3415
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getPropertyNames() 0 3 1
A removeProperty() 0 3 1
A getValue() 0 9 2
A getProperty() 0 7 2
A hasProperty() 0 3 1
A toArray() 0 11 3
A offsetGet() 0 3 1
A setProperty() 0 14 4
A __construct() 0 13 4
A offsetSet() 0 7 2
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\Jom;
4
5
use DaveRandom\Jom\Exceptions\InvalidKeyException;
6
use DaveRandom\Jom\Exceptions\InvalidSubjectNodeException;
7
use DaveRandom\Jom\Exceptions\WriteOperationForbiddenException;
8
9
final class ObjectNode extends VectorNode
10
{
11
    /**
12
     * @throws InvalidSubjectNodeException
13
     */
14 64
    public function __construct(?array $properties = [], ?Document $ownerDocument = null)
15
    {
16 64
        parent::__construct($ownerDocument);
17
18
        try {
19 64
            foreach ($properties ?? [] as $name => $node) {
20 64
                $this->setProperty((string)$name, $node);
21
            }
22
        } catch (InvalidSubjectNodeException $e) {
23
            throw $e;
24
        //@codeCoverageIgnoreStart
25
        } catch (\Exception $e) {
26
            throw unexpected($e);
27
        }
28
        //@codeCoverageIgnoreEnd
29
    }
30
31
    /**
32
     * @return string[]
33
     */
34
    public function getPropertyNames(): array
35
    {
36
        return \array_keys($this->children);
37
    }
38
39
    public function hasProperty(string $name): bool
40
    {
41
        return isset($this->children[$name]);
42
    }
43
44
    /**
45
     * @throws InvalidKeyException
46
     */
47 52
    public function getProperty(string $name): Node
48
    {
49 52
        if (!isset($this->children[$name])) {
50 2
            throw new InvalidKeyException("Property '{$name}' does not exist on the object");
51
        }
52
53 51
        return $this->children[$name];
54
    }
55
56
    /**
57
     * @throws InvalidSubjectNodeException
58
     * @throws WriteOperationForbiddenException
59
     */
60 64
    public function setProperty(string $name, Node $value): void
61
    {
62 64
        if (!isset($this->children[$name])) {
63 64
            $this->appendNode($value, $name);
64 64
            return;
65
        }
66
67
        try {
68
            $this->replaceNode($value, $this->children[$name]);
69
        } catch (WriteOperationForbiddenException | InvalidSubjectNodeException $e) {
70
            throw $e;
71
        //@codeCoverageIgnoreStart
72
        } catch (\Exception $e) {
73
            throw unexpected($e);
74
        }
75
        //@codeCoverageIgnoreEnd
76
    }
77
78
    /**
79
     * @param Node|string $nodeOrName
80
     * @throws InvalidSubjectNodeException
81
     * @throws WriteOperationForbiddenException
82
     * @throws InvalidKeyException
83
     */
84
    public function removeProperty($nodeOrName): void
85
    {
86
        $this->removeNode($this->resolveNode($nodeOrName));
87
    }
88
89
    /**
90
     * @throws InvalidKeyException
91
     */
92 49
    public function offsetGet($propertyName): Node
93
    {
94 49
        return $this->getProperty((string)$propertyName);
95
    }
96
97
    /**
98
     * @throws InvalidSubjectNodeException
99
     * @throws WriteOperationForbiddenException
100
     */
101
    public function offsetSet($propertyName, $value): void
102
    {
103
        if (!($value instanceof Node)) {
104
            throw new \TypeError('Child must be instance of ' . Node::class);
105
        }
106
107
        $this->setProperty((string)$propertyName, $value);
108
    }
109
110
    public function getValue(): \stdClass
111
    {
112
        $result = new \stdClass;
113
114
        foreach ($this as $name => $value) {
115
            $result->$name = $value->getValue();
116
        }
117
118
        return $result;
119
    }
120
121
    public function toArray(): array
122
    {
123
        $result = [];
124
125
        foreach ($this as $value) {
126
            $result[] = $value instanceof VectorNode
127
                ? $value->toArray()
128
                : $value->getValue();
129
        }
130
131
        return $result;
132
    }
133
}
134