Completed
Push — master ( 69a9cb...2b24be )
by Hannes
01:54
created

Node::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 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-18 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Tree;
24
25
use byrokrat\autogiro\Visitor\VisitorInterface;
26
27
/**
28
 * Defines a node in the parse tree
29
 */
30
abstract class Node
31
{
32
    /**
33
     * @var array
34
     */
35
    private $attributes = [];
36
37
    /**
38
     * @var Node[]
39
     */
40
    private $children = [];
41
42
    /**
43
     * @var int
44
     */
45
    private $lineNr = 0;
46
47
    /**
48
     * @var mixed
49
     */
50
    private $value = '';
51
52
    public function __construct(int $lineNr = 0, $value = '')
53
    {
54
        $this->lineNr = $lineNr;
55
        $this->value = $value;
56
    }
57
58
    /**
59
     * Accept a visitor
60
     */
61
    public function accept(VisitorInterface $visitor): void
62
    {
63
        $visitor->visitBefore($this);
64
65
        foreach ($this->getChildren() as $node) {
66
            $node->accept($visitor);
67
        }
68
69
        $visitor->visitAfter($this);
70
    }
71
72
    /**
73
     * Get line number this node definition started at
74
     */
75
    public function getLineNr(): int
76
    {
77
        return $this->lineNr;
78
    }
79
80
    /**
81
     * Get node name
82
     */
83
    public function getName(): string
84
    {
85
        return basename(str_replace('\\', '/', get_class($this)));
86
    }
87
88
    /**
89
     * Get node type identifier
90
     */
91
    abstract public function getType(): string;
92
93
    /**
94
     * Get raw value wrapped by node
95
     *
96
     * @return mixed Loaded node value
97
     */
98
    public function getValue()
99
    {
100
        return $this->value;
101
    }
102
103
    /**
104
     * Set a custom attribute on node
105
     *
106
     * @param string $name  Name of attribute
107
     * @param mixed  $value Value of attribute
108
     */
109
    public function setAttribute(string $name, $value): void
110
    {
111
        $this->attributes[$name] = $value;
112
    }
113
114
    /**
115
     * Get custom attribute
116
     *
117
     * @param  string $name Name of attribute
118
     * @return mixed  Value of attribute
119
     */
120
    public function getAttribute(string $name)
121
    {
122
        return $this->attributes[$name] ?? null;
123
    }
124
125
    /**
126
     * Check if attribute has been set
127
     */
128
    public function hasAttribute(string $name): bool
129
    {
130
        return isset($this->attributes[$name]);
131
    }
132
133
    /**
134
     * Get all registered attributes
135
     */
136
    public function getAttributes(): array
137
    {
138
        return $this->attributes;
139
    }
140
141
    /**
142
     * Set a child node
143
     */
144
    public function addChild(string $name, Node $child): void
145
    {
146
        $this->children[$name] = $child;
147
    }
148
149
    /**
150
     * Get child node
151
     */
152
    public function getChild(string $name): ?Node
153
    {
154
        if (!$this->hasChild($name)) {
155
            return null;
156
        }
157
158
        return $this->children[$name];
159
    }
160
161
    /**
162
     * Check if child exists
163
     */
164
    public function hasChild(string $name): bool
165
    {
166
        return isset($this->children[$name]);
167
    }
168
169
    /**
170
     * Get registered child nodes
171
     *
172
     * @return Node[]
173
     */
174
    public function getChildren(): array
175
    {
176
        return $this->children;
177
    }
178
}
179