TYPO3 API  SVNRelease
class.t3lib_tree_nodecollection.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 TYPO3 Tree Team <http://forge.typo3.org/projects/typo3v4-extjstrees>
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 /**
00029  * Tree Node Collection
00030  *
00031  * @author Stefan Galinski <stefan.galinski@gmail.com>
00032  * @author Steffen Ritter <info@steffen-ritter.net>
00033  * @package TYPO3
00034  * @subpackage t3lib
00035  */
00036 class t3lib_tree_NodeCollection extends ArrayObject {
00037     /**
00038      * Constructor
00039      *
00040      * You can move an initial data array to initialize the instance and further objects.
00041      * This is useful for the deserialization.
00042      *
00043      * @param array $data
00044      */
00045     public function __construct(array $data = array()) {
00046         if (count($data)) {
00047             $this->dataFromArray($data);
00048         }
00049     }
00050 
00051     /**
00052      * Sorts the internal nodes array
00053      *
00054      * @return void
00055      */
00056     public function asort() {
00057         $this->uasort(array($this, 'nodeCompare'));
00058     }
00059 
00060     /**
00061      * Compares a node with another one
00062      *
00063      * @noapi
00064      * @see t3lib_tree_Node::compareTo
00065      * @return void
00066      */
00067     public function nodeCompare(t3lib_tree_Node $node, t3lib_tree_Node $otherNode) {
00068         return $node->compareTo($otherNode);
00069     }
00070 
00071     /**
00072      * Returns the serialized instance
00073      *
00074      * @return string
00075      */
00076     public function serialize() {
00077         return serialize($this->toArray());
00078     }
00079 
00080     /**
00081      * Initializes the current object with a serialized instance
00082      *
00083      * @throws t3lib_exception if the deserialized is not identical to the current class
00084      * @param string $serializedString
00085      * @return void
00086      */
00087     public function unserialize($serializedString) {
00088         $arrayRepresentation = unserialize($serializedString);
00089         if ($arrayRepresentation['serializeClassName'] !== get_class($this)) {
00090             throw new t3lib_exception('Deserialized object type is not identical!');
00091         }
00092         $this->dataFromArray($arrayRepresentation);
00093     }
00094 
00095     /**
00096      * Returns the collection in an array representation for e.g. serialization
00097      *
00098      * @return array
00099      */
00100     public function toArray() {
00101         $arrayRepresentation = array(
00102             'serializeClassName' => get_class($this),
00103         );
00104 
00105         $iterator = $this->getIterator();
00106         while ($iterator->valid()) {
00107             $arrayRepresentation[] = $iterator->current()->toArray();
00108             $iterator->next();
00109         }
00110 
00111         return $arrayRepresentation;
00112     }
00113 
00114     /**
00115      * Sets the data of the node collection by a given array
00116      *
00117      * @param array $data
00118      * @return void
00119      */
00120     public function dataFromArray($data) {
00121         unset($data['serializeClassName']);
00122         foreach ($data as $index => $nodeArray) {
00123             $node = t3lib_div::makeInstance($nodeArray['serializeClassName'], $nodeArray);
00124             $this->offsetSet($index, $node);
00125         }
00126     }
00127 }
00128 
00129 ?>