Node.php
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Menu;
defined('JPATH_PLATFORM') or die;
use Joomla\Registry\Registry;
/**
* A Node for MenuTree
*
* @see Tree
*
* @since 3.8.0
*/
class Node
{
/**
* Node Id
*
* @var string
*
* @since 3.8.0
*/
protected $id = null;
/**
* CSS Class for node
*
* @var string
*
* @since 3.8.0
*/
protected $class = null;
/**
* Whether this node is active
*
* @var bool
*
* @since 3.8.0
*/
protected $active = false;
/**
* Additional custom node params
*
* @var Registry
*
* @since 3.8.0
*/
protected $params;
/**
* Parent node object
*
* @var Node
*
* @since 3.8.0
*/
protected $parent = null;
/**
* Array of Children node objects
*
* @var Node[]
*
* @since 3.8.0
*/
protected $children = array();
/**
* Constructor
*
* @since 3.8.0
*/
public function __construct()
{
$this->params = new Registry;
}
/**
* Add child to this node
*
* If the child already has a parent, the link is unset
*
* @param Node $child The child to be added
*
* @return Node The new added child
*
* @since 3.8.0
*/
public function addChild(Node $child)
{
$hash = spl_object_hash($child);
if (isset($child->parent))
{
$child->parent->removeChild($child);
}
$child->parent = $this;
$this->children[$hash] = $child;
return $child;
}
/**
* Remove a child from this node
*
* If the child exists it is unset
*
* @param Node $child The child to be added
*
* @return void
*
* @since 3.8.0
*/
public function removeChild(Node $child)
{
$hash = spl_object_hash($child);
if (isset($this->children[$hash]))
{
$child->parent = null;
unset($this->children[$hash]);
}
}
/**
* Test if this node has a parent
*
* @return boolean True if there is a parent
*
* @since 3.8.0
*/
public function hasParent()
{
return isset($this->parent);
}
/**
* Get the parent of this node
*
* @return Node The Node object's parent or null for no parent
*
* @since 3.8.0
*/
public function getParent()
{
return $this->parent;
}
/**
* Test if this node has children
*
* @return boolean
*
* @since 3.8.0
*/
public function hasChildren()
{
return count($this->children) > 0;
}
/**
* Get the children of this node
*
* @return Node[] The children
*
* @since 3.8.0
*/
public function getChildren()
{
return $this->children;
}
/**
* Find the current node depth in the tree hierarchy
*
* @return integer The node level in the hierarchy, where ROOT == 0, First level menu item == 1, and so on.
*
* @since 3.8.0
*/
public function getLevel()
{
return $this->hasParent() ? $this->getParent()->getLevel() + 1 : 0;
}
/**
* Check whether the object instance node is the root node
*
* @return boolean
*
* @since 3.8.0
*/
public function isRoot()
{
return !$this->hasParent();
}
/**
* Set the active state on or off
*
* @param bool $active The new active state
*
* @return void
*
* @since 3.8.0
*/
public function setActive($active)
{
$this->active = (bool) $active;
}
/**
* set the params array
*
* @param Registry $params The params attributes
*
* @return void
*
* @since 3.8.0
*/
public function setParams(Registry $params)
{
$this->params = $params;
}
/**
* Get the param value from the node params
*
* @param string $key The param name
*
* @return mixed
*
* @since 3.8.0
*/
public function getParam($key)
{
return isset($this->params[$key]) ? $this->params[$key] : null;
}
/**
* Get an attribute value
*
* @param string $name The attribute name
*
* @return mixed
*
* @since 3.8.0
*/
public function get($name)
{
switch ($name)
{
case 'id':
case 'class':
case 'active':
case 'params':
return $this->$name;
}
return null;
}
}