[CLEANUP] Remove/Replace non-standard phpdoc annotations
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Tree / TreeNodeCollection.php
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
17 /**
18 * Tree Node Collection
19 */
20 class TreeNodeCollection extends \ArrayObject
21 {
22 /**
23 * Constructor
24 *
25 * You can move an initial data array to initialize the instance and further objects.
26 * This is useful for the deserialization.
27 *
28 * @param array $data
29 */
30 public function __construct(array $data = [])
31 {
32 parent::__construct();
33 if (!empty($data)) {
34 $this->dataFromArray($data);
35 }
36 }
37
38 /**
39 * Sorts the internal nodes array
40 */
41 public function asort()
42 {
43 $this->uasort([$this, 'nodeCompare']);
44 }
45
46 /**
47 * Compares a node with another one
48 *
49 * @see \TYPO3\CMS\Backend\Tree\TreeNode::compareTo
50 * @internal
51 */
52 public function nodeCompare(\TYPO3\CMS\Backend\Tree\TreeNode $node, \TYPO3\CMS\Backend\Tree\TreeNode $otherNode)
53 {
54 return $node->compareTo($otherNode);
55 }
56
57 /**
58 * Returns the serialized instance
59 *
60 * @return string
61 */
62 public function serialize()
63 {
64 return serialize($this->toArray());
65 }
66
67 /**
68 * Initializes the current object with a serialized instance
69 *
70 * @param string $serializedString
71 * @throws \TYPO3\CMS\Core\Exception if the deserialized is not identical to the current class
72 */
73 public function unserialize($serializedString)
74 {
75 $arrayRepresentation = unserialize($serializedString);
76 if ($arrayRepresentation['serializeClassName'] !== static::class) {
77 throw new \TYPO3\CMS\Core\Exception('Deserialized object type is not identical!', 1294586647);
78 }
79 $this->dataFromArray($arrayRepresentation);
80 }
81
82 /**
83 * Returns the collection in an array representation for e.g. serialization
84 *
85 * @return array
86 */
87 public function toArray()
88 {
89 $arrayRepresentation = [
90 'serializeClassName' => static::class
91 ];
92 $iterator = $this->getIterator();
93 while ($iterator->valid()) {
94 $arrayRepresentation[] = $iterator->current()->toArray();
95 $iterator->next();
96 }
97 return $arrayRepresentation;
98 }
99
100 /**
101 * Sets the data of the node collection by a given array
102 *
103 * @param array $data
104 */
105 public function dataFromArray($data)
106 {
107 unset($data['serializeClassName']);
108 foreach ($data as $index => $nodeArray) {
109 $node = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($nodeArray['serializeClassName'], $nodeArray);
110 $this->offsetSet($index, $node);
111 }
112 }
113 }