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

Public Member Functions

 __construct (RecursiveIterator $it, $mode=self::LEAVES_ONLY, $flags=0)
 
 beginChildren ()
 
 callGetChildren ()
 
 callHasChildren ()
 
 current ()
 
 endChildren ()
 
 getDepth ()
 
 getInnerIterator ()
 
 getSubIterator ($level=NULL)
 
 key ()
 
 next ()
 
 nextElement ()
 
 rewind ()
 
 valid ()
 

Public Attributes

const CATCH_GET_CHILD = 0x00000002
 
const CHILD_FIRST = 2
 
const LEAVES_ONLY = 0
 
const SELF_FIRST = 1
 

Private Member Functions

 callNextElement ($after_move)
 

Private Attributes

 $ait = array()
 
 $count = 0
 
 $flags = 0
 
 $mode = self::LEAVES_ONLY
 

Detailed Description

Iterates through recursive iterators.

Author
Marcus Boerger
Version
1.2
Since
PHP 5.0

The objects of this class are created by instances of RecursiveIterator. Elements of those iterators may be traversable themselves. If so these sub elements are recursed into.

Definition at line 22 of file recursiveiteratoriterator.inc.

Constructor & Destructor Documentation

RecursiveIteratorIterator::__construct ( RecursiveIterator  $it,
  $mode = self::LEAVES_ONLY,
  $flags = 0 
)

Construct from RecursiveIterator.

Parameters
itRecursiveIterator to iterate
modeOperation mode (one of):
  • LEAVES_ONLY only show leaves
  • SELF_FIRST show parents prior to their childs
  • CHILD_FIRST show all children prior to their parent
flagsControl flags, zero or any combination of the following (since PHP 5.1).
  • CATCH_GET_CHILD which catches exceptions during getChildren() calls and simply jumps to the next element.

Definition at line 53 of file recursiveiteratoriterator.inc.

References $flags, $it, and $mode.

Referenced by DirectoryGraphIterator\__construct().

54  {
55  $this->ait[0] = $it;
56  $this->mode = $mode;
57  $this->flags = $flags;
58  }
$it
Definition: class_tree.php:105

Member Function Documentation

RecursiveIteratorIterator::beginChildren ( )

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

Since
PHP 5.1

Definition at line 201 of file recursiveiteratoriterator.inc.

Referenced by next().

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

Definition at line 193 of file recursiveiteratoriterator.inc.

References $count.

Referenced by next().

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

Definition at line 185 of file recursiveiteratoriterator.inc.

References $count.

Referenced by callNextElement(), and next().

186  {
187  return $this->ait[$this->count]->hasChildren();
188  }
RecursiveIteratorIterator::callNextElement (   $after_move)
private

Definition at line 213 of file recursiveiteratoriterator.inc.

References callHasChildren(), nextElement(), and valid().

Referenced by next(), and rewind().

214  {
215  if ($this->valid())
216  {
217  if ($after_move)
218  {
219  if (($this->mode == self::SELF_FIRST && $this->callHasChildren())
220  || $this->mode == self::LEAVES_ONLY)
221  $this->nextElement();
222  }
223  else
224  {
225  $this->nextElement();
226  }
227  }
228  }
nextElement()
Called when the next element is available.

Here is the call graph for this function:

RecursiveIteratorIterator::current ( )
Returns
current element

Implements Iterator.

Definition at line 99 of file recursiveiteratoriterator.inc.

References $count, and $it.

100  {
101  $it = $this->ait[$this->count];
102  return $it->current();
103  }
$it
Definition: class_tree.php:105
RecursiveIteratorIterator::endChildren ( )

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 next(), rewind(), and valid().

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

Definition at line 177 of file recursiveiteratoriterator.inc.

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

178  {
179  return $this->level;
180  }
RecursiveIteratorIterator::getInnerIterator ( )
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
RecursiveIteratorIterator::getSubIterator (   $level = NULL)
Returns
Sub Iterator at given level or if unspecified the current sub Iterator

Definition at line 159 of file recursiveiteratoriterator.inc.

References $count.

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

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

Implements Iterator.

Definition at line 91 of file recursiveiteratoriterator.inc.

References $count, and $it.

92  {
93  $it = $this->ait[$this->count];
94  return $it->key();
95  }
$it
Definition: class_tree.php:105
RecursiveIteratorIterator::next ( )

Forward to next element.

Implements Iterator.

Definition at line 107 of file recursiveiteratoriterator.inc.

References $count, $it, beginChildren(), callGetChildren(), callHasChildren(), callNextElement(), and 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 ( )

Called when the next element is available.

Definition at line 232 of file recursiveiteratoriterator.inc.

Referenced by callNextElement().

233  {
234  }
RecursiveIteratorIterator::rewind ( )

Rewind to top iterator as set in constructor.

Implements Iterator.

Definition at line 62 of file recursiveiteratoriterator.inc.

References callNextElement(), and 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:

RecursiveIteratorIterator::valid ( )
Returns
whether iterator is valid

Implements Iterator.

Definition at line 75 of file recursiveiteratoriterator.inc.

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

Referenced by 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

RecursiveIteratorIterator::$ait = array()
private

Definition at line 35 of file recursiveiteratoriterator.inc.

RecursiveIteratorIterator::$count = 0
private
RecursiveIteratorIterator::$flags = 0
private

Definition at line 38 of file recursiveiteratoriterator.inc.

Referenced by __construct().

RecursiveIteratorIterator::$mode = self::LEAVES_ONLY
private

Definition at line 37 of file recursiveiteratoriterator.inc.

Referenced by RecursiveTreeIterator\__construct(), and __construct().

const RecursiveIteratorIterator::CATCH_GET_CHILD = 0x00000002

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

Mode: Show all children prior to their parent.

Definition at line 29 of file recursiveiteratoriterator.inc.

const RecursiveIteratorIterator::LEAVES_ONLY = 0

Mode: Only show leaves.

Definition at line 25 of file recursiveiteratoriterator.inc.

const RecursiveIteratorIterator::SELF_FIRST = 1

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: