IdentifiableTree.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 IdentifiableTree
00018         extends IdentifiableObject
00019         implements Stringable
00020     {
00021         private $parent = null;
00022         
00026         public function getParent()
00027         {
00028             return $this->parent;
00029         }
00030         
00034         public function setParent(IdentifiableTree $parent)
00035         {
00036             Assert::brothers($this, $parent);
00037             
00038             $this->parent = $parent;
00039             
00040             return $this;
00041         }
00042         
00046         public function dropParent()
00047         {
00048             $this->parent = null;
00049             
00050             return $this;
00051         }
00052         
00056         public function getRoot()
00057         {
00058             $current = $this;
00059             $next = $this;
00060             
00061             while ($next) {
00062                 $current = $next;
00063                 $next = $next->getParent();
00064             }
00065             
00066             return $current;
00067         }
00068         
00069         public function toString($delimiter = ', ')
00070         {
00071             $ids = array($this->getId());
00072             
00073             $parent = $this;
00074             
00075             while ($parent = $parent->getParent())
00076                 $ids[] = $parent->getId();
00077             
00078             $ids = array_reverse($ids);
00079             
00080             return implode($delimiter, $ids);
00081         }
00082     }
00083 ?>