TYPO3 CMS  TYPO3_7-6
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  if (!empty($data)) {
33  $this->dataFromArray($data);
34  }
35  }
36 
42  public function asort()
43  {
44  $this->uasort([$this, 'nodeCompare']);
45  }
46 
54  public function nodeCompare(\TYPO3\CMS\Backend\Tree\TreeNode $node, \TYPO3\CMS\Backend\Tree\TreeNode $otherNode)
55  {
56  return $node->compareTo($otherNode);
57  }
58 
64  public function serialize()
65  {
66  return serialize($this->toArray());
67  }
68 
76  public function unserialize($serializedString)
77  {
78  $arrayRepresentation = unserialize($serializedString);
79  if ($arrayRepresentation['serializeClassName'] !== get_class($this)) {
80  throw new \TYPO3\CMS\Core\Exception('Deserialized object type is not identical!', 1294586647);
81  }
82  $this->dataFromArray($arrayRepresentation);
83  }
84 
90  public function toArray()
91  {
92  $arrayRepresentation = [
93  'serializeClassName' => get_class($this)
94  ];
95  $iterator = $this->getIterator();
96  while ($iterator->valid()) {
97  $arrayRepresentation[] = $iterator->current()->toArray();
98  $iterator->next();
99  }
100  return $arrayRepresentation;
101  }
102 
109  public function dataFromArray($data)
110  {
111  unset($data['serializeClassName']);
112  foreach ($data as $index => $nodeArray) {
113  $node = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($nodeArray['serializeClassName'], $nodeArray);
114  $this->offsetSet($index, $node);
115  }
116  }
117 }
nodeCompare(\TYPO3\CMS\Backend\Tree\TreeNode $node, \TYPO3\CMS\Backend\Tree\TreeNode $otherNode)