TYPO3 CMS  TYPO3_8-7
TreeNode.php
Go to the documentation of this file.
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 
18 
23 {
29  protected $id = '';
30 
36  protected $parentNode = null;
37 
43  protected $childNodes = null;
44 
53  public function __construct(array $data = [])
54  {
55  if (!empty($data)) {
56  $this->dataFromArray($data);
57  }
58  }
59 
65  public function setChildNodes(\TYPO3\CMS\Backend\Tree\TreeNodeCollection $childNodes)
66  {
67  $this->childNodes = $childNodes;
68  }
69 
73  public function removeChildNodes()
74  {
75  if ($this->childNodes !== null) {
76  unset($this->childNodes);
77  $this->childNodes = null;
78  }
79  }
80 
86  public function getChildNodes()
87  {
88  return $this->childNodes;
89  }
90 
96  public function hasChildNodes()
97  {
98  if ($this->childNodes !== null) {
99  return true;
100  }
101  return false;
102  }
103 
109  public function setId($id)
110  {
111  $this->id = $id;
112  }
113 
119  public function getId()
120  {
121  return $this->id;
122  }
123 
129  public function setParentNode(\TYPO3\CMS\Backend\Tree\TreeNode $parentNode = null)
130  {
131  $this->parentNode = $parentNode;
132  }
133 
139  public function getParentNode()
140  {
141  return $this->parentNode;
142  }
143 
150  public function equals(\TYPO3\CMS\Backend\Tree\TreeNode $other)
151  {
152  return $this->id == $other->getId();
153  }
154 
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 
180  public function toArray($addChildNodes = true)
181  {
182  $arrayRepresentation = [
183  'serializeClassName' => get_class($this),
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 
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 
220  public function serialize()
221  {
222  return serialize($this->toArray());
223  }
224 
231  public function unserialize($serializedString)
232  {
233  $arrayRepresentation = unserialize($serializedString);
234  if ($arrayRepresentation['serializeClassName'] !== get_class($this)) {
235  throw new \TYPO3\CMS\Core\Exception('Deserialized object type is not identical!', 1294586646);
236  }
237  $this->dataFromArray($arrayRepresentation);
238  }
239 }
equals(\TYPO3\CMS\Backend\Tree\TreeNode $other)
Definition: TreeNode.php:150
static makeInstance($className,... $constructorArguments)
unserialize($serializedString)
Definition: TreeNode.php:231
setChildNodes(\TYPO3\CMS\Backend\Tree\TreeNodeCollection $childNodes)
Definition: TreeNode.php:65
toArray($addChildNodes=true)
Definition: TreeNode.php:180
__construct(array $data=[])
Definition: TreeNode.php:53
setParentNode(\TYPO3\CMS\Backend\Tree\TreeNode $parentNode=null)
Definition: TreeNode.php:129