# NamedTree.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /****************************************************************************
    00003  *   Copyright (C) 2005-2007 by Konstantin V. Arkhipov, Anton E. Lebedevich *
    00004  *                                                                          *
    00005  *   This program is free software; you can redistribute it and/or modify   *
    00006  *   it under the terms of the GNU Lesser General Public License as         *
    00007  *   published by the Free Software Foundation; either version 3 of the     *
    00008  *   License, or (at your option) any later version.                        *
    00009  *                                                                          *
    00010  ****************************************************************************/
    00011
    00017     abstract class NamedTree extends NamedObject
    00018     {
    00019         private $parent = null;
    00020
    00024         public function getParent()
    00025         {
    00026             return $this->parent;
    00027         }
    00028
    00032         public function setParent(NamedTree $parent)
    00033         {
    00034             Assert::brothers($this, $parent);
    00035
    00036             $this->parent = $parent;
    00037
    00038             return $this;
    00039         }
    00040
    00044         public function dropParent()
    00045         {
    00046             $this->parent = null;
    00047
    00048             return $this;
    00049         }
    00050
    00054         public function getRoot()
    00055         {
    00056             $current = $this;
    00057             $next = $this;
    00058
    00059             while ($next) {
    00060                 $current = $next;
    00061                 $next = $next->getParent();
    00062             }
    00063
    00064             return $current;
    00065         }
    00066
    00067         public function toString($delimiter = ' :: ')
    00068         {
    00069             $name = array($this->getName());
    00070
    00071             $parent = $this;
    00072
    00073             while ($parent = $parent->getParent())
    00074                 $name[] = $parent->getName();
    00075
    00076             $name = array_reverse($name);
    00077
    00078             return implode($delimiter, $name);
    00079         }
    00080     }
    00081 ?>