[TASK] Clean up the phpDoc of t3lib/tree/
[Packages/TYPO3.CMS.git] / t3lib / tree / class.t3lib_tree_nodecollection.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 TYPO3 Tree Team <http://forge.typo3.org/projects/typo3v4-extjstrees>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Tree Node Collection
30 *
31 * @author Stefan Galinski <stefan.galinski@gmail.com>
32 * @author Steffen Ritter <info@steffen-ritter.net>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36 class t3lib_tree_NodeCollection extends ArrayObject {
37 /**
38 * Constructor
39 *
40 * You can move an initial data array to initialize the instance and further objects.
41 * This is useful for the deserialization.
42 *
43 * @param array $data
44 */
45 public function __construct(array $data = array()) {
46 if (count($data)) {
47 $this->dataFromArray($data);
48 }
49 }
50
51 /**
52 * Sorts the internal nodes array
53 *
54 * @return void
55 */
56 public function asort() {
57 $this->uasort(array($this, 'nodeCompare'));
58 }
59
60 /**
61 * Compares a node with another one
62 *
63 * @see t3lib_tree_Node::compareTo
64 * @return void
65 * @noapi
66 */
67 public function nodeCompare(t3lib_tree_Node $node, t3lib_tree_Node $otherNode) {
68 return $node->compareTo($otherNode);
69 }
70
71 /**
72 * Returns the serialized instance
73 *
74 * @return string
75 */
76 public function serialize() {
77 return serialize($this->toArray());
78 }
79
80 /**
81 * Initializes the current object with a serialized instance
82 *
83 * @param string $serializedString
84 * @return void
85 * @throws t3lib_exception if the deserialized is not identical to the current class
86 */
87 public function unserialize($serializedString) {
88 $arrayRepresentation = unserialize($serializedString);
89 if ($arrayRepresentation['serializeClassName'] !== get_class($this)) {
90 throw new t3lib_exception('Deserialized object type is not identical!', 1294586647);
91 }
92 $this->dataFromArray($arrayRepresentation);
93 }
94
95 /**
96 * Returns the collection in an array representation for e.g. serialization
97 *
98 * @return array
99 */
100 public function toArray() {
101 $arrayRepresentation = array(
102 'serializeClassName' => get_class($this),
103 );
104
105 $iterator = $this->getIterator();
106 while ($iterator->valid()) {
107 $arrayRepresentation[] = $iterator->current()->toArray();
108 $iterator->next();
109 }
110
111 return $arrayRepresentation;
112 }
113
114 /**
115 * Sets the data of the node collection by a given array
116 *
117 * @param array $data
118 * @return void
119 */
120 public function dataFromArray($data) {
121 unset($data['serializeClassName']);
122 foreach ($data as $index => $nodeArray) {
123 $node = t3lib_div::makeInstance($nodeArray['serializeClassName'], $nodeArray);
124 $this->offsetSet($index, $node);
125 }
126 }
127 }
128
129 ?>