[TASK] Clean up the phpDoc of t3lib/tree/
[Packages/TYPO3.CMS.git] / t3lib / tree / class.t3lib_tree_node.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 TYPO3 Tree Team <http://forge.typo3.org/projects/typo3v4-extjstrees>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Tree Node
30 *
31 * @author Stefan Galinski <stefan.galinski@gmail.com>
32 * @author Steffen Ritter <info@steffen-ritter.net>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36 class t3lib_tree_Node implements t3lib_tree_ComparableNode, Serializable {
37 /**
38 * Node Identifier
39 *
40 * @var string
41 */
42 protected $id = '';
43
44 /**
45 * Parent Node
46 *
47 * @var t3lib_tree_Node
48 */
49 protected $parentNode = NULL;
50
51 /**
52 * Child Nodes
53 *
54 * @var t3lib_tree_NodeCollection
55 */
56 protected $childNodes = NULL;
57
58 /**
59 * Constructor
60 *
61 * You can move an initial data array to initialize the instance and further objects.
62 * This is useful for the deserialization.
63 *
64 * @param array $data
65 * @return void
66 */
67 public function __construct(array $data = array()) {
68 if (count($data)) {
69 $this->dataFromArray($data);
70 }
71 }
72
73 /**
74 * Sets the child nodes collection
75 *
76 * @param t3lib_tree_NodeCollection $childNodes
77 * @return void
78 */
79 public function setChildNodes(t3lib_tree_NodeCollection $childNodes) {
80 $this->childNodes = $childNodes;
81 }
82
83 /**
84 * Removes child nodes collection
85 *
86 * @return void
87 */
88 public function removeChildNodes() {
89 if ($this->childNodes !== NULL) {
90 unset($this->childNodes);
91 $this->childNodes = NULL;
92 }
93 }
94
95 /**
96 * Returns child nodes collection
97 *
98 * @return t3lib_tree_NodeCollection
99 */
100 public function getChildNodes() {
101 return $this->childNodes;
102 }
103
104 /**
105 * Returns TRUE if the node has child nodes attached
106 *
107 * @return boolean
108 */
109 public function hasChildNodes() {
110 if ($this->childNodes !== NULL) {
111 return TRUE;
112 }
113
114 return FALSE;
115 }
116
117 /**
118 * Sets the identifier
119 *
120 * @param string $id
121 * @return void
122 */
123 public function setId($id) {
124 $this->id = $id;
125 }
126
127 /**
128 * Returns the identifier
129 *
130 * @return string
131 */
132 public function getId() {
133 return $this->id;
134 }
135
136 /**
137 * Sets the parent node
138 *
139 * @param t3lib_tree_Node $parentNode
140 * @return void
141 */
142 public function setParentNode(t3lib_tree_Node $parentNode = NULL) {
143 $this->parentNode = $parentNode;
144 }
145
146 /**
147 * Returns the parent node
148 *
149 * @return t3lib_tree_Node
150 */
151 public function getParentNode() {
152 return $this->parentNode;
153 }
154
155 /**
156 * Compares a node if it's identical to another node by the id property.
157 *
158 * @param t3lib_tree_Node $other
159 * @return boolean
160 */
161 public function equals(t3lib_tree_Node $other) {
162 return $this->id == $other->getId();
163 }
164
165 /**
166 * Compares a node to another one.
167 *
168 * Returns:
169 * 1 if its greater than the other one
170 * -1 if its smaller than the other one
171 * 0 if its equal
172 *
173 * @param t3lib_tree_Node $other
174 * @return integer See description above
175 */
176 public function compareTo($other) {
177 if ($this->equals($other)) {
178 return 0;
179 }
180
181 return ($this->id > $other->getId()) ? 1 : -1;
182 }
183
184 /**
185 * Returns the node in an array representation that can be used for serialization
186 *
187 * @param boolean $addChildNodes
188 * @return array
189 */
190 public function toArray($addChildNodes = TRUE) {
191 $arrayRepresentation = array(
192 'serializeClassName' => get_class($this),
193 'id' => $this->id
194 );
195
196 if ($this->parentNode !== NULL) {
197 $arrayRepresentation['parentNode'] = $this->parentNode->toArray(FALSE);
198 } else {
199 $arrayRepresentation['parentNode'] = '';
200 }
201
202 if ($this->hasChildNodes() && $addChildNodes) {
203 $arrayRepresentation['childNodes'] = $this->childNodes->toArray();
204 } else {
205 $arrayRepresentation['childNodes'] = '';
206 }
207
208 return $arrayRepresentation;
209 }
210
211 /**
212 * Sets data of the node by a given data array
213 *
214 * @param array $data
215 * @return void
216 */
217 public function dataFromArray($data) {
218 $this->setId($data['id']);
219
220 if (isset($data['parentNode']) && $data['parentNode'] !== '') {
221 $this->setParentNode(t3lib_div::makeInstance(
222 $data['parentNode']['serializeClassName'],
223 $data['parentNode']
224 ));
225 }
226
227 if (isset($data['childNodes']) && $data['childNodes'] !== '') {
228 $this->setChildNodes(t3lib_div::makeInstance(
229 $data['childNodes']['serializeClassName'],
230 $data['childNodes']
231 ));
232 }
233 }
234
235 /**
236 * Returns the serialized instance
237 *
238 * @return string
239 */
240 public function serialize() {
241 return serialize($this->toArray());
242 }
243
244 /**
245 * Fills the current node with the given serialized informations
246 *
247 * @throws t3lib_exception if the deserialized object type is not identical to the current one
248 * @param string $serializedString
249 * @return void
250 */
251 public function unserialize($serializedString) {
252 $arrayRepresentation = unserialize($serializedString);
253 if ($arrayRepresentation['serializeClassName'] !== get_class($this)) {
254 throw new t3lib_exception('Deserialized object type is not identical!', 1294586646);
255 }
256 $this->dataFromArray($arrayRepresentation);
257 }
258 }
259
260 ?>