‪TYPO3CMS  ‪main
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 
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 
71  {
72  $this->childNodes = ‪$childNodes;
73  }
74 
78  public function ‪removeChildNodes()
79  {
80  if ($this->childNodes !== null) {
81  unset($this->childNodes);
82  $this->childNodes = null;
83  }
84  }
85 
91  public function ‪getChildNodes()
92  {
93  return ‪$this->childNodes;
94  }
95 
101  public function ‪hasChildNodes()
102  {
103  if ($this->childNodes !== null) {
104  return true;
105  }
106  return false;
107  }
108 
114  public function ‪setId(‪$id)
115  {
116  $this->id = ‪$id;
117  }
118 
124  public function ‪getId()
125  {
126  return ‪$this->id;
127  }
128 
134  public function ‪setParentNode(‪TreeNode ‪$parentNode = null)
135  {
136  $this->parentNode = ‪$parentNode;
137  }
138 
144  public function ‪getParentNode()
145  {
146  return ‪$this->parentNode;
147  }
148 
154  public function ‪equals(‪TreeNode $other)
155  {
156  return $this->id == $other->‪getId();
157  }
158 
170  public function ‪compareTo($other)
171  {
172  if ($this->‪equals($other)) {
173  return 0;
174  }
175  return $this->id > $other->‪getId() ? 1 : -1;
176  }
177 
181  public function ‪getAdditionalData(): array
182  {
184  }
185 
192  public function ‪toArray($addChildNodes = true)
193  {
194  $arrayRepresentation = [
195  'serializeClassName' => static::class,
196  'id' => ‪$this->id,
197  ];
198  if ($this->parentNode !== null) {
199  $arrayRepresentation['parentNode'] = $this->parentNode->toArray(false);
200  } else {
201  $arrayRepresentation['parentNode'] = '';
202  }
203  if ($this->‪hasChildNodes() && $addChildNodes) {
204  $arrayRepresentation['childNodes'] = $this->childNodes->toArray();
205  } else {
206  $arrayRepresentation['childNodes'] = '';
207  }
208  return $arrayRepresentation;
209  }
210 
216  public function ‪dataFromArray($data)
217  {
218  $this->‪setId($data['id'] ?? $data['uid']);
219  if (isset($data['parentNode']) && $data['parentNode'] !== '') {
221  ‪$parentNode = GeneralUtility::makeInstance($data['parentNode']['serializeClassName'], $data['parentNode']);
223  }
224  if (isset($data['childNodes']) && $data['childNodes'] !== '') {
226  ‪$childNodes = GeneralUtility::makeInstance($data['childNodes']['serializeClassName'], $data['childNodes']);
227  $this->‪setChildNodes($childNodes);
228  }
229  // @todo: This is part of the category tree performance hack
230  $this->additionalData = $data;
231  }
232 
236  public function ‪__serialize(): array
237  {
238  return $this->‪toArray();
239  }
240 
246  public function ‪__unserialize(array $arrayRepresentation): void
247  {
248  if ($arrayRepresentation['serializeClassName'] !== static::class) {
249  throw new ‪Exception('Deserialized object type is not identical!', 1294586646);
250  }
251  $this->‪dataFromArray($arrayRepresentation);
252  }
253 }
‪TYPO3\CMS\Backend\Tree\TreeNode\__serialize
‪__serialize()
Definition: TreeNode.php:233
‪TYPO3\CMS\Backend\Tree\TreeNode\__unserialize
‪__unserialize(array $arrayRepresentation)
Definition: TreeNode.php:243
‪TYPO3\CMS\Backend\Tree\TreeNode\setChildNodes
‪setChildNodes(TreeNodeCollection $childNodes)
Definition: TreeNode.php:67
‪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:98
‪TYPO3\CMS\Backend\Tree\TreeNode\$id
‪string int $id
Definition: TreeNode.php:30
‪TYPO3\CMS\Backend\Tree\TreeNode\getAdditionalData
‪getAdditionalData()
Definition: TreeNode.php:178
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection
Definition: TreeNodeCollection.php:25
‪TYPO3\CMS\Backend\Tree\TreeNode\setParentNode
‪setParentNode(TreeNode $parentNode=null)
Definition: TreeNode.php:131
‪TYPO3\CMS\Backend\Tree\TreeNode\removeChildNodes
‪removeChildNodes()
Definition: TreeNode.php:75
‪TYPO3\CMS\Backend\Tree\TreeNode
Definition: TreeNode.php:25
‪TYPO3\CMS\Backend\Tree\TreeNode\toArray
‪array toArray($addChildNodes=true)
Definition: TreeNode.php:189
‪TYPO3\CMS\Backend\Tree\TreeNode\$parentNode
‪TreeNode null $parentNode
Definition: TreeNode.php:36
‪TYPO3\CMS\Backend\Tree\TreeNode\dataFromArray
‪dataFromArray($data)
Definition: TreeNode.php:213
‪TYPO3\CMS\Backend\Tree\TreeNode\getId
‪string int getId()
Definition: TreeNode.php:121
‪TYPO3\CMS\Backend\Tree\TreeNode\equals
‪bool equals(TreeNode $other)
Definition: TreeNode.php:151
‪TYPO3\CMS\Backend\Tree\TreeNode\getParentNode
‪TreeNode getParentNode()
Definition: TreeNode.php:141
‪TYPO3\CMS\Backend\Tree\TreeNode\$additionalData
‪array $additionalData
Definition: TreeNode.php:47
‪TYPO3\CMS\Backend\Tree\TreeNode\getChildNodes
‪TreeNodeCollection getChildNodes()
Definition: TreeNode.php:88
‪TYPO3\CMS\Backend\Tree\TreeNode\setId
‪setId($id)
Definition: TreeNode.php:111
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Tree\TreeNode\compareTo
‪int compareTo($other)
Definition: TreeNode.php:167
‪TYPO3\CMS\Backend\Tree\TreeNode\$childNodes
‪TreeNodeCollection null $childNodes
Definition: TreeNode.php:42
‪TYPO3\CMS\Backend\Tree\ComparableNodeInterface
Definition: ComparableNodeInterface.php:22