1
|
|
|
<?php namespace Mbh\Tree\Traits; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MBHFramework |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/MBHFramework/mbh-framework |
7
|
|
|
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
8
|
|
|
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use Mbh\Tree\Interfaces\Node as NodeInterface; |
12
|
|
|
use Mbh\Tree\Interfaces\Visitor as VisitorInterface; |
13
|
|
|
|
14
|
|
|
trait Node |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var mixed |
18
|
|
|
*/ |
19
|
|
|
private $value; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* parent |
23
|
|
|
* |
24
|
|
|
* @var NodeInterface |
25
|
|
|
* @access private |
26
|
|
|
*/ |
27
|
|
|
private $parent; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var NodeInterface[] |
31
|
|
|
*/ |
32
|
|
|
private $children = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param mixed $value |
36
|
|
|
* @param NodeInterface[] $children |
37
|
|
|
*/ |
38
|
|
|
public function __construct($value = null, array $children = []) |
39
|
|
|
{ |
40
|
|
|
$this->setValue($value); |
41
|
|
|
if (!empty($children)) { |
42
|
|
|
$this->setChildren($children); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function setValue($value) |
50
|
|
|
{ |
51
|
|
|
$this->value = $value; |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function getValue() |
59
|
|
|
{ |
60
|
|
|
return $this->value; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function addChild(NodeInterface $child) |
67
|
|
|
{ |
68
|
|
|
$child->setParent($this); |
|
|
|
|
69
|
|
|
$this->children[] = $child; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function removeChild(NodeInterface $child) |
78
|
|
|
{ |
79
|
|
|
foreach ($this->children as $key => $myChild) { |
80
|
|
|
if ($child == $myChild) { |
81
|
|
|
unset($this->children[$key]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->children = array_values($this->children); |
86
|
|
|
$child->setParent(null); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function removeAllChildren() |
95
|
|
|
{ |
96
|
|
|
$this->setChildren([]); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function getChildren() |
104
|
|
|
{ |
105
|
|
|
return $this->children; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function setChildren(array $children) |
112
|
|
|
{ |
113
|
|
|
$this->removeParentFromChildren(); |
114
|
|
|
$this->children = []; |
115
|
|
|
|
116
|
|
|
foreach ($children as $child) { |
117
|
|
|
$this->addChild($child); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function setParent(NodeInterface $parent = null) |
127
|
|
|
{ |
128
|
|
|
$this->parent = $parent; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function getParent() |
135
|
|
|
{ |
136
|
|
|
return $this->parent; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function getAncestors() |
143
|
|
|
{ |
144
|
|
|
$parents = []; |
145
|
|
|
$node = $this; |
146
|
|
|
|
147
|
|
|
while ($parent = $node->getParent()) { |
148
|
|
|
array_unshift($parents, $parent); |
149
|
|
|
$node = $parent; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $parents; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritDoc} |
157
|
|
|
*/ |
158
|
|
|
public function getAncestorsAndSelf() |
159
|
|
|
{ |
160
|
|
|
return array_merge($this->getAncestors(), [$this]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
|
|
public function getNeighbors() |
167
|
|
|
{ |
168
|
|
|
$neighbors = $this->getParent()->getChildren(); |
169
|
|
|
$current = $this; |
170
|
|
|
|
171
|
|
|
// Uses array_values to reset indexes after filter. |
172
|
|
|
return array_values( |
173
|
|
|
array_filter( |
174
|
|
|
$neighbors, |
175
|
|
|
function($item) use ($current) { |
176
|
|
|
return $item != $current; |
177
|
|
|
} |
178
|
|
|
) |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritDoc} |
184
|
|
|
*/ |
185
|
|
|
public function getNeighborsAndSelf() |
186
|
|
|
{ |
187
|
|
|
return $this->getParent()->getChildren(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritDoc} |
192
|
|
|
*/ |
193
|
|
|
public function isLeaf() |
194
|
|
|
{ |
195
|
|
|
return count($this->children) === 0; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
public function isRoot() |
202
|
|
|
{ |
203
|
|
|
return $this->getParent() === null; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritDoc} |
208
|
|
|
*/ |
209
|
|
|
public function isChild() |
210
|
|
|
{ |
211
|
|
|
return $this->getParent() !== null; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Find the root of the node |
216
|
|
|
* |
217
|
|
|
* @return NodeInterface |
218
|
|
|
*/ |
219
|
|
|
public function root() |
220
|
|
|
{ |
221
|
|
|
$node = $this; |
222
|
|
|
|
223
|
|
|
while ($parent = $node->getParent()) { |
224
|
|
|
$node = $parent; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $node; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Return the distance from the current node to the root. |
232
|
|
|
* |
233
|
|
|
* Warning, can be expensive, since each descendant is visited |
234
|
|
|
* |
235
|
|
|
* @return int |
236
|
|
|
*/ |
237
|
|
|
public function getDepth() |
238
|
|
|
{ |
239
|
|
|
if ($this->isRoot()) { |
240
|
|
|
return 0; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $this->getParent()->getDepth() + 1; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Return the height of the tree whose root is this node |
248
|
|
|
* |
249
|
|
|
* @return int |
250
|
|
|
*/ |
251
|
|
|
public function getHeight() |
252
|
|
|
{ |
253
|
|
|
if ($this->isLeaf()) { |
254
|
|
|
return 0; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$heights = []; |
258
|
|
|
|
259
|
|
|
foreach ($this->getChildren() as $child) { |
260
|
|
|
$heights[] = $child->getHeight(); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return max($heights) + 1; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
|
|
public function getSize() |
270
|
|
|
{ |
271
|
|
|
$size = 1; |
272
|
|
|
foreach ($this->getChildren() as $child) { |
273
|
|
|
$size += $child->getSize(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $size; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* {@inheritdoc} |
281
|
|
|
*/ |
282
|
|
|
public function accept(VisitorInterface $visitor) |
283
|
|
|
{ |
284
|
|
|
return $visitor->visit($this); |
|
|
|
|
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
private function removeParentFromChildren() |
288
|
|
|
{ |
289
|
|
|
foreach ($this->getChildren() as $child) { |
290
|
|
|
$child->setParent(null); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|