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!
18 * Tree Node Collection
20 class TreeNodeCollection
extends \ArrayObject
25 * You can move an initial data array to initialize the instance and further objects.
26 * This is useful for the deserialization.
30 public function __construct(array $data = [])
32 parent
::__construct();
34 $this->dataFromArray($data);
39 * Sorts the internal nodes array
41 public function asort()
43 $this->uasort([$this, 'nodeCompare']);
47 * Compares a node with another one
49 * @see \TYPO3\CMS\Backend\Tree\TreeNode::compareTo
51 * @param TreeNode $node
52 * @param TreeNode $otherNode
55 public function nodeCompare(\TYPO3\CMS\Backend\Tree\TreeNode
$node, \TYPO3\CMS\Backend\Tree\TreeNode
$otherNode)
57 return $node->compareTo($otherNode);
61 * Returns the serialized instance
65 public function serialize()
67 return serialize($this->toArray());
71 * Initializes the current object with a serialized instance
73 * @param string $serializedString
74 * @throws \TYPO3\CMS\Core\Exception if the deserialized is not identical to the current class
76 public function unserialize($serializedString)
78 $arrayRepresentation = unserialize($serializedString);
79 if ($arrayRepresentation['serializeClassName'] !== static::class) {
80 throw new \TYPO3\CMS\Core\
Exception('Deserialized object type is not identical!', 1294586647);
82 $this->dataFromArray($arrayRepresentation);
86 * Returns the collection in an array representation for e.g. serialization
90 public function toArray()
92 $arrayRepresentation = [
93 'serializeClassName' => static::class
95 $iterator = $this->getIterator();
96 while ($iterator->valid()) {
97 $arrayRepresentation[] = $iterator->current()->toArray();
100 return $arrayRepresentation;
104 * Sets the data of the node collection by a given array
108 public function dataFromArray($data)
110 unset($data['serializeClassName']);
111 foreach ($data as $index => $nodeArray) {
112 $node = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance($nodeArray['serializeClassName'], $nodeArray);
113 $this->offsetSet($index, $node);