SPL-StandardPHPLibrary
Public Member Functions | Public Attributes | Private Attributes | List of all members
RecursiveTreeIterator Class Reference
Inheritance diagram for RecursiveTreeIterator:
Inheritance graph
Collaboration diagram for RecursiveTreeIterator:
Collaboration graph

Public Member Functions

 __call ($func, $params)
 
 __construct (RecursiveIterator $it, $rit_flags=self::BYPASS_KEY, $cit_flags=CachingIterator::CATCH_GET_CHILD, $mode=self::SELF_FIRST)
 
 beginChildren ()
 
 callGetChildren ()
 
 callHasChildren ()
 
 current ()
 
 endChildren ()
 
 getDepth ()
 
 getEntry ()
 
 getInnerIterator ()
 
 getPostfix ()
 
 getPrefix ()
 
 getSubIterator ($level=NULL)
 
 key ()
 
 next ()
 
 nextElement ()
 
 rewind ()
 
 setPrefixPart ($part, $value)
 
 valid ()
 

Public Attributes

const BYPASS_CURRENT = 0x00000004
 
const BYPASS_KEY = 0x00000008
 
const CATCH_GET_CHILD = 0x00000002
 
const CHILD_FIRST = 2
 
const LEAVES_ONLY = 0
 
const PREFIX_END_HAS_NEXT = 3
 
const PREFIX_END_LAST = 4
 
const PREFIX_LEFT = 0
 
const PREFIX_MID_HAS_NEXT = 1
 
const PREFIX_MID_LAST = 2
 
const PREFIX_RIGHT = 5
 
const SELF_FIRST = 1
 

Private Attributes

 $prefix = array(0=>'', 1=>'| ', 2=>' ', 3=>'|-', 4=>'\-', 5=>'')
 
 $rit_flags
 

Detailed Description

RecursiveIteratorIterator to generate ASCII graphic trees for the entries in a RecursiveIterator.

Author
Marcus Boerger, Johannes Schlueter
Version
1.1
Since
PHP 5.3

Definition at line 20 of file recursivetreeiterator.inc.

Constructor & Destructor Documentation

RecursiveTreeIterator::__construct ( RecursiveIterator  $it,
  $rit_flags = self::BYPASS_KEY,
  $cit_flags = CachingIterator::CATCH_GET_CHILD,
  $mode = self::SELF_FIRST 
)
Parameters
ititerator to use as inner iterator
rit_flagsflags passed to RecursiveIteratoIterator (parent)
cit_flagsflags passed to RecursiveCachingIterator (for hasNext)
modemode passed to RecursiveIteratoIterator (parent)

Definition at line 33 of file recursivetreeiterator.inc.

References RecursiveIteratorIterator\$mode, and $rit_flags.

34  {
35  parent::__construct(new RecursiveCachingIterator($it, $cit_flags), $mode, $rit_flags);
36  $this->rit_flags = $rit_flags;
37  }
Cached recursive iteration over another Iterator.

Member Function Documentation

RecursiveTreeIterator::__call (   $func,
  $params 
)

Aggregates the inner iterator.

Definition at line 126 of file recursivetreeiterator.inc.

References RecursiveIteratorIterator\getSubIterator().

127  {
128  return call_user_func_array(array($this->getSubIterator(), $func), $params);
129  }

Here is the call graph for this function:

RecursiveIteratorIterator::beginChildren ( )
inherited

Called right after calling getChildren() and its rewind().

Since
PHP 5.1

Definition at line 201 of file recursiveiteratoriterator.inc.

Referenced by RecursiveIteratorIterator\next().

202  {
203  }
RecursiveIteratorIterator::callGetChildren ( )
inherited
Returns
current sub iterators current children
Since
PHP 5.1

Definition at line 193 of file recursiveiteratoriterator.inc.

References RecursiveIteratorIterator\$count.

Referenced by RecursiveIteratorIterator\next().

194  {
195  return $this->ait[$this->count]->getChildren();
196  }
RecursiveIteratorIterator::callHasChildren ( )
inherited
Returns
whether current sub iterators current element has children
Since
PHP 5.1

Definition at line 185 of file recursiveiteratoriterator.inc.

References RecursiveIteratorIterator\$count.

Referenced by RecursiveIteratorIterator\callNextElement(), and RecursiveIteratorIterator\next().

186  {
187  return $this->ait[$this->count]->hasChildren();
188  }
RecursiveTreeIterator::current ( )
Returns
the current element prefixed and postfixed

Implements Iterator.

Definition at line 98 of file recursivetreeiterator.inc.

References getEntry(), getPostfix(), and getPrefix().

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  }

Here is the call graph for this function:

RecursiveIteratorIterator::endChildren ( )
inherited

Called after current child iterator is invalid and right before it gets destructed.

Since
PHP 5.1

Definition at line 209 of file recursiveiteratoriterator.inc.

Referenced by RecursiveIteratorIterator\next(), RecursiveIteratorIterator\rewind(), and RecursiveIteratorIterator\valid().

210  {
211  }
RecursiveIteratorIterator::getDepth ( )
inherited
Returns
Current Depth (Number of parents)

Definition at line 177 of file recursiveiteratoriterator.inc.

Referenced by DirectoryTreeIterator\current(), and getPrefix().

178  {
179  return $this->level;
180  }
RecursiveTreeIterator::getEntry ( )
Returns
string presentation build for current element

Definition at line 84 of file recursivetreeiterator.inc.

Referenced by current().

85  {
86  return @(string)parent::current();
87  }
RecursiveIteratorIterator::getInnerIterator ( )
inherited
Returns
The inner iterator

Implements OuterIterator.

Definition at line 170 of file recursiveiteratoriterator.inc.

References $it.

Referenced by RecursiveCompareDualIterator\areEqual(), RecursiveCompareDualIterator\areIdentical(), and RecursiveCompareDualIterator\endChildren().

171  {
172  return $this->it;
173  }
$it
Definition: class_tree.php:105
RecursiveTreeIterator::getPostfix ( )
Returns
string to place after the current element

Definition at line 91 of file recursivetreeiterator.inc.

Referenced by current(), and key().

92  {
93  return '';
94  }
RecursiveTreeIterator::getPrefix ( )
Returns
string to place in front of current element

Definition at line 70 of file recursivetreeiterator.inc.

References RecursiveIteratorIterator\getDepth(), and RecursiveIteratorIterator\getSubIterator().

Referenced by current(), and key().

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  }

Here is the call graph for this function:

RecursiveIteratorIterator::getSubIterator (   $level = NULL)
inherited
Returns
Sub Iterator at given level or if unspecified the current sub Iterator

Definition at line 159 of file recursiveiteratoriterator.inc.

References RecursiveIteratorIterator\$count.

Referenced by DirectoryTreeIterator\__call(), __call(), DirectoryTreeIterator\current(), and getPrefix().

160  {
161  if (is_null($level)) {
162  $level = $this->count;
163  }
164  return @$this->ait[$level];
165  }
RecursiveTreeIterator::key ( )
Returns
the current key prefixed and postfixed

Implements Iterator.

Definition at line 112 of file recursivetreeiterator.inc.

References getPostfix(), and getPrefix().

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  }

Here is the call graph for this function:

RecursiveIteratorIterator::next ( )
inherited

Forward to next element.

Implements Iterator.

Definition at line 107 of file recursiveiteratoriterator.inc.

References RecursiveIteratorIterator\$count, $it, RecursiveIteratorIterator\beginChildren(), RecursiveIteratorIterator\callGetChildren(), RecursiveIteratorIterator\callHasChildren(), RecursiveIteratorIterator\callNextElement(), and RecursiveIteratorIterator\endChildren().

108  {
109  while ($this->count) {
110  $it = $this->ait[$this->count];
111  if ($it->valid()) {
112  if (!$it->recursed && callHasChildren()) {
113  $it->recursed = true;
114  try
115  {
116  $sub = callGetChildren();
117  }
118  catch (Exception $e)
119  {
120  if (!($this->flags & self::CATCH_GET_CHILD))
121  {
122  throw $e;
123  }
124  $it->next();
125  continue;
126  }
127  $sub->recursed = false;
128  $sub->rewind();
129  if ($sub->valid()) {
130  $this->ait[++$this->count] = $sub;
131  if (!$sub instanceof RecursiveIterator) {
132  throw new Exception(get_class($sub).'::getChildren() must return an object that implements RecursiveIterator');
133  }
134  $this->beginChildren();
135  return;
136  }
137  unset($sub);
138  }
139  $it->next();
140  $it->recursed = false;
141  if ($it->valid()) {
142  return;
143  }
144  $it->recursed = false;
145  }
146  if ($this->count) {
147  unset($this->ait[$this->count--]);
148  $it = $this->ait[$this->count];
149  $this->endChildren();
150  callNextElement(false);
151  }
152  }
153  callNextElement(true);
154  }
$it
Definition: class_tree.php:105
Interface for recursive iteration with RecursiveIteratorIterator.
beginChildren()
Called right after calling getChildren() and its rewind().
Basic Exception class.
Definition: spl.php:258
endChildren()
Called after current child iterator is invalid and right before it gets destructed.

Here is the call graph for this function:

RecursiveIteratorIterator::nextElement ( )
inherited

Called when the next element is available.

Definition at line 232 of file recursiveiteratoriterator.inc.

Referenced by RecursiveIteratorIterator\callNextElement().

233  {
234  }
RecursiveIteratorIterator::rewind ( )
inherited

Rewind to top iterator as set in constructor.

Implements Iterator.

Definition at line 62 of file recursiveiteratoriterator.inc.

References RecursiveIteratorIterator\callNextElement(), and RecursiveIteratorIterator\endChildren().

63  {
64  while ($this->count) {
65  unset($this->ait[$this->count--]);
66  $this->endChildren();
67  }
68  $this->ait[0]->rewind();
69  $this->ait[0]->recursed = false;
70  callNextElement(true);
71  }
endChildren()
Called after current child iterator is invalid and right before it gets destructed.

Here is the call graph for this function:

RecursiveTreeIterator::setPrefixPart (   $part,
  $value 
)

Set prefix part as used in getPrefix() and stored in $prefix.

Parameters
$partany PREFIX_* const.
$valuenew prefix string for specified part.
Exceptions
OutOfRangeExceptionif 0 > $part or $part > 5.

Definition at line 60 of file recursivetreeiterator.inc.

61  {
62  if (0 > $part || $part > 5) {
63  throw new OutOfRangeException();
64  }
65  $this->prefix[$part] = (string)$value;
66  }
Exception thrown when an illegal index was requested.
Definition: spl.php:415
RecursiveIteratorIterator::valid ( )
inherited
Returns
whether iterator is valid

Implements Iterator.

Definition at line 75 of file recursiveiteratoriterator.inc.

References RecursiveIteratorIterator\$count, $it, and RecursiveIteratorIterator\endChildren().

Referenced by RecursiveIteratorIterator\callNextElement().

76  {
78  while ($count) {
79  $it = $this->ait[$count];
80  if ($it->valid()) {
81  return true;
82  }
83  $count--;
84  $this->endChildren();
85  }
86  return false;
87  }
$it
Definition: class_tree.php:105
endChildren()
Called after current child iterator is invalid and right before it gets destructed.

Here is the call graph for this function:

Member Data Documentation

RecursiveTreeIterator::$prefix = array(0=>'', 1=>'| ', 2=>' ', 3=>'|-', 4=>'\-', 5=>'')
private

Definition at line 39 of file recursivetreeiterator.inc.

RecursiveTreeIterator::$rit_flags
private

Definition at line 25 of file recursivetreeiterator.inc.

Referenced by __construct().

const RecursiveTreeIterator::BYPASS_CURRENT = 0x00000004

Definition at line 22 of file recursivetreeiterator.inc.

const RecursiveTreeIterator::BYPASS_KEY = 0x00000008

Definition at line 23 of file recursivetreeiterator.inc.

const RecursiveIteratorIterator::CATCH_GET_CHILD = 0x00000002
inherited

Flag: Catches exceptions during getChildren() calls and simply jumps to the next element.

Definition at line 33 of file recursiveiteratoriterator.inc.

const RecursiveIteratorIterator::CHILD_FIRST = 2
inherited

Mode: Show all children prior to their parent.

Definition at line 29 of file recursiveiteratoriterator.inc.

const RecursiveIteratorIterator::LEAVES_ONLY = 0
inherited

Mode: Only show leaves.

Definition at line 25 of file recursiveiteratoriterator.inc.

const RecursiveTreeIterator::PREFIX_END_HAS_NEXT = 3

Prefix used if $level == depth and hasNext($level) == true.

Definition at line 48 of file recursivetreeiterator.inc.

const RecursiveTreeIterator::PREFIX_END_LAST = 4

Prefix used if $level == depth and hasNext($level) == false.

Definition at line 50 of file recursivetreeiterator.inc.

const RecursiveTreeIterator::PREFIX_LEFT = 0

Prefix used to start elements.

Definition at line 42 of file recursivetreeiterator.inc.

const RecursiveTreeIterator::PREFIX_MID_HAS_NEXT = 1

Prefix used if $level < depth and hasNext($level) == true.

Definition at line 44 of file recursivetreeiterator.inc.

const RecursiveTreeIterator::PREFIX_MID_LAST = 2

Prefix used if $level < depth and hasNext($level) == false.

Definition at line 46 of file recursivetreeiterator.inc.

const RecursiveTreeIterator::PREFIX_RIGHT = 5

Prefix used right in front of the current element.

Definition at line 52 of file recursivetreeiterator.inc.

const RecursiveIteratorIterator::SELF_FIRST = 1
inherited

Mode: Show parents prior to their children.

Definition at line 27 of file recursiveiteratoriterator.inc.


The documentation for this class was generated from the following file: