1
|
|
|
<?php |
2
|
|
|
namespace Jmw\Collection\Tree; |
3
|
|
|
|
4
|
|
|
use Jmw\Collection\Lists\ListInterface; |
5
|
|
|
use Jmw\Collection\Lists\ArrayList; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Simple implementation of a tree node. |
9
|
|
|
* Allows for variable |
10
|
|
|
* @author john |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class SimpleTreeNode implements TreeNodeInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var multitype |
17
|
|
|
*/ |
18
|
|
|
protected $value; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var SimpleTreeNode |
22
|
|
|
*/ |
23
|
|
|
protected $parent; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ListInterface |
27
|
|
|
*/ |
28
|
|
|
protected $children; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructs a new SimpleTreeNode |
32
|
|
|
* @param multitype $value |
33
|
|
|
* @param SimpleTreeNode $parent |
34
|
|
|
* @param ListInterface $children |
35
|
|
|
*/ |
36
|
|
|
public function __construct($value, SimpleTreeNode $parent = null, ListInterface $children = null) |
37
|
|
|
{ |
38
|
|
|
$this->value = $value; |
39
|
|
|
|
40
|
|
|
$this->parent = $parent; |
41
|
|
|
|
42
|
|
|
if($children === null) |
43
|
|
|
{ |
44
|
|
|
$children = new ArrayList(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->children = $children; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns the children of the receiver as an Enumeration. |
52
|
|
|
* @return ListInterface |
53
|
|
|
*/ |
54
|
|
|
public function children() |
55
|
|
|
{ |
56
|
|
|
return $this->children; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns true if the receiver allows children. |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
|
|
public function getAllowsChildren() |
64
|
|
|
{ |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the child TreeNode at index childIndex. |
70
|
|
|
* @param int $index |
71
|
|
|
* @return TreeNodeInterface |
72
|
|
|
*/ |
73
|
|
|
public function getChildAt($index) |
74
|
|
|
{ |
75
|
|
|
return $this->children->get($index); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the number of children TreeNodes the receiver contains. |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
|
|
public function getChildCount() |
83
|
|
|
{ |
84
|
|
|
return $this->children->size(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the index of node in the receivers children. |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
public function getIndex(TreeNodeInterface $node) |
92
|
|
|
{ |
93
|
|
|
return $this->children->indexOf($node); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the parent TreeNode of the receiver. |
98
|
|
|
* @return TreeNodeInterface |
99
|
|
|
*/ |
100
|
|
|
public function getParent() |
101
|
|
|
{ |
102
|
|
|
return $this->parent; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retrieves the value of this TreeNode |
107
|
|
|
* @return multitype |
108
|
|
|
*/ |
109
|
|
|
public function value() |
110
|
|
|
{ |
111
|
|
|
return $this->value; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Injects a node into the current tree structure, |
116
|
|
|
* such that the node at the specified index becomes |
117
|
|
|
* a child of the injected node, and the injected node |
118
|
|
|
* moves to the specified index in the receiver |
119
|
|
|
* @param int $index |
120
|
|
|
* @param TreeNodeInterface $node |
121
|
|
|
*/ |
122
|
|
|
public function injectAt($index, TreeNodeInterface $node) |
123
|
|
|
{ |
124
|
|
|
$node->insert($this->children->get($index)); |
125
|
|
|
$this->replaceAt($index, $node); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Adds child to the receiver at index. |
130
|
|
|
* @param TreeNodeInterface $nodex |
|
|
|
|
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
public function insert(TreeNodeInterface $node) |
134
|
|
|
{ |
135
|
|
|
$node->setParent($this); |
136
|
|
|
$this->children->add($node); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Adds child to the receiver at index. |
141
|
|
|
* @param int $index |
142
|
|
|
* @param TreeNodeInterface $node |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
public function insertAt($index, TreeNodeInterface $node) |
146
|
|
|
{ |
147
|
|
|
$node->setParent($this); |
148
|
|
|
$this->children->addAt($index, $node); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns true if the receiver is a leaf. |
153
|
|
|
* @return boolean |
154
|
|
|
*/ |
155
|
|
|
public function isLeaf() |
156
|
|
|
{ |
157
|
|
|
return $this->children->isEmpty(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Removes node from the receiver. |
162
|
|
|
* @param TreeNodeInterface $node |
163
|
|
|
*/ |
164
|
|
|
public function remove(TreeNodeInterface $node) |
165
|
|
|
{ |
166
|
|
|
$this->children->remove($node); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Removes the child at index from the receiver. |
171
|
|
|
* @param int $index |
172
|
|
|
*/ |
173
|
|
|
public function removeAt($index) |
174
|
|
|
{ |
175
|
|
|
$this->children->removeAt($index); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Removes the receiver from its parent. |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
|
|
public function removeFromParent() |
183
|
|
|
{ |
184
|
|
|
if($this->parent !== null) |
185
|
|
|
{ |
186
|
|
|
$this->parent->children()->remove($this); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Replace the node at the specified index in the |
192
|
|
|
* receiver with the node argument |
193
|
|
|
* @param int $index |
194
|
|
|
* @param TreeNodeInterface $node |
195
|
|
|
*/ |
196
|
|
|
public function replaceAt($index, TreeNodeInterface $node) |
197
|
|
|
{ |
198
|
|
|
$node->setParent($this); |
199
|
|
|
$this->children->removeAt($index); |
200
|
|
|
$this->children->addAt($index, $node); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Sets the parent of the receiver to newParent. |
205
|
|
|
* @param TreeNodeInterface $node |
206
|
|
|
*/ |
207
|
|
|
public function setParent(TreeNodeInterface $node) |
208
|
|
|
{ |
209
|
|
|
$this->parent = $node; |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Sets the receiver's value |
214
|
|
|
* @param multitype $value |
215
|
|
|
*/ |
216
|
|
|
public function setValue($value) |
217
|
|
|
{ |
218
|
|
|
$this->value = $value; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Outputs to stdout a simplistic representation of this node and |
223
|
|
|
* it's children |
224
|
|
|
* @param number $level |
225
|
|
|
*/ |
226
|
|
|
public function toString($level = 0) |
227
|
|
|
{ |
228
|
|
|
$output = ""; |
229
|
|
|
for($i = 0; $i < $level; $i++) |
230
|
|
|
{ |
231
|
|
|
$output .= "\t"; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$output .= print_r($this->value(), true); |
235
|
|
|
|
236
|
|
|
$output .= "\n"; |
237
|
|
|
|
238
|
|
|
$iterator = $this->children->iterator(); |
239
|
|
|
|
240
|
|
|
while($iterator->hasNext()) |
241
|
|
|
{ |
242
|
|
|
$output .= $iterator->next()->toString($level + 1); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $output; |
246
|
|
|
} |
247
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: