‪TYPO3CMS  11.5
TreeNode.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 
24 class ‪TreeNode implements ‪ComparableNodeInterface, \Serializable
25 {
31  protected ‪$id = '';
32 
38  protected ‪$parentNode;
39 
45  protected ‪$childNodes;
46 
50  protected array ‪$additionalData = [];
51 
60  public function ‪__construct(array $data = [])
61  {
62  if ($data !== []) {
63  $this->‪dataFromArray($data);
64  }
65  }
66 
73  {
74  $this->childNodes = ‪$childNodes;
75  }
76 
80  public function ‪removeChildNodes()
81  {
82  if ($this->childNodes !== null) {
83  unset($this->childNodes);
84  $this->childNodes = null;
85  }
86  }
87 
93  public function ‪getChildNodes()
94  {
95  return ‪$this->childNodes;
96  }
97 
103  public function ‪hasChildNodes()
104  {
105  if ($this->childNodes !== null) {
106  return true;
107  }
108  return false;
109  }
110 
116  public function ‪setId(‪$id)
117  {
118  $this->id = ‪$id;
119  }
120 
126  public function ‪getId()
127  {
128  return ‪$this->id;
129  }
130 
136  public function ‪setParentNode(‪TreeNode ‪$parentNode = null)
137  {
138  $this->parentNode = ‪$parentNode;
139  }
140 
146  public function ‪getParentNode()
147  {
148  return ‪$this->parentNode;
149  }
150 
157  public function ‪equals(‪TreeNode $other)
158  {
159  return $this->id == $other->‪getId();
160  }
161 
173  public function ‪compareTo($other)
174  {
175  if ($this->‪equals($other)) {
176  return 0;
177  }
178  return $this->id > $other->‪getId() ? 1 : -1;
179  }
180 
184  public function ‪getAdditionalData(): array
185  {
187  }
188 
195  public function ‪toArray($addChildNodes = true)
196  {
197  $arrayRepresentation = [
198  'serializeClassName' => static::class,
199  'id' => ‪$this->id,
200  ];
201  if ($this->parentNode !== null) {
202  $arrayRepresentation['parentNode'] = $this->parentNode->toArray(false);
203  } else {
204  $arrayRepresentation['parentNode'] = '';
205  }
206  if ($this->‪hasChildNodes() && $addChildNodes) {
207  $arrayRepresentation['childNodes'] = $this->childNodes->toArray();
208  } else {
209  $arrayRepresentation['childNodes'] = '';
210  }
211  return $arrayRepresentation;
212  }
213 
219  public function ‪dataFromArray($data)
220  {
221  $this->‪setId($data['id'] ?? $data['uid']);
222  if (isset($data['parentNode']) && $data['parentNode'] !== '') {
224  ‪$parentNode = GeneralUtility::makeInstance($data['parentNode']['serializeClassName'], $data['parentNode']);
226  }
227  if (isset($data['childNodes']) && $data['childNodes'] !== '') {
229  ‪$childNodes = GeneralUtility::makeInstance($data['childNodes']['serializeClassName'], $data['childNodes']);
230  $this->‪setChildNodes($childNodes);
231  }
232  // @todo: This is part of the category tree performance hack
233  $this->additionalData = $data;
234  }
235 
242  public function ‪serialize()
243  {
244  return ‪serialize($this->‪__serialize());
245  }
246 
250  public function ‪__serialize(): array
251  {
252  return $this->‪toArray();
253  }
254 
262  public function ‪unserialize($serializedString)
263  {
264  $this->‪__unserialize(‪unserialize($serializedString));
265  }
266 
272  public function ‪__unserialize(array $arrayRepresentation): void
273  {
274  if ($arrayRepresentation['serializeClassName'] !== static::class) {
275  throw new ‪Exception('Deserialized object type is not identical!', 1294586646);
276  }
277  $this->‪dataFromArray($arrayRepresentation);
278  }
279 }
‪TYPO3\CMS\Backend\Tree\TreeNode\__serialize
‪__serialize()
Definition: TreeNode.php:247
‪TYPO3\CMS\Backend\Tree\TreeNode\__unserialize
‪__unserialize(array $arrayRepresentation)
Definition: TreeNode.php:269
‪TYPO3\CMS\Backend\Tree\TreeNode\setChildNodes
‪setChildNodes(TreeNodeCollection $childNodes)
Definition: TreeNode.php:69
‪TYPO3\CMS\Backend\Tree\TreeNode\__construct
‪__construct(array $data=[])
Definition: TreeNode.php:57
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Backend\Tree
Definition: AbstractTree.php:16
‪TYPO3\CMS\Backend\Tree\TreeNode\hasChildNodes
‪bool hasChildNodes()
Definition: TreeNode.php:100
‪TYPO3\CMS\Backend\Tree\TreeNode\$id
‪string int $id
Definition: TreeNode.php:30
‪TYPO3\CMS\Backend\Tree\TreeNode\getAdditionalData
‪getAdditionalData()
Definition: TreeNode.php:181
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection
Definition: TreeNodeCollection.php:25
‪TYPO3\CMS\Backend\Tree\TreeNode\setParentNode
‪setParentNode(TreeNode $parentNode=null)
Definition: TreeNode.php:133
‪TYPO3\CMS\Backend\Tree\TreeNode\removeChildNodes
‪removeChildNodes()
Definition: TreeNode.php:77
‪TYPO3\CMS\Backend\Tree\TreeNode
Definition: TreeNode.php:25
‪TYPO3\CMS\Backend\Tree\TreeNode\toArray
‪array toArray($addChildNodes=true)
Definition: TreeNode.php:192
‪TYPO3\CMS\Backend\Tree\TreeNode\$parentNode
‪TreeNode null $parentNode
Definition: TreeNode.php:36
‪TYPO3\CMS\Backend\Tree\TreeNode\dataFromArray
‪dataFromArray($data)
Definition: TreeNode.php:216
‪TYPO3\CMS\Backend\Tree\TreeNode\getId
‪string int getId()
Definition: TreeNode.php:123
‪TYPO3\CMS\Backend\Tree\TreeNode\equals
‪bool equals(TreeNode $other)
Definition: TreeNode.php:154
‪TYPO3\CMS\Backend\Tree\TreeNode\getParentNode
‪TreeNode getParentNode()
Definition: TreeNode.php:143
‪TYPO3\CMS\Backend\Tree\TreeNode\unserialize
‪unserialize($serializedString)
Definition: TreeNode.php:259
‪TYPO3\CMS\Backend\Tree\TreeNode\$additionalData
‪array $additionalData
Definition: TreeNode.php:47
‪TYPO3\CMS\Backend\Tree\TreeNode\getChildNodes
‪TreeNodeCollection getChildNodes()
Definition: TreeNode.php:90
‪TYPO3\CMS\Backend\Tree\TreeNode\setId
‪setId($id)
Definition: TreeNode.php:113
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Tree\TreeNode\compareTo
‪int compareTo($other)
Definition: TreeNode.php:170
‪TYPO3\CMS\Backend\Tree\TreeNode\serialize
‪string serialize()
Definition: TreeNode.php:239
‪TYPO3\CMS\Backend\Tree\TreeNode\$childNodes
‪TreeNodeCollection null $childNodes
Definition: TreeNode.php:42
‪TYPO3\CMS\Backend\Tree\ComparableNodeInterface
Definition: ComparableNodeInterface.php:22