Newer
Older
<?php
/**
* Tree data structure, used for rendering purposes.
*
* @author Taddeus Kroes
* @version 1.0
* @date 13-07-2012
*/
require_once 'base.php';
/**
* Tree node.
*
* Each tree node has a (non-unique) name, a list of variables, and zero or
* more children.
*
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
*/
class Node extends Base {
/**
* The number of Node instances, used to create unique node id's.
*
* @var int
*/
private static $count = 0;
/**
* The unique id of this Bloc.
*
* @var int
*/
private $id;
/**
* The node's name.
*
* @var string
*/
private $name;
/**
* An optional parent node.
*
* If NULL, this node is the root of the data tree.
*
* @var Node
*/
private $parent_node;
/**
* Child nodes.
*
* @var array
*/
private $children = array();
/**
* Variables in this node.
*
* All variables in a node are also available in its descendants through
* {@link get()}.
*
* @var array
*/
private $variables = array();
/**
* Constructor.
*
* The id of the node is determined by the node counter.
*
* @param string $name The node's name.
* @param Node|null &$parent_node A parent node (optional).
* @param int|null $id An id to assign. If none is specified, a new unique
* id is generated.
* @uses $count
*/
function __construct($name='', Node &$parent_node=null, $id=null) {
$this->id = $id ? $id : ++self::$count;
$this->name = $name;
$this->parent_node = $parent_node;
}
/**
* Get the node's unique id.
*
* @return int The node's id.
*/
function get_id() {
return $this->id;
}
/**
* Get the node's name.
*
* @return string The node's name.
*/
function get_name() {
return $this->name;
}
/**
* Get the node's parent.
*
* @return Node|null The parent node if any, NULL otherwise.
*/
function get_parent() {
return $this->parent_node;
}
/**
* Get the node's children.
*
* @return array A list of child nodes.
*/
function get_children() {
return $this->children;
}
/**
* Check if a node is the same instance or a copy of this node.
*
* @param Node $node The node to compare this node to.
* @return bool Whether the nodes have the same unique id.
*/
function is(Node $node) {
return $node->get_id() == $this->id;
}
/**
* Check if this node is the root node of the tree.
*
* A node is the root node if it has no parent.
*
* @return bool Whether this node is the root node.
*/
function is_root() {
return $this->parent_node === null;
}
/**
* Check if this node is a leaf node of the tree.
*
* A node is a leaf if it has no children.
*
* @return bool Whether this node is a leaf node.
*/
function is_leaf() {
return !count($this->children);
}
/**
* Add a child node.
*
* @param Node &$node The child node to add.
* @param bool $set_parent Whether to set this node as the child's parent
* (defaults to TRUE).
*/
function add_child(Node &$node, $set_parent=true) {
$this->children[] = $node;
$set_parent && $node->set_parent($this);
}
/**
* Add a child node.
*
* @param string $name The name of the node to add.
* @param array $data Data to set in the created node (optional).
* @return Node The created node.
*/
function add($name, array $data=array()) {
$node = new self($name, $this);
$this->add_child($node, false);
return $node->set($data);
}
/**
* Remove a child node.
*
* @param Node &$child The node to remove.
*/
function remove_child(Node &$child) {
foreach( $this->children as $i => $node )
$node->is($child) && array_splice($this->children, $i, 1);
}
/**
* Remove this node from its parent.
*
* @throws \RuntimeException If the node has no parent.
* @return Node This node.
*/
function remove() {
if( $this->is_root() )
throw new \RuntimeException('Cannot remove the root node of a tree.');
$this->parent_node->remove_child($this);
foreach( $this->children as $child )
$child->set_parent(null);
return $this;
}
/**
* Set the node's parent.
*
* Removes this node as child of the original parent, if a parent was
* already set.
*
* @param Node|null $parent The parent node to set.
* @return Node This node.
*/
function set_parent($parent) {
if( $this->parent_node !== null )
$this->parent_node->remove_child($this);
$this->parent_node = &$parent;
return $this;
}
/**
* Set the value of one or more variables in the node.
*
* @param string|array $name Either a single variable name, or a set of name/value pairs.
* @param mixed $value The value of a single variable to set.
* @return Node This node.
*/
function set($name, $value=null) {
if( is_array($name) ) {
foreach( $name as $var => $val )
$this->variables[$var] = $val;
} else {
$this->variables[$name] = $value;
}
return $this;
}
/**
* Get the value of a variable.
*
* @param string $name The name of the variable to get the value of.
* @return mixed The value of the variable if it exists, NULL otherwise.
*/
function get($name) {
// Variable inside this node?
if( isset($this->variables[$name]) )
return $this->variables[$name];
// Variable in one of ancestors?
if( $this->parent_node !== null )
return $this->parent_node->get($name);
// All nodes up to the tree's root node do not contain the variable
return null;
}
/**
* Set the value of a variable.
*
* This method provides a shortcut for {@link set()}.
*
* @param string $name The name of the variable to set the value of.
* @param mixed $value The value to set.
*/
function __set($name, $value) {
$this->set($name, $value);
}
/**
* Get the value of a variable.
*
* This method provides a shortcut for {@link get()}.
*
* @param string $name The name of the variable to get the value of.
* @return mixed The value of the variable if it exists, NULL otherwise.
*/
function __get($name) {
return $this->get($name);
}
/**
* Find all child nodes that have the specified name.
*
* @param string $name The name of the nodes to find.
* @return array The positively matched nodes.
*/
function find($name) {
$has_name = function($child) use ($name) {
return $child->get_name() == $name;
};
return array_values(array_filter($this->children, $has_name));
}
/**
* Create a copy of this node.
*
* The copy will have the same list of children and variables. In case of
* a 'deep copy', the list of children is also cloned recursively.
*
* @param bool $deep Whether to create a deep copy.
* @return Node A copy of this node.
*/
function copy($deep=false) {
$copy = new self($this->name, $this->parent_node, $this->id);
$copy->set($this->variables);
foreach( $this->children as $child ) {
if( $deep ) {
$child_copy = $child->copy(true);
$copy->add_child($child_copy);
} else {
$copy->add_child($child, false);
}
}
return $copy;
}
}
?>