SPL-StandardPHPLibrary
recursivetreeiterator.inc
Go to the documentation of this file.
1 <?php
2 
21 {
22  const BYPASS_CURRENT = 0x00000004;
23  const BYPASS_KEY = 0x00000008;
24 
25  private $rit_flags;
26 
33  function __construct(RecursiveIterator $it, $rit_flags = self::BYPASS_KEY, $cit_flags = CachingIterator::CATCH_GET_CHILD, $mode = self::SELF_FIRST)
34  {
35  parent::__construct(new RecursiveCachingIterator($it, $cit_flags), $mode, $rit_flags);
36  $this->rit_flags = $rit_flags;
37  }
38 
39  private $prefix = array(0=>'', 1=>'| ', 2=>' ', 3=>'|-', 4=>'\-', 5=>'');
40 
42  const PREFIX_LEFT = 0;
46  const PREFIX_MID_LAST = 2;
50  const PREFIX_END_LAST = 4;
52  const PREFIX_RIGHT = 5;
53 
60  function setPrefixPart($part, $value)
61  {
62  if (0 > $part || $part > 5) {
63  throw new OutOfRangeException();
64  }
65  $this->prefix[$part] = (string)$value;
66  }
67 
70  function getPrefix()
71  {
72  $tree = '';
73  for ($level = 0; $level < $this->getDepth(); $level++)
74  {
75  $tree .= $this->getSubIterator($level)->hasNext() ? $this->prefix[1] : $this->prefix[2];
76  }
77  $tree .= $this->getSubIterator($level)->hasNext() ? $this->prefix[3] : $this->prefix[4];
78 
79  return $this->prefix[0] . $tree . $this->prefix[5];
80  }
81 
84  function getEntry()
85  {
86  return @(string)parent::current();
87  }
88 
91  function getPostfix()
92  {
93  return '';
94  }
95 
98  function current()
99  {
100  if ($this->rit_flags & self::BYPASS_CURRENT)
101  {
102  return parent::current();
103  }
104  else
105  {
106  return $this->getPrefix() . $this->getEntry() . $this->getPostfix();
107  }
108  }
109 
112  function key()
113  {
114  if ($this->rit_flags & self::BYPASS_KEY)
115  {
116  return parent::key();
117  }
118  else
119  {
120  return $this->getPrefix() . parent::key() . $this->getPostfix();
121  }
122  }
123 
126  function __call($func, $params)
127  {
128  return call_user_func_array(array($this->getSubIterator(), $func), $params);
129  }
130 }
131 
132 ?>
RecursiveIteratorIterator to generate ASCII graphic trees for the entries in a RecursiveIterator.
const PREFIX_END_HAS_NEXT
Prefix used if $level == depth and hasNext($level) == true.
Iterates through recursive iterators.
const PREFIX_RIGHT
Prefix used right in front of the current element.
const PREFIX_MID_LAST
Prefix used if $level &lt; depth and hasNext($level) == false.
$it
Definition: class_tree.php:105
Cached recursive iteration over another Iterator.
Interface for recursive iteration with RecursiveIteratorIterator.
const PREFIX_MID_HAS_NEXT
Prefix used if $level &lt; depth and hasNext($level) == true.
setPrefixPart($part, $value)
Set prefix part as used in getPrefix() and stored in $prefix.
Exception thrown when an illegal index was requested.
Definition: spl.php:415
const PREFIX_END_LAST
Prefix used if $level == depth and hasNext($level) == false.
const PREFIX_LEFT
Prefix used to start elements.
__call($func, $params)
Aggregates the inner iterator.
__construct(RecursiveIterator $it, $rit_flags=self::BYPASS_KEY, $cit_flags=CachingIterator::CATCH_GET_CHILD, $mode=self::SELF_FIRST)