TYPO3 CMS  TYPO3_8-7
TreeNodeCollection.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 
20 class TreeNodeCollection extends \ArrayObject
21 {
30  public function __construct(array $data = [])
31  {
32  parent::__construct();
33  if (!empty($data)) {
34  $this->dataFromArray($data);
35  }
36  }
37 
41  public function asort()
42  {
43  $this->uasort([$this, 'nodeCompare']);
44  }
45 
52  public function nodeCompare(\TYPO3\CMS\Backend\Tree\TreeNode $node, \TYPO3\CMS\Backend\Tree\TreeNode $otherNode)
53  {
54  return $node->compareTo($otherNode);
55  }
56 
62  public function serialize()
63  {
64  return serialize($this->toArray());
65  }
66 
73  public function unserialize($serializedString)
74  {
75  $arrayRepresentation = unserialize($serializedString);
76  if ($arrayRepresentation['serializeClassName'] !== get_class($this)) {
77  throw new \TYPO3\CMS\Core\Exception('Deserialized object type is not identical!', 1294586647);
78  }
79  $this->dataFromArray($arrayRepresentation);
80  }
81 
87  public function toArray()
88  {
89  $arrayRepresentation = [
90  'serializeClassName' => get_class($this)
91  ];
92  $iterator = $this->getIterator();
93  while ($iterator->valid()) {
94  $arrayRepresentation[] = $iterator->current()->toArray();
95  $iterator->next();
96  }
97  return $arrayRepresentation;
98  }
99 
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 }
static makeInstance($className,... $constructorArguments)
nodeCompare(\TYPO3\CMS\Backend\Tree\TreeNode $node, \TYPO3\CMS\Backend\Tree\TreeNode $otherNode)