Completed
Push — master ( 796bfc...ea86b0 )
by Hannes
01:46
created

Node::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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-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 Node[]
34
     */
35
    private $children = [];
36
37
    /**
38
     * @var int
39
     */
40
    private $lineNr = 0;
41
42
    /**
43
     * @var string
44
     */
45
    private $name;
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
        $this->name = basename(str_replace('\\', '/', get_class($this)));
57
    }
58
59
    /**
60
     * Accept a visitor
61
     */
62
    public function accept(VisitorInterface $visitor): void
63
    {
64
        $visitor->visitBefore($this);
65
66
        foreach ($this->getChildren() as $node) {
67
            $node->accept($visitor);
68
        }
69
70
        $visitor->visitAfter($this);
71
    }
72
73
    /**
74
     * Get line number this node definition started at
75
     */
76
    public function getLineNr(): int
77
    {
78
        return $this->lineNr;
79
    }
80
81
    /**
82
     * Get node name
83
     */
84
    public function getName(): string
85
    {
86
        return $this->name;
87
    }
88
89
    /**
90
     * Set a custom node name
91
     */
92
    public function setName(string $name): void
93
    {
94
        $this->name = $name;
95
    }
96
97
    /**
98
     * Get node type identifier
99
     */
100
    public function getType(): string
101
    {
102
        return basename(str_replace('\\', '/', get_class()));
103
    }
104
105
    /**
106
     * Get raw value wrapped by node
107
     *
108
     * @return mixed Loaded node value
109
     */
110
    public function getValue()
111
    {
112
        return $this->value;
113
    }
114
115
    /**
116
     * Check if this is a null object implementation
117
     */
118
    public function isNull(): bool
119
    {
120
        return false;
121
    }
122
123
    /**
124
     * Set a child node
125
     */
126
    public function addChild(Node $node): void
127
    {
128
        $this->children[] = $node;
129
    }
130
131
    /**
132
     * Get child node
133
     */
134
    public function getChild(string $name): Node
135
    {
136
        foreach ($this->children as $node) {
137
            if (strcasecmp($node->getName(), $name) == 0) {
138
                return $node;
139
            }
140
        }
141
142
        return new NullNode;
143
    }
144
145
    /**
146
     * Check if child exists
147
     */
148
    public function hasChild(string $name): bool
149
    {
150
        foreach ($this->children as $node) {
151
            if (strcasecmp($node->getName(), $name) == 0) {
152
                return true;
153
            }
154
        }
155
156
        return false;
157
    }
158
159
    /**
160
     * Get registered child nodes
161
     *
162
     * @return Node[]
163
     */
164
    public function getChildren(string $name = ''): array
165
    {
166
        $nodes = [];
167
168
        foreach ($this->children as $node) {
169
            if (!$name || strcasecmp($node->getName(), $name) == 0) {
170
                $nodes[] = $node;
171
            }
172
        }
173
174
        return $nodes;
175
    }
176
177
    /**
178
     * Get raw value wrapped in child node
179
     *
180
     * @return mixed Loaded node value
181
     */
182
    public function getValueFrom(string $name)
183
    {
184
        return $this->getChild($name)->getValue();
185
    }
186
}
187