Completed
Push — master ( 2917fa...796bfc )
by Hannes
02:44
created

Node::getValueFrom()   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 1
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
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 string
49
     */
50
    private $name;
51
52
    /**
53
     * @var mixed
54
     */
55
    private $value = '';
56
57
    public function __construct(int $lineNr = 0, $value = '')
58
    {
59
        $this->lineNr = $lineNr;
60
        $this->value = $value;
61
        $this->name = basename(str_replace('\\', '/', get_class($this)));
62
    }
63
64
    /**
65
     * Accept a visitor
66
     */
67
    public function accept(VisitorInterface $visitor): void
68
    {
69
        $visitor->visitBefore($this);
70
71
        foreach ($this->getChildren() as $node) {
72
            $node->accept($visitor);
73
        }
74
75
        $visitor->visitAfter($this);
76
    }
77
78
    /**
79
     * Get line number this node definition started at
80
     */
81
    public function getLineNr(): int
82
    {
83
        return $this->lineNr;
84
    }
85
86
    /**
87
     * Get node name
88
     */
89
    public function getName(): string
90
    {
91
        return $this->name;
92
    }
93
94
    /**
95
     * Set a custom node name
96
     */
97
    public function setName(string $name): void
98
    {
99
        $this->name = $name;
100
    }
101
102
    /**
103
     * Get node type identifier
104
     */
105
    public function getType(): string
106
    {
107
        return basename(str_replace('\\', '/', get_class()));
108
    }
109
110
    /**
111
     * Get raw value wrapped by node
112
     *
113
     * @return mixed Loaded node value
114
     */
115
    public function getValue()
116
    {
117
        return $this->value;
118
    }
119
120
    /**
121
     * Check if this is a null object implementation
122
     */
123
    public function isNull(): bool
124
    {
125
        return false;
126
    }
127
128
    /**
129
     * Set a child node
130
     */
131
    public function addChild(Node $node): void
132
    {
133
        $this->children[] = $node;
134
    }
135
136
    /**
137
     * Get child node
138
     */
139
    public function getChild(string $name): Node
140
    {
141
        foreach ($this->children as $node) {
142
            if (strcasecmp($node->getName(), $name) == 0) {
143
                return $node;
144
            }
145
        }
146
147
        return new NullNode;
148
    }
149
150
    /**
151
     * Check if child exists
152
     */
153
    public function hasChild(string $name): bool
154
    {
155
        foreach ($this->children as $node) {
156
            if (strcasecmp($node->getName(), $name) == 0) {
157
                return true;
158
            }
159
        }
160
161
        return false;
162
    }
163
164
    /**
165
     * Get registered child nodes
166
     *
167
     * @return Node[]
168
     */
169
    public function getChildren(string $name = ''): array
170
    {
171
        $nodes = [];
172
173
        foreach ($this->children as $node) {
174
            if (!$name || strcasecmp($node->getName(), $name) == 0) {
175
                $nodes[] = $node;
176
            }
177
        }
178
179
        return $nodes;
180
    }
181
182
    /**
183
     * Get raw value wrapped in child node
184
     *
185
     * @return mixed Loaded node value
186
     */
187
    public function getValueFrom(string $name)
188
    {
189
        return $this->getChild($name)->getValue();
190
    }
191
192
    /**
193
     * Set a custom attribute on node
194
     *
195
     * @param string $name  Name of attribute
196
     * @param mixed  $value Value of attribute
197
     */
198
    public function setAttribute(string $name, $value): void
199
    {
200
        $this->attributes[$name] = $value;
201
    }
202
203
    /**
204
     * Get custom attribute
205
     *
206
     * @param  string $name Name of attribute
207
     * @return mixed  Value of attribute
208
     */
209
    public function getAttribute(string $name)
210
    {
211
        return $this->attributes[$name] ?? null;
212
    }
213
214
    /**
215
     * Check if attribute has been set
216
     */
217
    public function hasAttribute(string $name): bool
218
    {
219
        return isset($this->attributes[$name]);
220
    }
221
222
    /**
223
     * Get all registered attributes
224
     */
225
    public function getAttributes(): array
226
    {
227
        return $this->attributes;
228
    }
229
}
230