‪TYPO3CMS  10.4
TreeNodeCollection.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 
24 class ‪TreeNodeCollection extends \ArrayObject
25 {
34  public function ‪__construct(array $data = [])
35  {
36  parent::__construct();
37  if (!empty($data)) {
38  $this->‪dataFromArray($data);
39  }
40  }
41 
45  public function ‪asort()
46  {
47  $this->uasort([$this, 'nodeCompare']);
48  }
49 
59  public function ‪nodeCompare(‪TreeNode $node, ‪TreeNode $otherNode)
60  {
61  return $node->‪compareTo($otherNode);
62  }
63 
69  public function ‪serialize()
70  {
71  return ‪serialize($this->‪toArray());
72  }
73 
80  public function ‪unserialize($serializedString)
81  {
82  $arrayRepresentation = ‪unserialize($serializedString);
83  if ($arrayRepresentation['serializeClassName'] !== static::class) {
84  throw new ‪Exception('Deserialized object type is not identical!', 1294586647);
85  }
86  $this->‪dataFromArray($arrayRepresentation);
87  }
88 
94  public function ‪toArray()
95  {
96  $arrayRepresentation = [
97  'serializeClassName' => static::class
98  ];
99  $iterator = $this->getIterator();
100  while ($iterator->valid()) {
101  $arrayRepresentation[] = $iterator->current()->toArray();
102  $iterator->next();
103  }
104  return $arrayRepresentation;
105  }
106 
112  public function ‪dataFromArray($data)
113  {
114  unset($data['serializeClassName']);
115  foreach ($data as $index => $nodeArray) {
116  $node = GeneralUtility::makeInstance($nodeArray['serializeClassName'], $nodeArray);
117  $this->offsetSet($index, $node);
118  }
119  }
120 }
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection\__construct
‪__construct(array $data=[])
Definition: TreeNodeCollection.php:34
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection\serialize
‪string serialize()
Definition: TreeNodeCollection.php:69
‪TYPO3\CMS\Backend\Tree
Definition: AbstractTree.php:16
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection\nodeCompare
‪int nodeCompare(TreeNode $node, TreeNode $otherNode)
Definition: TreeNodeCollection.php:59
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection\unserialize
‪unserialize($serializedString)
Definition: TreeNodeCollection.php:80
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection
Definition: TreeNodeCollection.php:25
‪TYPO3\CMS\Backend\Tree\TreeNode
Definition: TreeNode.php:25
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection\dataFromArray
‪dataFromArray($data)
Definition: TreeNodeCollection.php:112
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Tree\TreeNode\compareTo
‪int compareTo($other)
Definition: TreeNode.php:165
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection\asort
‪asort()
Definition: TreeNodeCollection.php:45
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection\toArray
‪array toArray()
Definition: TreeNodeCollection.php:94