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

## Source for file tree_node_list.php

Documentation is available at tree_node_list.php

1. <?php
2. /**
3. * File containing the ezcTreeNodeList 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. * ezcTreeNodeList represents a lists of nodes.
30. *
31. * The nodes in the list can be accessed through an array as this class
32. * implements the ArrayAccess SPL interface. The array is indexed based on the
33. * the node's ID.
34. *
35. * Example:
36. * <code>
37. * <?php
38. * // Create a list with two elements
39. * $list = new ezcTreeNodeList;
40. * $list->addNode( new ezcTreeNode( $tree, 'Leo' ) );
41. * $list->addNode( new ezcTreeNode( $tree, 'Virgo' ) );
42. *
43. * // Retrieve the list's size
44. * echo $list->size, "\n"; // prints 2
45. *
46. * // Find a node in the list
47. * $node = $list['Virgo'];
48. *
49. * // Add nodes in an alternative way
50. * $list['Libra'] = new ezcTreeNode( $tree, 'Libra' );
51. *
52. * // Remove a node from the list
53. * unset( $list['Leo'] );
54. *
55. * // Checking if a node exists
56. * if ( isset( $list['Scorpius'] ) )
57. * {
58. * // do something if it exists
59. * }
60. *
61. * // Use the associated data store to fetch the data for all nodes at once
62. * $list->fetchDataForNodes();
63. * ?>
64. * </code>
65. *
66. * @see ezcTreeNodeListIterator
67. *
68. * @property-read string $size
69. * The number of nodes in the list.
70. * @property-read array(string=>ezcTreeNode) $nodes
71. * The nodes belonging to this list.
72. *
73. * @package Tree
74. * @version //autogentag//
75. */
76. class ezcTreeNodeList implements ArrayAccess
77. {
78. /**
79. * Holds the nodes of this list.
80. *
81. * @var array(ezcTreeNode)
82. */
83. private $nodes;
85. /**
86. * Constructs a new ezcTreeNodeList object.
87. */
88. public function __construct ()
89. {
90. $this->nodes = array();
91. }
93. /**
94. * Returns the value of the property $name.
95. *
96. * @throws ezcBasePropertyNotFoundException if the property does not exist.
97. * @param string $name
98. * @ignore
99. */
100. public function __get( $name )
101. {
102. switch ( $name )
103. {
104. case 'nodes':
105. return $this->nodes;
107. case 'size':
108. return count ( $this->nodes );
110. }
111. throw new ezcBasePropertyNotFoundException ( $name );
112. }
114. /**
115. * Sets the property $name to $value.
116. *
117. * @throws ezcBasePropertyNotFoundException if the property does not exist.
118. * @throws ezcBasePropertyPermissionException if a read-only property is
119. * tried to be modified.
120. * @param string $name
121. * @param mixed $value
122. * @ignore
123. */
124. public function __set( $name, $value )
125. {
126. switch ( $name )
127. {
128. case 'nodes':
129. case 'size':
130. throw new ezcBasePropertyPermissionException ( $name, ezcBasePropertyPermissionException ::READ );
132. default:
133. throw new ezcBasePropertyNotFoundException ( $name );
134. }
135. }
137. /**
138. * Returns whether a node with the ID $nodeId exists in the list.
139. *
140. * This method is part of the SPL ArrayAccess interface.
141. *
142. * @param string $nodeId
143. * @return bool
144. * @ignore
145. */
146. public function offsetExists( $nodeId )
147. {
148. return array_key_exists ( $nodeId, $this->nodes );
149. }
151. /**
152. * Returns the node with the ID $nodeId.
153. *
154. * This method is part of the SPL ArrayAccess interface.
155. *
156. * @param string $nodeId
157. * @return ezcTreeNode
158. * @ignore
159. */
160. public function offsetGet( $nodeId )
161. {
162. return $this->nodes[$nodeId];
163. }
165. /**
166. * Adds a new node with node ID $nodeId to the list.
167. *
168. * This method is part of the SPL ArrayAccess interface.
169. *
170. * @throws ezcTreeInvalidClassException if the data to be set as array
171. * element is not an instance of ezcTreeNode
172. * @throws ezcTreeIdsDoNotMatchException if the array index $nodeId does not
173. * match the tree node's ID that is stored in the $data object
174. * @param string $nodeId
175. * @param ezcTreeNode $data
176. * @ignore
177. */
178. public function offsetSet( $nodeId, $data )
179. {
180. if ( !$data instanceof ezcTreeNode )
181. {
182. throw new ezcTreeInvalidClassException ( 'ezcTreeNode', get_class ( $data ) );
183. }
184. if ( $data->id !== $nodeId )
185. {
186. throw new ezcTreeIdsDoNotMatchException ( $data->id, $nodeId );
187. }
188. $this-> addNode ( $data );
189. }
191. /**
192. * Removes the node with ID $nodeId from the list.
193. *
194. * This method is part of the SPL ArrayAccess interface.
195. *
196. * @param string $nodeId
197. * @ignore
198. */
199. public function offsetUnset( $nodeId )
200. {
201. unset( $this->nodes[$nodeId] );
202. }
205. /**
206. * Adds the node $node to the list.
207. *
208. * @param ezcTreeNode $node
209. */
210. public function addNode ( ezcTreeNode $node )
211. {
212. $this->nodes[$node->id] = $node;
213. }
215. /**
216. * Fetches data for all nodes in the node list.
217. */
218. public function fetchDataForNodes ()
219. {
220. // We need to use a little trick to get to the tree object. We can do
221. // that through ezcTreeNode objects that are part of this list. We
222. // can't do that when the list is empty. In that case we just return.
223. if ( count ( $this->nodes ) === 0 )
224. {
225. return;
226. }
227. // Find a node in the list
228. reset ( $this->nodes );
229. $node = current ( $this->nodes );
230. // Grab the tree and use it to fetch data for all nodes from the store
231. $tree = $node->tree;
232. $tree->store->fetchDataForNodes( $this );
233. }
234. }
235. ?>

Documentation generated by phpDocumentor 1.4.3