SPL-StandardPHPLibrary
recursivetreeiterator.inc
Go to the documentation of this file.
00001 <?php
00002 
00020 class RecursiveTreeIterator extends RecursiveIteratorIterator
00021 {
00022     const BYPASS_CURRENT = 0x00000004;
00023     const BYPASS_KEY     = 0x00000008;
00024 
00025     private $rit_flags;
00026 
00033     function __construct(RecursiveIterator $it, $rit_flags = self::BYPASS_KEY, $cit_flags = CachingIterator::CATCH_GET_CHILD, $mode = self::SELF_FIRST)
00034     {
00035         parent::__construct(new RecursiveCachingIterator($it, $cit_flags), $mode, $rit_flags);
00036         $this->rit_flags = $rit_flags;
00037     }
00038 
00039     private $prefix = array(0=>'', 1=>'| ', 2=>'  ', 3=>'|-', 4=>'\-', 5=>'');
00040 
00042     const PREFIX_LEFT         = 0;
00044     const PREFIX_MID_HAS_NEXT = 1;
00046     const PREFIX_MID_LAST     = 2;
00048     const PREFIX_END_HAS_NEXT = 3;
00050     const PREFIX_END_LAST     = 4;
00052     const PREFIX_RIGHT        = 5;
00053 
00060     function setPrefixPart($part, $value)
00061     {
00062         if (0 > $part || $part > 5) {
00063             throw new OutOfRangeException();
00064         }
00065         $this->prefix[$part] = (string)$value;
00066     }
00067 
00070     function getPrefix()
00071     {
00072         $tree = '';
00073         for ($level = 0; $level < $this->getDepth(); $level++)
00074         {
00075             $tree .= $this->getSubIterator($level)->hasNext() ? $this->prefix[1] : $this->prefix[2];
00076         }
00077         $tree .= $this->getSubIterator($level)->hasNext() ? $this->prefix[3] : $this->prefix[4];
00078 
00079         return $this->prefix[0] . $tree . $this->prefix[5];
00080     }
00081 
00084     function getEntry()
00085     {
00086         return @(string)parent::current();
00087     }
00088 
00091     function getPostfix()
00092     {
00093         return '';
00094     }
00095 
00098     function current()
00099     {
00100         if ($this->rit_flags & self::BYPASS_CURRENT)
00101         {
00102             return parent::current();
00103         }
00104         else
00105         {
00106             return $this->getPrefix() . $this->getEntry() .  $this->getPostfix();
00107         }
00108     }
00109 
00112     function key()
00113     {
00114         if ($this->rit_flags & self::BYPASS_KEY)
00115         {
00116             return parent::key();
00117         }
00118         else
00119         {
00120             return $this->getPrefix() . parent::key() .  $this->getPostfix();
00121         }
00122     }
00123 
00126     function __call($func, $params)
00127     {
00128         return call_user_func_array(array($this->getSubIterator(), $func), $params);
00129     }
00130 }
00131 
00132 ?>