Completed
Push — master ( e6fe6f...a29d2d )
by Hannes
09:33
created

Node::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of byrokrat\autogiro.
4
 *
5
 * byrokrat\autogiro is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\autogiro is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Tree;
24
25
use byrokrat\autogiro\Visitor;
26
use byrokrat\autogiro\Exception\LogicException;
27
28
/**
29
 * Defines a node in the parse tree
30
 */
31
class Node
32
{
33
    /**
34
     * @var array
35
     */
36
    private $attributes = [];
37
38
    /**
39
     * @var Node
40
     */
41
    private $children = [];
42
43
    /**
44
     * @var int
45
     */
46
    private $lineNr = 0;
47
48
    /**
49
     * @var string
50
     */
51
    private $value = '';
52
53
    public function __construct(int $lineNr = 0, string $value = '')
54
    {
55
        $this->lineNr = $lineNr;
56
        $this->value = $value;
57
    }
58
59
    /**
60
     * Accept a visitor
61
     */
62
    public function accept(Visitor $visitor)
63
    {
64
        $visitor->visit($this);
65
66
        foreach ($this->getChildren() as $node) {
67
            $node->accept($visitor);
68
        }
69
    }
70
71
    /**
72
     * Get line number this node definition started at
73
     */
74
    public function getLineNr(): int
75
    {
76
        return $this->lineNr;
77
    }
78
79
    /**
80
     * Get node type identifier
81
     */
82
    public function getType(): string
83
    {
84
        return basename(str_replace('\\', '/', get_class($this)));
85
    }
86
87
    /**
88
     * Get raw value wrapped by node
89
     */
90
    public function getValue(): string
91
    {
92
        return $this->value;
93
    }
94
95
    /**
96
     * Set a custom attribute on node
97
     *
98
     * @param string $name  Name of attribute
99
     * @param mixed  $value Value of attribute
100
     */
101
    public function setAttribute(string $name, $value)
102
    {
103
        $this->attributes[$name] = $value;
104
    }
105
106
    /**
107
     * Get custom attribute
108
     *
109
     * @param  string $name Name of attribute
110
     * @return mixed  Value of attribute
111
     */
112
    public function getAttribute(string $name)
113
    {
114
        return $this->attributes[$name] ?? null;
115
    }
116
117
    /**
118
     * Check if attribute has been set
119
     */
120
    public function hasAttribute(string $name): bool
121
    {
122
        return isset($this->attributes[$name]);
123
    }
124
125
    /**
126
     * Get all registered attributes
127
     */
128
    public function getAttributes(): array
129
    {
130
        return $this->attributes;
131
    }
132
133
    /**
134
     * Set a child node
135
     */
136
    public function setChild(string $name, Node $child)
137
    {
138
        $this->children[$name] = $child;
139
    }
140
141
    /**
142
     * Get child node
143
     *
144
     * @throws LogicException If child does not exist
145
     */
146
    public function getChild(string $name): Node
147
    {
148
        if (!$this->hasChild($name)) {
149
            throw new LogicException("Trying to read unknown child $name");
150
        }
151
152
        return $this->children[$name];
153
    }
154
155
    /**
156
     * Check if child exists
157
     */
158
    public function hasChild(string $name): bool
159
    {
160
        return isset($this->children[$name]);
161
    }
162
163
    /**
164
     * Get registered child nodes
165
     *
166
     * @return Node[]
167
     */
168
    public function getChildren(): array
169
    {
170
        return $this->children;
171
    }
172
}
173