Node::getChildIndexList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\UniLex\AST;
6
7
use Remorhaz\UniLex\Exception as UniLexException;
8
use Remorhaz\UniLex\Stack\StackableSymbolInterface;
9
10
use function chr;
11
use function is_array;
12
use function is_int;
13
14
class Node implements StackableSymbolInterface
15
{
16
    private $id;
17
18
    private $name;
19
20
    private $attributeMap = [];
21
22
    private $childMap = [];
23
24
    public function __construct(int $id, string $name)
25
    {
26
        $this->id = $id;
27
        $this->name = $name;
28
    }
29
30
    public function getId(): int
31
    {
32
        return $this->id;
33
    }
34
35
    public function getName(): string
36
    {
37
        return $this->name;
38
    }
39
40
    public function getClone(): self
41
    {
42
        $clone = clone $this;
43
        $clone->childMap = [];
44
        foreach ($this->getChildList() as $childNode) {
45
            $clone->addChild($childNode->getClone());
46
        }
47
48
        return $clone;
49
    }
50
51
    /**
52
     * @param string $name
53
     * @param        $value
54
     * @return Node
55
     * @throws UniLexException
56
     */
57
    public function setAttribute(string $name, $value): self
58
    {
59
        if (isset($this->attributeMap[$name])) {
60
            throw new UniLexException("Attribute '{$name}' is already defined in syntax tree node {$this->getId()}");
61
        }
62
        $this->attributeMap[$name] = $value;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param string $name
69
     * @return mixed
70
     * @throws UniLexException
71
     */
72
    public function getAttribute(string $name)
73
    {
74
        if (!isset($this->attributeMap[$name])) {
75
            throw new UniLexException("Attribute '{$name}' is not defined in syntax tree node {$this->getId()}");
76
        }
77
78
        return $this->attributeMap[$name];
79
    }
80
81
    /**
82
     * @param string $name
83
     * @return int
84
     * @throws UniLexException
85
     */
86
    public function getIntAttribute(string $name): int
87
    {
88
        $attribute = $this->getAttribute($name);
89
        if (is_int($attribute)) {
90
            return $attribute;
91
        }
92
93
        throw new Exception\InvalidAttributeException($name, $attribute, 'integer');
94
    }
95
96
    /**
97
     * @param string $name
98
     * @return string
99
     * @throws UniLexException
100
     */
101
    public function getStringAttribute(string $name): string
102
    {
103
        $attribute = $this->getAttribute($name);
104
        if (is_array($attribute)) {
105
            $string = '';
106
            foreach ($attribute as $symbol) {
107
                if (!is_int($symbol) || $symbol > 0xFF) {
108
                    throw new Exception\InvalidAttributeException($name, $attribute, 'integer[]');
109
                }
110
111
                $string .= chr($symbol);
112
            }
113
114
            return $string;
115
        }
116
117
        throw new Exception\InvalidAttributeException($name, $attribute, 'integer[]');
118
    }
119
120
    public function addChild(Node $node): self
121
    {
122
        $this->childMap[] = $node;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param int $index
129
     * @return Node
130
     * @throws UniLexException
131
     */
132
    public function getChild(int $index): Node
133
    {
134
        if (!isset($this->childMap[$index])) {
135
            throw new UniLexException("Child node at index {$index} in node {$this->getId()} is not defined");
136
        }
137
138
        return $this->childMap[$index];
139
    }
140
141
    /**
142
     * @return Node[]
143
     */
144
    public function getChildList(): array
145
    {
146
        return $this->childMap;
147
    }
148
149
    /**
150
     * @return int[]
151
     */
152
    public function getChildIndexList(): array
153
    {
154
        return array_keys($this->childMap);
155
    }
156
157
    public function getAttributeList(): array
158
    {
159
        return $this->attributeMap;
160
    }
161
}
162