## Apache Zeta Components Manual :: File Source for tree_node.php

## Source for file tree_node.php

Documentation is available at tree_node.php

1. <?php
2. /**
3. * File containing the ezcTreeNode class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @version //autogentag//
24. * @filesource
25. * @package Tree
26. */
28. /**
29. * ezcTreeNode represents a node in a tree.
30. *
31. * The methods that operate on nodes (fetchChildren, fetchPath, ...,
32. * isSiblingOf) are all marshalled to calls on the tree (that is stored in the
33. * $tree private variable) itself.
34. *
35. * Example:
36. * <code>
37. * <?php
38. * // Creates a new node with ID 'O' and as data 'Oxygen'
39. * $node = new ezcTreeNode( $this->tree, 'O', 'Oxygen' );
40. *
41. * // Adds a node as child element to another already create node in a tree
42. * $parentNode->addChild( $node );
43. * ?>
44. * </code>
45. *
46. * To use your own implementation of tree nodes, you can override the class
47. * that is used by the tree by setting the nodeClassName property of the
48. * ezcTree class. The class must inherit from this class though.
49. *
50. * @property-read string $id The ID that uniquely identifies a node
51. * @property-read ezcTree $tree The tree object that this node belongs to
52. * @property mixed $data The data belonging to a node
53. * @property bool $dataFetched Whether the data for this node has been
54. * fetched. Should *only* be modified by
55. * data store implementations.
56. * @property bool $dataStored Whether the data for this node has been
57. * stored. Should *only* be modified by
58. * data store implementations.
59. *
60. * @package Tree
61. * @version //autogentag//
62. * @mainclass
63. */
64. class ezcTreeNode implements ezcTreeVisitable
65. {
66. /**
67. * Holds the properties of this class.
68. *
69. * @var array(string=>mixed)
70. */
71. private $properties = array();
73. /**
74. * Constructs a new ezcTreeNode object with ID $nodeId on tree $tree.
75. *
76. * If a third argument is specified it is used as data for the new node.
77. *
78. * @param ezcTree $tree
79. * @param string $nodeId
80. * @param mixed $data
81. */
82. public function __construct ( ezcTree $tree, $nodeId )
83. {
84. $this->properties['id'] = (string) $nodeId;
85. $this->properties['tree'] = $tree;
87. if ( func_num_args () === 2 )
88. {
89. $this->properties['data'] = null;
90. $this->properties['dataFetched'] = false;
91. $this->properties['dataStored'] = true;
92. }
93. else
94. {
95. $this->properties['data'] = func_get_arg ( 2 );
96. $this->properties['dataFetched'] = true;
97. $this->properties['dataStored'] = false;
98. }
99. }
101. /**
102. * Returns the value of the property $name.
103. *
104. * @throws ezcBasePropertyNotFoundException if the property does not exist.
105. * @param string $name
106. * @return mixed
107. * @ignore
108. */
109. public function __get( $name )
110. {
111. switch ( $name )
112. {
113. case 'data':
114. if ( $this->properties['dataFetched'] === false )
115. {
116. // fetch the data on the fly
117. $this->tree->store->fetchDataForNode( $this );
118. }
119. // break intentionally missing
120. case 'id':
121. case 'dataFetched':
122. case 'dataStored':
123. case 'tree':
124. return $this->properties[$name];
126. }
127. throw new ezcBasePropertyNotFoundException ( $name );
128. }
130. /**
131. * Sets the property $name to $value.
132. *
133. * @throws ezcBasePropertyNotFoundException if the property does not exist.
134. * @throws ezcBasePropertyPermissionException if a read-only property is
135. * tried to be modified.
136. * @throws ezcBaseValueException if trying to assign a wrong value to
137. * the property
138. * @param string $name
139. * @param mixed $value
140. * @ignore
141. */
142. public function __set( $name, $value )
143. {
144. switch ( $name )
145. {
146. case 'id':
147. case 'tree':
148. throw new ezcBasePropertyPermissionException ( $name, ezcBasePropertyPermissionException ::READ );
150. case 'data':
151. if ( !$this->properties['dataFetched'] )
152. {
153. $this->tree->store->fetchDataForNode( $this );
154. $this->properties['dataFetched'] = true;
155. }
157. $this->properties[$name] = $value;
158. $this->properties['dataStored'] = false;
159. $this->tree->store->storeDataForNode( $this );
160. return;
162. case 'dataFetched':
163. case 'dataStored':
164. if ( ! is_bool ( $value ) )
165. {
166. throw new ezcBaseValueException ( $name, $value, 'boolean' );
167. }
168. $this->properties[$name] = $value;
169. break;
171. default:
172. throw new ezcBasePropertyNotFoundException ( $name );
173. }
174. }
176. /**
177. * Returns true if the property $name is set, otherwise false.
178. *
179. * @param string $name
180. * @return bool
181. * @ignore
182. */
183. public function __isset( $name )
184. {
185. switch ( $name )
186. {
187. case 'id':
188. case 'tree':
189. case 'data':
190. case 'dataFetched':
191. case 'dataStored':
192. return isset( $this->properties[$name] );
194. default:
195. return false;
196. }
197. }
199. /**
200. * Inject data.
201. *
202. * Used to set the data from a data loader. Should not be used for
203. * interfacing with the tree node, since the node will not be flagged as
204. * modified by this method.
205. *
206. * @access private
207. * @param string $data
208. * @return void
209. */
210. public function injectData ( $data )
211. {
212. $this->properties['data'] = $data;
213. }
215. /**
216. * Implements the accept method for visiting.
217. *
218. * @param ezcTreeVisitor $visitor
219. */
220. public function accept ( ezcTreeVisitor $visitor )
221. {
222. $visitor->visit( $this );
223. foreach ( $this-> fetchChildren ()->nodes as $childNode )
224. {
225. $childNode->accept( $visitor );
226. }
227. }
229. /**
230. * Adds the node $node as child of the current node to the tree.
231. *
232. * @param ezcTreeNode $node
233. */
234. public function addChild ( ezcTreeNode $node )
235. {
236. $this->tree->addChild( $this->id, $node );
237. }
239. /**
240. * Returns all the children of this node.
241. *
242. * @return ezcTreeNodeList
243. */
244. public function fetchChildren ()
245. {
246. return $this->tree->fetchChildren( $this->id );
247. }
249. /**
250. * Returns all the nodes in the path from the root node to this node.
251. *
252. * @return ezcTreeNodeList
253. */
254. public function fetchPath ()
255. {
256. return $this->tree->fetchPath( $this->id );
257. }
259. /**
260. * Returns the parent node of this node.
261. *
262. * @return ezcTreeNode
263. */
264. public function fetchParent ()
265. {
266. return $this->tree->fetchParent( $this->id );
267. }
269. /**
270. * Returns this node and all its children, sorted according to the
271. * {@link http://en.wikipedia.org/wiki/Depth-first_search Depth-first sorting}
272. * algorithm.
273. *
274. * @return ezcTreeNodeList
275. */
276. public function fetchSubtreeDepthFirst ()
277. {
278. return $this->tree->fetchSubtreeDepthFirst( $this->id );
279. }
281. /**
282. * Alias for fetchSubtreeDepthFirst().
283. *
284. * @see fetchSubtreeDepthFirst
285. * @return ezcTreeNodeList
286. */
287. public function fetchSubtree ()
288. {
289. return $this-> fetchSubtreeDepthFirst ();
290. }
292. /**
293. * Returns this node and all its children, sorted accoring to the
294. * {@link http://en.wikipedia.org/wiki/Breadth-first_search Breadth-first sorting}
295. * algorithm.
296. *
297. * @return ezcTreeNodeList
298. */
299. public function fetchSubtreeBreadthFirst ()
300. {
301. return $this->tree->fetchSubtreeBreadthFirst( $this->id );
302. }
304. /**
305. * Returns the number of direct children of this node.
306. *
307. * @return int
308. */
309. public function getChildCount ()
310. {
311. return $this->tree->getChildCount( $this->id );
312. }
314. /**
315. * Returns the number of children of this node, recursively iterating over
316. * the children.
317. *
318. * @return int
319. */
320. public function getChildCountRecursive ()
321. {
322. return $this->tree->getChildCountRecursive( $this->id );
323. }
325. /**
326. * Returns the distance from the root node to this node.
327. *
328. * @return int
329. */
330. public function getPathLength ()
331. {
332. return $this->tree->getPathlength( $this->id );
333. }
335. /**
336. * Returns whether this node has children.
337. *
338. * @return bool
339. */
340. public function hasChildNodes ()
341. {
342. return $this->tree->hasChildNodes( $this->id );
343. }
345. /**
346. * Returns whether this node is a direct child of the $parentNode node.
347. *
348. * @param ezcTreeNode $parentNode
349. *
350. * @return bool
351. */
352. public function isChildOf ( ezcTreeNode $parentNode )
353. {
354. return $this->tree->isChildOf( $this->id, $parentNode->id );
355. }
357. /**
358. * Returns whether this node is a direct or indirect child of the
359. * $parentNode node.
360. *
361. * @param ezcTreeNode $parentNode
362. *
363. * @return bool
364. */
365. public function isDescendantOf ( ezcTreeNode $parentNode )
366. {
367. return $this->tree->isDescendantOf( $this->id, $parentNode->id );
368. }
370. /**
371. * Returns whether this node, and the $child2Node node are are siblings
372. * (ie, they share the same parent).
373. *
374. * @param ezcTreeNode $child2Node
375. *
376. * @return bool
377. */
378. public function isSiblingOf ( ezcTreeNode $child2Node )
379. {
380. return $this->tree->isSiblingOf( $this->id, $child2Node->id );
381. }
383. /**
384. * Returns the text representation of a node (its ID).
385. *
386. * @return string
387. * @ignore
388. */
389. public function __toString()
390. {
391. return $this->id;
392. }
393. }
394. ?>

Documentation generated by phpDocumentor 1.4.3