|
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 array[] |
|
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
|
|
|
* Check if this is a null object implementation |
|
105
|
|
|
*/ |
|
106
|
|
|
public function isNull(): bool |
|
107
|
|
|
{ |
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Set a custom attribute on node |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $name Name of attribute |
|
115
|
|
|
* @param mixed $value Value of attribute |
|
116
|
|
|
*/ |
|
117
|
|
|
public function setAttribute(string $name, $value): void |
|
118
|
|
|
{ |
|
119
|
|
|
$this->attributes[$name] = $value; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get custom attribute |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $name Name of attribute |
|
126
|
|
|
* @return mixed Value of attribute |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getAttribute(string $name) |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->attributes[$name] ?? null; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Check if attribute has been set |
|
135
|
|
|
*/ |
|
136
|
|
|
public function hasAttribute(string $name): bool |
|
137
|
|
|
{ |
|
138
|
|
|
return isset($this->attributes[$name]); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get all registered attributes |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getAttributes(): array |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->attributes; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Set a child node |
|
151
|
|
|
*/ |
|
152
|
|
|
public function addChild(string $name, Node $node): void |
|
153
|
|
|
{ |
|
154
|
|
|
$this->children[] = [$name, $node]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Get child node |
|
159
|
|
|
*/ |
|
160
|
|
View Code Duplication |
public function getChild(string $name): Node |
|
|
|
|
|
|
161
|
|
|
{ |
|
162
|
|
|
foreach ($this->children as list($nodeName, $node)) { |
|
163
|
|
|
if (strcasecmp($nodeName, $name) == 0) { |
|
164
|
|
|
return $node; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return new NullNode; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Check if child exists |
|
173
|
|
|
*/ |
|
174
|
|
View Code Duplication |
public function hasChild(string $name): bool |
|
|
|
|
|
|
175
|
|
|
{ |
|
176
|
|
|
foreach ($this->children as list($nodeName, $node)) { |
|
177
|
|
|
if (strcasecmp($nodeName, $name) == 0) { |
|
178
|
|
|
return true; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get registered child nodes |
|
187
|
|
|
* |
|
188
|
|
|
* @return iterable & Node[] |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getChildren(string $name = ''): iterable |
|
191
|
|
|
{ |
|
192
|
|
|
foreach ($this->children as list($nodeName, $node)) { |
|
193
|
|
|
if (!$name || strcasecmp($nodeName, $name) == 0) { |
|
194
|
|
|
yield $node; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.