1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Ariadne Component Library. |
4
|
|
|
* |
5
|
|
|
* (c) Muze <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace arc\tree; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Implements a tree node which has uniquely named child nodes, as in a filesystem. |
14
|
|
|
* @property \arc\tree\NamedNodeList $childNodes |
15
|
|
|
* @property \arc\tree\NamedNode $parentNode |
16
|
|
|
* @property string $nodeName |
17
|
|
|
*/ |
18
|
|
|
class NamedNode implements \Serializable |
19
|
|
|
{ |
20
|
|
|
public $nodeValue = null; |
21
|
|
|
private $parentNode = null; |
22
|
|
|
private $childNodes = null; |
23
|
|
|
private $nodeName = ''; |
24
|
|
|
|
25
|
14 |
|
public function __construct($nodeName='', $parentNode = null, $childNodes = null, $nodeValue = null) |
26
|
|
|
{ |
27
|
14 |
|
$this->nodeName = $nodeName; |
28
|
14 |
|
$this->parentNode = $parentNode; |
29
|
14 |
|
$this->childNodes = new NamedNodeList( (array) $childNodes, $this ); |
30
|
14 |
|
$this->nodeValue = $nodeValue; |
31
|
14 |
|
} |
32
|
|
|
|
33
|
14 |
|
public function __get($name) |
34
|
|
|
{ |
35
|
|
|
switch ($name) { |
36
|
14 |
|
case 'nodeName': |
37
|
10 |
|
return $this->nodeName; |
38
|
|
|
break; |
39
|
7 |
|
case 'childNodes': |
40
|
12 |
|
return $this->childNodes; |
41
|
|
|
break; |
42
|
2 |
|
case 'parentNode': |
43
|
4 |
|
return $this->parentNode; |
44
|
|
|
break; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
14 |
|
public function __set($name, $value) |
49
|
|
|
{ |
50
|
|
|
switch ($name) { |
51
|
14 |
|
case 'nodeName': |
52
|
|
|
$this->_setNodeName( $value ); |
53
|
|
|
break; |
54
|
7 |
|
case 'childNodes': |
55
|
|
|
// make sure nodelists aren't shared between namednodes. |
56
|
|
|
$this->childNodes = new NamedNodeList( (array) $value, $this ); |
57
|
|
|
break; |
58
|
7 |
|
case 'parentNode': |
59
|
14 |
|
$this->_setParentNode( $value ); |
60
|
14 |
|
break; |
61
|
|
|
} |
62
|
14 |
|
} |
63
|
|
|
|
64
|
|
|
private function _setNodeName($name) |
65
|
|
|
{ |
66
|
|
|
if ($this->parentNode) { |
67
|
|
|
if ($this->parentNode->childNodes[$name] !== $this) { |
68
|
|
|
$this->parentNode->childNodes[$name] = $this; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
$this->nodeName = $name; |
72
|
|
|
} |
73
|
|
|
|
74
|
14 |
|
private function _setParentNode($node) |
75
|
|
|
{ |
76
|
14 |
|
if ($node instanceof NamedNode) { |
77
|
14 |
|
$node->appendChild( $this->nodeName, $this ); |
78
|
7 |
|
} elseif (isset($node)) { |
79
|
|
|
throw new \arc\UnknownError( 'parentNode is not a \arc\tree\NamedNode', \arc\exceptions::ILLEGAL_ARGUMENT ); |
80
|
2 |
|
} elseif ($this->parentNode) { |
81
|
2 |
|
$this->parentNode->removeChild( $this->nodeName ); |
82
|
1 |
|
} |
83
|
14 |
|
} |
84
|
|
|
|
85
|
1 |
|
public function __isset($name) |
86
|
|
|
{ |
87
|
|
|
switch ($name) { |
88
|
|
|
case 'nodeName': |
89
|
|
|
case 'childNodes': |
90
|
|
|
return true; // these are always _set_, but may be empty |
91
|
|
|
break; |
92
|
|
|
case 'parentNode': |
93
|
|
|
return isset( $this->parentNode ); |
94
|
|
|
break; |
95
|
|
|
default: |
96
|
1 |
|
return isset( $this->childNodes[ $name ] ); |
97
|
1 |
|
break; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/* The tree itself must always be deep cloned, a single node cannot have two parentNodes. |
102
|
|
|
* The nodeValue may be whatever - so if it is an object, that object will not be cloned. |
103
|
|
|
*/ |
104
|
2 |
|
public function __clone() |
105
|
|
|
{ |
106
|
2 |
|
$this->parentNode = null; |
107
|
2 |
|
$this->childNodes = clone $this->childNodes; |
108
|
2 |
|
$this->childNodes->parentNode = $this; |
109
|
2 |
|
} |
110
|
|
|
|
111
|
4 |
|
public function __toString() |
112
|
|
|
{ |
113
|
4 |
|
return (string) $this->nodeValue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// \Serializable interface |
117
|
|
|
public function serialize() |
118
|
|
|
{ |
119
|
|
|
return serialize( \arc\tree::collapse( $this ) ); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function unserialize($data) |
123
|
|
|
{ |
124
|
|
|
return \arc\tree::expand( unserialize( $data ) ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Adds a new child element to this node with the given name as index in the child list. |
129
|
|
|
* If an existing child has the same name, that child will be discarded. |
130
|
|
|
* @param string $name The index name of the child |
|
|
|
|
131
|
|
|
* @param mixed $data The data for the new child. If $data is not an instance of \arc\tree\NamedNode |
|
|
|
|
132
|
|
|
* a new instance will be constructed with $data as its nodeValue. |
133
|
|
|
* @return \arc\tree\NamedNode The new child node. |
134
|
|
|
*/ |
135
|
14 |
|
public function appendChild($nodeName, $child=null) |
136
|
|
|
{ |
137
|
14 |
|
if ( !( $child instanceof \arc\tree\NamedNode )) { |
138
|
12 |
|
$child = new \arc\tree\NamedNode( $nodeName, $this, null, $child ); |
139
|
6 |
|
} |
140
|
14 |
|
if ( $child->parentNode !== $this) { |
141
|
2 |
|
if ( isset($child->parentNode)) { |
142
|
|
|
$child->parentNode->removeChild( $child->nodeName ); |
143
|
|
|
} |
144
|
2 |
|
if (isset( $this->childNodes[ $nodeName ] )) { |
145
|
2 |
|
$oldChild = $this->childNodes[ $nodeName ]; |
146
|
2 |
|
$oldChild->parentNode = null; |
147
|
1 |
|
} |
148
|
2 |
|
$child->parentNode = $this; |
149
|
1 |
|
} |
150
|
14 |
|
$this->childNodes[ $nodeName ] = $child; |
151
|
|
|
|
152
|
14 |
|
return $child; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Removes an existing child with the given name from this node. |
157
|
|
|
* @param string $nodeName The index name of the child |
158
|
|
|
* @return \arc\tree\NamedNode The removed child or null. |
159
|
|
|
*/ |
160
|
2 |
|
public function removeChild($nodeName) |
161
|
|
|
{ |
162
|
2 |
|
if ( isset( $this->childNodes[ $nodeName ] )) { |
163
|
2 |
|
$child = $this->childNodes[ $nodeName ]; |
164
|
2 |
|
unset( $this->childNodes[ $nodeName ] ); |
165
|
2 |
|
$child->parentNode = null; |
166
|
|
|
|
167
|
2 |
|
return $child; |
168
|
|
|
} else { |
169
|
|
|
return null; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns the absolute path of the node. |
175
|
|
|
* @return string the absolute path of the node |
176
|
|
|
*/ |
177
|
4 |
|
public function getPath() |
178
|
|
|
{ |
179
|
4 |
|
return \arc\tree::parents( |
180
|
2 |
|
$this, |
|
|
|
|
181
|
|
|
function ($node, $result) { |
182
|
4 |
|
return $result . $node->nodeName . '/'; |
183
|
2 |
|
} |
184
|
2 |
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns the root node object of this tree. |
189
|
|
|
* @return \arc\tree\Node the root node |
190
|
|
|
*/ |
191
|
6 |
|
public function getRootNode() |
192
|
|
|
{ |
193
|
6 |
|
$result = \arc\tree::dive( |
194
|
3 |
|
$this, |
|
|
|
|
195
|
|
|
function ($node) { |
196
|
6 |
|
return isset( $node->parentNode ) ? null : $node; |
197
|
3 |
|
} |
198
|
3 |
|
); |
199
|
|
|
|
200
|
6 |
|
return $result; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns the node with the given path, relative to this node. If the path |
205
|
|
|
* does not exist, missing nodes will be created automatically. |
206
|
|
|
* @param string $path The path to change to |
207
|
|
|
* @return \arc\tree\NamedNode The target node corresponding with the given path. |
208
|
|
|
*/ |
209
|
8 |
|
public function cd($path) |
210
|
|
|
{ |
211
|
8 |
|
if (\arc\path::isAbsolute( $path )) { |
212
|
6 |
|
$node = $this->getRootNode(); |
213
|
3 |
|
} else { |
214
|
4 |
|
$node = $this; |
215
|
|
|
} |
216
|
8 |
|
$result = \arc\path::reduce( $path, function ($node, $name) { |
217
|
|
|
switch ($name) { |
218
|
8 |
|
case '..': |
219
|
|
|
return ( isset( $node->parentNode ) ? $node->parentNode : $node ); |
220
|
|
|
break; |
221
|
4 |
|
case '.': |
222
|
8 |
|
case '': |
223
|
|
|
return $node; |
224
|
|
|
break; |
225
|
4 |
|
default: |
226
|
8 |
|
if ( !isset( $node->childNodes[ $name ] ) ) { |
227
|
2 |
|
return $node->appendChild( $name ); |
228
|
|
|
} else { |
229
|
6 |
|
return $node->childNodes[ $name ]; |
230
|
|
|
} |
231
|
|
|
break; |
232
|
4 |
|
} |
233
|
8 |
|
}, $node); |
234
|
|
|
|
235
|
8 |
|
return $result; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Calls a callback method on each child of this node, returns an array with name => result pairs. |
240
|
|
|
* The callback method must accept two parameters, the name of the child and the child node itself. |
241
|
|
|
* @param callable $callback The callback method to run on each child. |
242
|
|
|
* @return array An array of result values with the name of each child as key. |
243
|
|
|
*/ |
244
|
|
|
public function ls($callback) |
245
|
|
|
{ |
246
|
|
|
return \arc\tree::ls( $this, $callback ); |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
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: