TYPO3 CMS  TYPO3_7-6
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 
66  public function setChildNodes(\TYPO3\CMS\Backend\Tree\TreeNodeCollection $childNodes)
67  {
68  $this->childNodes = $childNodes;
69  }
70 
76  public function removeChildNodes()
77  {
78  if ($this->childNodes !== null) {
79  unset($this->childNodes);
80  $this->childNodes = null;
81  }
82  }
83 
89  public function getChildNodes()
90  {
91  return $this->childNodes;
92  }
93 
99  public function hasChildNodes()
100  {
101  if ($this->childNodes !== null) {
102  return true;
103  }
104  return false;
105  }
106 
113  public function setId($id)
114  {
115  $this->id = $id;
116  }
117 
123  public function getId()
124  {
125  return $this->id;
126  }
127 
134  public function setParentNode(\TYPO3\CMS\Backend\Tree\TreeNode $parentNode = null)
135  {
136  $this->parentNode = $parentNode;
137  }
138 
144  public function getParentNode()
145  {
146  return $this->parentNode;
147  }
148 
155  public function equals(\TYPO3\CMS\Backend\Tree\TreeNode $other)
156  {
157  return $this->id == $other->getId();
158  }
159 
171  public function compareTo($other)
172  {
173  if ($this->equals($other)) {
174  return 0;
175  }
176  return $this->id > $other->getId() ? 1 : -1;
177  }
178 
185  public function toArray($addChildNodes = true)
186  {
187  $arrayRepresentation = [
188  'serializeClassName' => get_class($this),
189  'id' => $this->id
190  ];
191  if ($this->parentNode !== null) {
192  $arrayRepresentation['parentNode'] = $this->parentNode->toArray(false);
193  } else {
194  $arrayRepresentation['parentNode'] = '';
195  }
196  if ($this->hasChildNodes() && $addChildNodes) {
197  $arrayRepresentation['childNodes'] = $this->childNodes->toArray();
198  } else {
199  $arrayRepresentation['childNodes'] = '';
200  }
201  return $arrayRepresentation;
202  }
203 
210  public function dataFromArray($data)
211  {
212  $this->setId($data['id']);
213  if (isset($data['parentNode']) && $data['parentNode'] !== '') {
214  $this->setParentNode(GeneralUtility::makeInstance($data['parentNode']['serializeClassName'], $data['parentNode']));
215  }
216  if (isset($data['childNodes']) && $data['childNodes'] !== '') {
217  $this->setChildNodes(GeneralUtility::makeInstance($data['childNodes']['serializeClassName'], $data['childNodes']));
218  }
219  }
220 
226  public function serialize()
227  {
228  return serialize($this->toArray());
229  }
230 
238  public function unserialize($serializedString)
239  {
240  $arrayRepresentation = unserialize($serializedString);
241  if ($arrayRepresentation['serializeClassName'] !== get_class($this)) {
242  throw new \TYPO3\CMS\Core\Exception('Deserialized object type is not identical!', 1294586646);
243  }
244  $this->dataFromArray($arrayRepresentation);
245  }
246 }
equals(\TYPO3\CMS\Backend\Tree\TreeNode $other)
Definition: TreeNode.php:155
unserialize($serializedString)
Definition: TreeNode.php:238
setChildNodes(\TYPO3\CMS\Backend\Tree\TreeNodeCollection $childNodes)
Definition: TreeNode.php:66
toArray($addChildNodes=true)
Definition: TreeNode.php:185
__construct(array $data=[])
Definition: TreeNode.php:53
setParentNode(\TYPO3\CMS\Backend\Tree\TreeNode $parentNode=null)
Definition: TreeNode.php:134