1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Pluswerk\TypoScriptAutoFixer\Fixer\NestingConsistency; |
5
|
|
|
|
6
|
|
|
use Pluswerk\TypoScriptAutoFixer\Adapter\Configuration\Configuration; |
7
|
|
|
use Pluswerk\TypoScriptAutoFixer\Exception\NodeTitleMustNotBeEmptyException; |
8
|
|
|
|
9
|
|
|
final class Node |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $identifier; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var NodeCollection |
18
|
|
|
*/ |
19
|
|
|
private $children; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $value; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Operator |
28
|
|
|
*/ |
29
|
|
|
private $operator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
private $level; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Node constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $identifier |
40
|
|
|
* @param string $value |
41
|
|
|
* @param Operator $operator |
42
|
|
|
* @param int $level |
43
|
|
|
*/ |
44
|
|
|
public function __construct(string $identifier, string $value = '', Operator $operator = null, int $level = 0) |
45
|
|
|
{ |
46
|
|
|
if ($identifier === '') { |
47
|
|
|
throw new NodeTitleMustNotBeEmptyException('Node title must not be empty!'); |
48
|
|
|
} |
49
|
|
|
$this->identifier = $identifier; |
50
|
|
|
|
51
|
|
|
if ($value !== '') { |
52
|
|
|
$this->value = $value; |
53
|
|
|
} |
54
|
|
|
if ($operator !== null) { |
55
|
|
|
$this->operator = $operator; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->level = $level; |
59
|
|
|
|
60
|
|
|
$this->children = new NodeCollection(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Node $childNode |
65
|
|
|
*/ |
66
|
|
|
public function addChildNode(Node $childNode): void |
67
|
|
|
{ |
68
|
|
|
$childNode->updateLevel($this->level + 1); |
69
|
|
|
$this->children->add($childNode); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return NodeCollection |
74
|
|
|
*/ |
75
|
|
|
public function children(): NodeCollection |
76
|
|
|
{ |
77
|
|
|
return $this->children; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function identifier(): string |
84
|
|
|
{ |
85
|
|
|
return $this->identifier; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function hasChildren(): bool |
92
|
|
|
{ |
93
|
|
|
return $this->children->count() > 0; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string|null |
98
|
|
|
*/ |
99
|
|
|
public function value(): ?string |
100
|
|
|
{ |
101
|
|
|
return $this->value; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function hasValue(): bool |
108
|
|
|
{ |
109
|
|
|
if (is_string($this->value) && $this->value !== '') { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return Operator|null |
117
|
|
|
*/ |
118
|
|
|
public function operator(): ?Operator |
119
|
|
|
{ |
120
|
|
|
return $this->operator; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param NodeCollection $collection |
125
|
|
|
*/ |
126
|
|
|
public function updateCollection(NodeCollection $collection): void |
127
|
|
|
{ |
128
|
|
|
$this->children = $collection; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return int |
133
|
|
|
*/ |
134
|
|
|
public function level(): int |
135
|
|
|
{ |
136
|
|
|
return $this->level; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param int $level |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function updateLevel(int $level): void |
145
|
|
|
{ |
146
|
|
|
$this->level = $level; |
147
|
|
|
|
148
|
|
|
$subLevel = $level + 1; |
149
|
|
|
foreach ($this->children as $child) { |
150
|
|
|
$child->updateLevel($subLevel); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function __toString(): string |
158
|
|
|
{ |
159
|
|
|
$configuration = Configuration::getInstance(); |
160
|
|
|
$indentation = str_repeat($configuration->oneLevelIndentationString(), $this->level); |
161
|
|
|
$string = ''; |
162
|
|
|
|
163
|
|
|
if ($this->operator instanceof Operator) { |
|
|
|
|
164
|
|
|
if ($this->operator->isMultiLine()) { |
165
|
|
|
$string .= $indentation . $this->identifier . ' (' . PHP_EOL . $this->value . PHP_EOL . ')' . PHP_EOL; |
166
|
|
|
} else { |
167
|
|
|
$string .= $indentation . trim($this->identifier . ' ' . $this->operator . ' ' . $this->value) . PHP_EOL; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($this->children->count() > 0) { |
172
|
|
|
$string .= $indentation . $this->identifier . ' {' . PHP_EOL; |
173
|
|
|
$string .= $this->children; |
174
|
|
|
$string .= $indentation . '}' . PHP_EOL; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $string; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|