|
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 extends Node implements \Serializable |
|
19
|
|
|
{ |
|
20
|
|
|
public $nodeValue = null; |
|
21
|
|
|
private $parentNode = null; |
|
22
|
|
|
private $childNodes = null; |
|
23
|
|
|
private $nodeName = ''; |
|
24
|
|
|
|
|
25
|
16 |
|
public function __construct($nodeName='', $parentNode = null, $childNodes = null, $nodeValue = null) |
|
26
|
|
|
{ |
|
27
|
16 |
|
$this->nodeName = $nodeName; |
|
28
|
16 |
|
$this->parentNode = $parentNode; |
|
29
|
16 |
|
$this->childNodes = new NamedNodeList( (array) $childNodes, $this ); |
|
30
|
16 |
|
$this->nodeValue = $nodeValue; |
|
31
|
16 |
|
} |
|
32
|
|
|
|
|
33
|
16 |
|
public function __get($name) |
|
34
|
|
|
{ |
|
35
|
16 |
|
switch ($name) { |
|
36
|
16 |
|
case 'nodeName': |
|
37
|
10 |
|
return $this->nodeName; |
|
38
|
|
|
break; |
|
39
|
16 |
|
case 'childNodes': |
|
40
|
14 |
|
return $this->childNodes; |
|
41
|
|
|
break; |
|
42
|
16 |
|
case 'parentNode': |
|
43
|
16 |
|
return $this->parentNode; |
|
44
|
|
|
break; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
public function __set($name, $value) |
|
49
|
|
|
{ |
|
50
|
2 |
|
switch ($name) { |
|
51
|
2 |
|
case 'nodeName': |
|
52
|
|
|
$this->_setNodeName( $value ); |
|
53
|
|
|
break; |
|
54
|
2 |
|
case 'childNodes': |
|
55
|
|
|
// make sure nodelists aren't shared between namednodes. |
|
56
|
|
|
$this->childNodes = new NamedNodeList( (array) $value, $this ); |
|
57
|
|
|
break; |
|
58
|
2 |
|
case 'parentNode': |
|
59
|
2 |
|
$this->_setParentNode( $value ); |
|
60
|
2 |
|
break; |
|
61
|
|
|
} |
|
62
|
2 |
|
} |
|
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
|
2 |
|
private function _setParentNode($node) |
|
75
|
|
|
{ |
|
76
|
2 |
|
if ($node instanceof NamedNode) { |
|
77
|
2 |
|
$node->appendChild( $this->nodeName, $this ); |
|
78
|
|
|
} 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
|
|
|
} |
|
83
|
2 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
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
|
|
|
return isset( $this->childNodes[ $name ] ); |
|
97
|
|
|
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
|
|
|
} |
|
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
|
|
|
} |
|
148
|
2 |
|
$child->parentNode = $this; |
|
149
|
|
|
} |
|
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
|
4 |
|
public function removeChild($nodeName) |
|
161
|
|
|
{ |
|
162
|
4 |
|
if ( isset( $this->childNodes[ $nodeName ] )) { |
|
163
|
4 |
|
$child = $this->childNodes[ $nodeName ]; |
|
164
|
4 |
|
$child->parentNode = null; |
|
165
|
4 |
|
unset( $this->childNodes[ $nodeName ] ); |
|
166
|
|
|
|
|
167
|
4 |
|
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
|
4 |
|
$this, |
|
181
|
4 |
|
function ($node, $result) { |
|
182
|
4 |
|
return $result . $node->nodeName . '/'; |
|
183
|
4 |
|
} |
|
184
|
|
|
); |
|
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
|
6 |
|
$this, |
|
195
|
6 |
|
function ($node) { |
|
196
|
6 |
|
return isset( $node->parentNode ) ? null : $node; |
|
197
|
6 |
|
} |
|
198
|
|
|
); |
|
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
|
|
|
} else { |
|
214
|
4 |
|
$node = $this; |
|
215
|
|
|
} |
|
216
|
8 |
|
$result = \arc\path::reduce( $path, function ($node, $name) { |
|
217
|
8 |
|
switch ($name) { |
|
218
|
8 |
|
case '..': |
|
219
|
|
|
return ( isset( $node->parentNode ) ? $node->parentNode : $node ); |
|
220
|
|
|
break; |
|
221
|
8 |
|
case '.': |
|
222
|
8 |
|
case '': |
|
223
|
|
|
return $node; |
|
224
|
|
|
break; |
|
225
|
|
|
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
|
|
|
} |
|
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
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.