2 namespace TYPO3\CMS\Backend\Tree
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Core\Utility\GeneralUtility
;
22 class TreeNode
implements \TYPO3\CMS\Backend\Tree\ComparableNodeInterface
, \Serializable
34 * @var \TYPO3\CMS\Backend\Tree\TreeNode
36 protected $parentNode = null
;
41 * @var \TYPO3\CMS\Backend\Tree\TreeNodeCollection
43 protected $childNodes = null
;
48 * You can move an initial data array to initialize the instance and further objects.
49 * This is useful for the deserialization.
53 public function __construct(array $data = [])
56 $this->dataFromArray($data);
61 * Sets the child nodes collection
63 * @param \TYPO3\CMS\Backend\Tree\TreeNodeCollection $childNodes
65 public function setChildNodes(\TYPO3\CMS\Backend\Tree\TreeNodeCollection
$childNodes)
67 $this->childNodes
= $childNodes;
71 * Removes child nodes collection
73 public function removeChildNodes()
75 if ($this->childNodes
!== null
) {
76 unset($this->childNodes
);
77 $this->childNodes
= null
;
82 * Returns child nodes collection
84 * @return \TYPO3\CMS\Backend\Tree\TreeNodeCollection
86 public function getChildNodes()
88 return $this->childNodes
;
92 * Returns TRUE if the node has child nodes attached
96 public function hasChildNodes()
98 if ($this->childNodes
!== null
) {
105 * Sets the identifier
109 public function setId($id)
115 * Returns the identifier
119 public function getId()
125 * Sets the parent node
127 * @param null|\TYPO3\CMS\Backend\Tree\TreeNode $parentNode
129 public function setParentNode(\TYPO3\CMS\Backend\Tree\TreeNode
$parentNode = null
)
131 $this->parentNode
= $parentNode;
135 * Returns the parent node
137 * @return \TYPO3\CMS\Backend\Tree\TreeNode
139 public function getParentNode()
141 return $this->parentNode
;
145 * Compares a node if it's identical to another node by the id property.
147 * @param \TYPO3\CMS\Backend\Tree\TreeNode $other
150 public function equals(\TYPO3\CMS\Backend\Tree\TreeNode
$other)
152 return $this->id
== $other->getId();
156 * Compares a node to another one.
159 * 1 if its greater than the other one
160 * -1 if its smaller than the other one
163 * @param \TYPO3\CMS\Backend\Tree\TreeNode $other
164 * @return int See description above
166 public function compareTo($other)
168 if ($this->equals($other)) {
171 return $this->id
> $other->getId() ?
1 : -1;
175 * Returns the node in an array representation that can be used for serialization
177 * @param bool $addChildNodes
180 public function toArray($addChildNodes = true
)
182 $arrayRepresentation = [
183 'serializeClassName' => get_class($this),
186 if ($this->parentNode
!== null
) {
187 $arrayRepresentation['parentNode'] = $this->parentNode
->toArray(false
);
189 $arrayRepresentation['parentNode'] = '';
191 if ($this->hasChildNodes() && $addChildNodes) {
192 $arrayRepresentation['childNodes'] = $this->childNodes
->toArray();
194 $arrayRepresentation['childNodes'] = '';
196 return $arrayRepresentation;
200 * Sets data of the node by a given data array
204 public function dataFromArray($data)
206 $this->setId($data['id']);
207 if (isset($data['parentNode']) && $data['parentNode'] !== '') {
208 $this->setParentNode(GeneralUtility
::makeInstance($data['parentNode']['serializeClassName'], $data['parentNode']));
210 if (isset($data['childNodes']) && $data['childNodes'] !== '') {
211 $this->setChildNodes(GeneralUtility
::makeInstance($data['childNodes']['serializeClassName'], $data['childNodes']));
216 * Returns the serialized instance
220 public function serialize()
222 return serialize($this->toArray());
226 * Fills the current node with the given serialized informations
228 * @throws \TYPO3\CMS\Core\Exception if the deserialized object type is not identical to the current one
229 * @param string $serializedString
231 public function unserialize($serializedString)
233 $arrayRepresentation = unserialize($serializedString);
234 if ($arrayRepresentation['serializeClassName'] !== get_class($this)) {
235 throw new \TYPO3\CMS\Core\
Exception('Deserialized object type is not identical!', 1294586646);
237 $this->dataFromArray($arrayRepresentation);