‪TYPO3CMS  10.4
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 
55  public function ‪__construct(array $data = [])
56  {
57  if (!empty($data)) {
58  $this->‪dataFromArray($data);
59  }
60  }
61 
68  {
69  $this->childNodes = ‪$childNodes;
70  }
71 
75  public function ‪removeChildNodes()
76  {
77  if ($this->childNodes !== null) {
78  unset($this->childNodes);
79  $this->childNodes = null;
80  }
81  }
82 
88  public function ‪getChildNodes()
89  {
90  return ‪$this->childNodes;
91  }
92 
98  public function ‪hasChildNodes()
99  {
100  if ($this->childNodes !== null) {
101  return true;
102  }
103  return false;
104  }
105 
111  public function ‪setId(‪$id)
112  {
113  $this->id = ‪$id;
114  }
115 
121  public function ‪getId()
122  {
123  return ‪$this->id;
124  }
125 
131  public function ‪setParentNode(\‪TYPO3\CMS\Backend\Tree\‪TreeNode ‪$parentNode = null)
132  {
133  $this->parentNode = ‪$parentNode;
134  }
135 
141  public function ‪getParentNode()
142  {
143  return ‪$this->parentNode;
144  }
145 
152  public function ‪equals(\‪TYPO3\CMS\Backend\Tree\‪TreeNode $other)
153  {
154  return $this->id == $other->getId();
155  }
156 
168  public function ‪compareTo($other)
169  {
170  if ($this->‪equals($other)) {
171  return 0;
172  }
173  return $this->id > $other->getId() ? 1 : -1;
174  }
175 
182  public function ‪toArray($addChildNodes = true)
183  {
184  $arrayRepresentation = [
185  'serializeClassName' => static::class,
186  'id' => ‪$this->id
187  ];
188  if ($this->parentNode !== null) {
189  $arrayRepresentation['parentNode'] = $this->parentNode->toArray(false);
190  } else {
191  $arrayRepresentation['parentNode'] = '';
192  }
193  if ($this->‪hasChildNodes() && $addChildNodes) {
194  $arrayRepresentation['childNodes'] = $this->childNodes->toArray();
195  } else {
196  $arrayRepresentation['childNodes'] = '';
197  }
198  return $arrayRepresentation;
199  }
200 
206  public function ‪dataFromArray($data)
207  {
208  $this->‪setId($data['id']);
209  if (isset($data['parentNode']) && $data['parentNode'] !== '') {
211  ‪$parentNode = GeneralUtility::makeInstance($data['parentNode']['serializeClassName'], $data['parentNode']);
213  }
214  if (isset($data['childNodes']) && $data['childNodes'] !== '') {
216  ‪$childNodes = GeneralUtility::makeInstance($data['childNodes']['serializeClassName'], $data['childNodes']);
217  $this->‪setChildNodes($childNodes);
218  }
219  }
220 
226  public function ‪serialize()
227  {
228  return ‪serialize($this->‪toArray());
229  }
230 
237  public function ‪unserialize($serializedString)
238  {
239  $arrayRepresentation = ‪unserialize($serializedString);
240  if ($arrayRepresentation['serializeClassName'] !== static::class) {
241  throw new ‪Exception('Deserialized object type is not identical!', 1294586646);
242  }
243  $this->‪dataFromArray($arrayRepresentation);
244  }
245 }
‪TYPO3\CMS\Backend\Tree\TreeNode\equals
‪bool equals(\TYPO3\CMS\Backend\Tree\TreeNode $other)
Definition: TreeNode.php:149
‪TYPO3\CMS\Backend\Tree\TreeNode\$childNodes
‪TYPO3 CMS Backend Tree TreeNodeCollection $childNodes
Definition: TreeNode.php:42
‪TYPO3\CMS\Backend\Tree\TreeNode\setChildNodes
‪setChildNodes(TreeNodeCollection $childNodes)
Definition: TreeNode.php:64
‪TYPO3\CMS\Backend\Tree\TreeNode\__construct
‪__construct(array $data=[])
Definition: TreeNode.php:52
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Backend\Tree\TreeNode\getParentNode
‪TYPO3 CMS Backend Tree TreeNode getParentNode()
Definition: TreeNode.php:138
‪TYPO3
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Backend\Tree
Definition: AbstractTree.php:16
‪TYPO3\CMS\Backend\Tree\TreeNode\hasChildNodes
‪bool hasChildNodes()
Definition: TreeNode.php:95
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection
Definition: TreeNodeCollection.php:25
‪TYPO3\CMS\Backend\Tree\TreeNode\removeChildNodes
‪removeChildNodes()
Definition: TreeNode.php:72
‪TYPO3\CMS\Backend\Tree\TreeNode\$parentNode
‪TYPO3 CMS Backend Tree TreeNode $parentNode
Definition: TreeNode.php:36
‪TYPO3\CMS\Backend\Tree\TreeNode
Definition: TreeNode.php:25
‪TYPO3\CMS\Backend\Tree\TreeNode\toArray
‪array toArray($addChildNodes=true)
Definition: TreeNode.php:179
‪TYPO3\CMS\Backend\Tree\TreeNode\dataFromArray
‪dataFromArray($data)
Definition: TreeNode.php:203
‪TYPO3\CMS\Backend\Tree\TreeNode\setParentNode
‪setParentNode(\TYPO3\CMS\Backend\Tree\TreeNode $parentNode=null)
Definition: TreeNode.php:128
‪TYPO3\CMS\Backend\Tree\TreeNode\$id
‪string $id
Definition: TreeNode.php:30
‪TYPO3\CMS\Backend\Tree\TreeNode\unserialize
‪unserialize($serializedString)
Definition: TreeNode.php:234
‪TYPO3\CMS\Backend\Tree\TreeNode\setId
‪setId($id)
Definition: TreeNode.php:108
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Tree\TreeNode\getId
‪string getId()
Definition: TreeNode.php:118
‪TYPO3\CMS\Backend\Tree\TreeNode\getChildNodes
‪TYPO3 CMS Backend Tree TreeNodeCollection getChildNodes()
Definition: TreeNode.php:85
‪TYPO3\CMS\Backend\Tree\TreeNode\compareTo
‪int compareTo($other)
Definition: TreeNode.php:165
‪TYPO3\CMS\Backend\Tree\TreeNode\serialize
‪string serialize()
Definition: TreeNode.php:223
‪TYPO3\CMS\Backend\Tree\ComparableNodeInterface
Definition: ComparableNodeInterface.php:22