[TASK] Streamline PHPDoc comment matches function/method signature
[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 * @param TreeNode $node
52 * @param TreeNode $otherNode
53 * @return int
54 */
55 public function nodeCompare(\TYPO3\CMS\Backend\Tree\TreeNode $node, \TYPO3\CMS\Backend\Tree\TreeNode $otherNode)
56 {
57 return $node->compareTo($otherNode);
58 }
59
60 /**
61 * Returns the serialized instance
62 *
63 * @return string
64 */
65 public function serialize()
66 {
67 return serialize($this->toArray());
68 }
69
70 /**
71 * Initializes the current object with a serialized instance
72 *
73 * @param string $serializedString
74 * @throws \TYPO3\CMS\Core\Exception if the deserialized is not identical to the current class
75 */
76 public function unserialize($serializedString)
77 {
78 $arrayRepresentation = unserialize($serializedString);
79 if ($arrayRepresentation['serializeClassName'] !== static::class) {
80 throw new \TYPO3\CMS\Core\Exception('Deserialized object type is not identical!', 1294586647);
81 }
82 $this->dataFromArray($arrayRepresentation);
83 }
84
85 /**
86 * Returns the collection in an array representation for e.g. serialization
87 *
88 * @return array
89 */
90 public function toArray()
91 {
92 $arrayRepresentation = [
93 'serializeClassName' => static::class
94 ];
95 $iterator = $this->getIterator();
96 while ($iterator->valid()) {
97 $arrayRepresentation[] = $iterator->current()->toArray();
98 $iterator->next();
99 }
100 return $arrayRepresentation;
101 }
102
103 /**
104 * Sets the data of the node collection by a given array
105 *
106 * @param array $data
107 */
108 public function dataFromArray($data)
109 {
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);
114 }
115 }
116 }