TYPO3 API  SVNRelease
class.t3lib_tree_node.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
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_Node implements t3lib_tree_ComparableNode, Serializable {
00037     /**
00038      * Node Identifier
00039      *
00040      * @var string
00041      */
00042     protected $id = '';
00043 
00044     /**
00045      * Parent Node
00046      *
00047      * @var t3lib_tree_Node
00048      */
00049     protected $parentNode = NULL;
00050 
00051     /**
00052      * Child Nodes
00053      *
00054      * @var t3lib_tree_NodeCollection
00055      */
00056     protected $childNodes = NULL;
00057 
00058     /**
00059      * Constructor
00060      *
00061      * You can move an initial data array to initialize the instance and further objects.
00062      * This is useful for the deserialization.
00063      *
00064      * @param array $data
00065      * @return void
00066      */
00067     public function __construct(array $data = array()) {
00068         if (count($data)) {
00069             $this->dataFromArray($data);
00070         }
00071     }
00072 
00073     /**
00074      * Sets the child nodes collection
00075      *
00076      * @param t3lib_tree_NodeCollection $childNodes
00077      * @return void
00078      */
00079     public function setChildNodes(t3lib_tree_NodeCollection $childNodes) {
00080         $this->childNodes = $childNodes;
00081     }
00082 
00083     /**
00084      * Removes child nodes collection
00085      *
00086      * @return void
00087      */
00088     public function removeChildNodes() {
00089         if ($this->childNodes !== NULL) {
00090             unset($this->childNodes);
00091             $this->childNodes = NULL;
00092         }
00093     }
00094 
00095     /**
00096      * Returns child nodes collection
00097      *
00098      * @return t3lib_tree_NodeCollection
00099      */
00100     public function getChildNodes() {
00101         return $this->childNodes;
00102     }
00103 
00104     /**
00105      * Returns true if the node has child nodes attached
00106      *
00107      * @return boolean
00108      */
00109     public function hasChildNodes() {
00110         if ($this->childNodes !== NULL) {
00111             return TRUE;
00112         }
00113 
00114         return FALSE;
00115     }
00116 
00117     /**
00118      * Sets the identifier
00119      *
00120      * @param string $id
00121      * @return void
00122      */
00123     public function setId($id) {
00124         $this->id = $id;
00125     }
00126 
00127     /**
00128      * Returns the identifier
00129      *
00130      * @return string
00131      */
00132     public function getId() {
00133         return $this->id;
00134     }
00135 
00136     /**
00137      * Sets the parent node
00138      *
00139      * @param t3lib_tree_Node $parentNode
00140      * @return void
00141      */
00142     public function setParentNode(t3lib_tree_Node $parentNode = NULL) {
00143         $this->parentNode = $parentNode;
00144     }
00145 
00146     /**
00147      * Returns the parent node
00148      *
00149      * @return t3lib_tree_Node
00150      */
00151     public function getParentNode() {
00152         return $this->parentNode;
00153     }
00154 
00155     /**
00156      * Compares a node if it's identical to another node by the id property.
00157      *
00158      * @param t3lib_tree_Node $other
00159      * @return boolean
00160      */
00161     public function equals(t3lib_tree_Node $other) {
00162         return $this->id == $other->getId();
00163     }
00164 
00165     /**
00166      * Compares a node to another one.
00167      *
00168      * Returns:
00169      * 1 if its greater than the other one
00170      * -1 if its smaller than the other one
00171      * 0 if its equal
00172      *
00173      * @param t3lib_tree_Node $other
00174      * @return int see description above
00175      */
00176     public function compareTo($other) {
00177         if ($this->equals($other)) {
00178             return 0;
00179         }
00180 
00181         return ($this->id > $other->getId()) ? 1 : -1;
00182     }
00183 
00184     /**
00185      * Returns the node in an array representation that can be used for serialization
00186      *
00187      * @param bool $addChildNodes
00188      * @return array
00189      */
00190     public function toArray($addChildNodes = TRUE) {
00191         $arrayRepresentation = array(
00192             'serializeClassName' => get_class($this),
00193             'id' => $this->id
00194         );
00195 
00196         if ($this->parentNode !== NULL) {
00197             $arrayRepresentation['parentNode'] = $this->parentNode->toArray(FALSE);
00198         } else {
00199             $arrayRepresentation['parentNode'] = '';
00200         }
00201 
00202         if ($this->hasChildNodes() && $addChildNodes) {
00203             $arrayRepresentation['childNodes'] = $this->childNodes->toArray();
00204         } else {
00205             $arrayRepresentation['childNodes'] = '';
00206         }
00207 
00208         return $arrayRepresentation;
00209     }
00210 
00211     /**
00212      * Sets data of the node by a given data array
00213      *
00214      * @param array $data
00215      * @return void
00216      */
00217     public function dataFromArray($data) {
00218         $this->setId($data['id']);
00219 
00220         if (isset($data['parentNode']) && $data['parentNode'] !== '') {
00221             $this->setParentNode(t3lib_div::makeInstance(
00222                 $data['parentNode']['serializeClassName'],
00223                 $data['parentNode']
00224             ));
00225         }
00226 
00227         if (isset($data['childNodes']) && $data['childNodes'] !== '') {
00228             $this->setChildNodes(t3lib_div::makeInstance(
00229                 $data['childNodes']['serializeClassName'],
00230                 $data['childNodes']
00231             ));
00232         }
00233     }
00234 
00235     /**
00236      * Returns the serialized instance
00237      *
00238      * @return string
00239      */
00240     public function serialize() {
00241         return serialize($this->toArray());
00242     }
00243 
00244     /**
00245      * Fills the current node with the given serialized informations
00246      *
00247      * @throws t3lib_exception if the deserialized object type is not identical to the current one
00248      * @param string $serializedString
00249      * @return void
00250      */
00251     public function unserialize($serializedString) {
00252         $arrayRepresentation = unserialize($serializedString);
00253         if ($arrayRepresentation['serializeClassName'] !== get_class($this)) {
00254             throw new t3lib_exception('Deserialized object type is not identical!');
00255         }
00256         $this->dataFromArray($arrayRepresentation);
00257     }
00258 }
00259 
00260 ?>