12e20128426cd13b3b38a963a5b1e6cb9ba7afdc
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Tree / TreeNode.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tree;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Tree Node
21 */
22 class TreeNode implements \TYPO3\CMS\Backend\Tree\ComparableNodeInterface, \Serializable
23 {
24 /**
25 * Node Identifier
26 *
27 * @var string
28 */
29 protected $id = '';
30
31 /**
32 * Parent Node
33 *
34 * @var \TYPO3\CMS\Backend\Tree\TreeNode
35 */
36 protected $parentNode = null;
37
38 /**
39 * Child Nodes
40 *
41 * @var \TYPO3\CMS\Backend\Tree\TreeNodeCollection
42 */
43 protected $childNodes = null;
44
45 /**
46 * Constructor
47 *
48 * You can move an initial data array to initialize the instance and further objects.
49 * This is useful for the deserialization.
50 *
51 * @param array $data
52 */
53 public function __construct(array $data = [])
54 {
55 if (!empty($data)) {
56 $this->dataFromArray($data);
57 }
58 }
59
60 /**
61 * Sets the child nodes collection
62 *
63 * @param \TYPO3\CMS\Backend\Tree\TreeNodeCollection $childNodes
64 */
65 public function setChildNodes(\TYPO3\CMS\Backend\Tree\TreeNodeCollection $childNodes)
66 {
67 $this->childNodes = $childNodes;
68 }
69
70 /**
71 * Removes child nodes collection
72 */
73 public function removeChildNodes()
74 {
75 if ($this->childNodes !== null) {
76 unset($this->childNodes);
77 $this->childNodes = null;
78 }
79 }
80
81 /**
82 * Returns child nodes collection
83 *
84 * @return \TYPO3\CMS\Backend\Tree\TreeNodeCollection
85 */
86 public function getChildNodes()
87 {
88 return $this->childNodes;
89 }
90
91 /**
92 * Returns TRUE if the node has child nodes attached
93 *
94 * @return bool
95 */
96 public function hasChildNodes()
97 {
98 if ($this->childNodes !== null) {
99 return true;
100 }
101 return false;
102 }
103
104 /**
105 * Sets the identifier
106 *
107 * @param string $id
108 */
109 public function setId($id)
110 {
111 $this->id = $id;
112 }
113
114 /**
115 * Returns the identifier
116 *
117 * @return string
118 */
119 public function getId()
120 {
121 return $this->id;
122 }
123
124 /**
125 * Sets the parent node
126 *
127 * @param NULL|\TYPO3\CMS\Backend\Tree\TreeNode $parentNode
128 */
129 public function setParentNode(\TYPO3\CMS\Backend\Tree\TreeNode $parentNode = null)
130 {
131 $this->parentNode = $parentNode;
132 }
133
134 /**
135 * Returns the parent node
136 *
137 * @return \TYPO3\CMS\Backend\Tree\TreeNode
138 */
139 public function getParentNode()
140 {
141 return $this->parentNode;
142 }
143
144 /**
145 * Compares a node if it's identical to another node by the id property.
146 *
147 * @param \TYPO3\CMS\Backend\Tree\TreeNode $other
148 * @return bool
149 */
150 public function equals(\TYPO3\CMS\Backend\Tree\TreeNode $other)
151 {
152 return $this->id == $other->getId();
153 }
154
155 /**
156 * Compares a node to another one.
157 *
158 * Returns:
159 * 1 if its greater than the other one
160 * -1 if its smaller than the other one
161 * 0 if its equal
162 *
163 * @param \TYPO3\CMS\Backend\Tree\TreeNode $other
164 * @return int See description above
165 */
166 public function compareTo($other)
167 {
168 if ($this->equals($other)) {
169 return 0;
170 }
171 return $this->id > $other->getId() ? 1 : -1;
172 }
173
174 /**
175 * Returns the node in an array representation that can be used for serialization
176 *
177 * @param bool $addChildNodes
178 * @return array
179 */
180 public function toArray($addChildNodes = true)
181 {
182 $arrayRepresentation = [
183 'serializeClassName' => static::class,
184 'id' => $this->id
185 ];
186 if ($this->parentNode !== null) {
187 $arrayRepresentation['parentNode'] = $this->parentNode->toArray(false);
188 } else {
189 $arrayRepresentation['parentNode'] = '';
190 }
191 if ($this->hasChildNodes() && $addChildNodes) {
192 $arrayRepresentation['childNodes'] = $this->childNodes->toArray();
193 } else {
194 $arrayRepresentation['childNodes'] = '';
195 }
196 return $arrayRepresentation;
197 }
198
199 /**
200 * Sets data of the node by a given data array
201 *
202 * @param array $data
203 */
204 public function dataFromArray($data)
205 {
206 $this->setId($data['id']);
207 if (isset($data['parentNode']) && $data['parentNode'] !== '') {
208 $this->setParentNode(GeneralUtility::makeInstance($data['parentNode']['serializeClassName'], $data['parentNode']));
209 }
210 if (isset($data['childNodes']) && $data['childNodes'] !== '') {
211 $this->setChildNodes(GeneralUtility::makeInstance($data['childNodes']['serializeClassName'], $data['childNodes']));
212 }
213 }
214
215 /**
216 * Returns the serialized instance
217 *
218 * @return string
219 */
220 public function serialize()
221 {
222 return serialize($this->toArray());
223 }
224
225 /**
226 * Fills the current node with the given serialized informations
227 *
228 * @throws \TYPO3\CMS\Core\Exception if the deserialized object type is not identical to the current one
229 * @param string $serializedString
230 */
231 public function unserialize($serializedString)
232 {
233 $arrayRepresentation = unserialize($serializedString);
234 if ($arrayRepresentation['serializeClassName'] !== static::class) {
235 throw new \TYPO3\CMS\Core\Exception('Deserialized object type is not identical!', 1294586646);
236 }
237 $this->dataFromArray($arrayRepresentation);
238 }
239 }