‪TYPO3CMS  9.5
TreeNode.php
Go to the documentation of this file.
1 <?php
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;
37 
43  protected ‪$childNodes;
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' => 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 
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'] !== static::class) {
235  throw new \TYPO3\CMS\Core\Exception('Deserialized object type is not identical!', 1294586646);
236  }
237  $this->‪dataFromArray($arrayRepresentation);
238  }
239 }
‪TYPO3\CMS\Backend\Tree\TreeNode\equals
‪bool equals(\TYPO3\CMS\Backend\Tree\TreeNode $other)
Definition: TreeNode.php:147
‪TYPO3\CMS\Backend\Tree\TreeNode\setChildNodes
‪setChildNodes(\TYPO3\CMS\Backend\Tree\TreeNodeCollection $childNodes)
Definition: TreeNode.php:62
‪TYPO3\CMS\Backend\Tree\TreeNode\$childNodes
‪TYPO3 CMS Backend Tree TreeNodeCollection $childNodes
Definition: TreeNode.php:40
‪TYPO3\CMS\Backend\Tree\TreeNode\__construct
‪__construct(array $data=[])
Definition: TreeNode.php:50
‪TYPO3\CMS\Backend\Tree\TreeNode\getParentNode
‪TYPO3 CMS Backend Tree TreeNode getParentNode()
Definition: TreeNode.php:136
‪TYPO3
‪TYPO3\CMS\Backend\Tree
Definition: AbstractTree.php:2
‪TYPO3\CMS\Backend\Tree\TreeNode\hasChildNodes
‪bool hasChildNodes()
Definition: TreeNode.php:93
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection
Definition: TreeNodeCollection.php:21
‪TYPO3\CMS\Backend\Tree\TreeNode\removeChildNodes
‪removeChildNodes()
Definition: TreeNode.php:70
‪TYPO3\CMS\Backend\Tree\TreeNode\$parentNode
‪TYPO3 CMS Backend Tree TreeNode $parentNode
Definition: TreeNode.php:34
‪TYPO3\CMS\Backend\Tree\TreeNode
Definition: TreeNode.php:23
‪TYPO3\CMS\Backend\Tree\TreeNode\toArray
‪array toArray($addChildNodes=true)
Definition: TreeNode.php:177
‪TYPO3\CMS\Backend\Tree\TreeNode\dataFromArray
‪dataFromArray($data)
Definition: TreeNode.php:201
‪TYPO3\CMS\Backend\Tree\TreeNode\setParentNode
‪setParentNode(\TYPO3\CMS\Backend\Tree\TreeNode $parentNode=null)
Definition: TreeNode.php:126
‪TYPO3\CMS\Backend\Tree\TreeNode\$id
‪string $id
Definition: TreeNode.php:28
‪TYPO3\CMS\Backend\Tree\TreeNode\unserialize
‪unserialize($serializedString)
Definition: TreeNode.php:228
‪TYPO3\CMS\Backend\Tree\TreeNode\setId
‪setId($id)
Definition: TreeNode.php:106
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Tree\TreeNode\getId
‪string getId()
Definition: TreeNode.php:116
‪TYPO3\CMS\Backend\Tree\TreeNode\getChildNodes
‪TYPO3 CMS Backend Tree TreeNodeCollection getChildNodes()
Definition: TreeNode.php:83
‪TYPO3\CMS\Backend\Tree\TreeNode\compareTo
‪int compareTo($other)
Definition: TreeNode.php:163
‪TYPO3\CMS\Backend\Tree\TreeNode\serialize
‪string serialize()
Definition: TreeNode.php:217
‪TYPO3\CMS\Backend\Tree\ComparableNodeInterface
Definition: ComparableNodeInterface.php:21