summaryrefslogtreecommitdiff
path: root/ext/spl/internal/recursiveiteratoriterator.inc
blob: 395c67079ee8dbf20b498564bb8c0055bd64e312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php

/** @file recursiveiteratoriterator.inc
 * @ingroup SPL
 * @brief class RecursiveIteratorIterator
 * @author  Marcus Boerger
 * @date    2003 - 2005
 *
 * SPL - Standard PHP Library
 */

define('RIT_LEAVES_ONLY', 0);
define('RIT_SELF_FIRST',  1);
define('RIT_CHILD_FIRST', 2);

/**
 * @brief   Iterates through recursive iterators
 * @author  Marcus Boerger
 * @version 1.1
 *
 * 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.
 */
class RecursiveIteratorIterator implements OuterIterator
{
	private $ait = array();
	private $count = 0;

	/** Construct from RecursiveIterator
	 *
	 * @param it     RecursiveIterator to iterate
	 * @param flags  Operation mode:
	 *               - RIT_LEAVES_ONLY only show leaves
	 *               - RIT_SELF_FIRST  show parents prior to their childs
	 *               - RIT_CHILD_FIRST show all childs prior to their parent
	 */
	function __construct(RecursiveIterator $it, $flags)
	{
		$this->ait[0] = $it;
	}

	/** Rewind to top iterator as set in constructor
	 */
	function rewind()
	{
		while ($this->count) {
			unset($this->ait[$this->count--]);
			$this->endChildren();
		}
		$this->ait[0]->rewind();
		$this->ait[0]->recursed = false;
	}
	
	/** @return whether iterator is valid
	 */
	function valid()
	{
		$count = $this->count;
		while ($count) {
			$it = $this->ait[$count];
			if ($it->valid()) {
				return true;
			}
			$count--;
			$this->endChildren();
		}
		return false;
	}
	
	/** @return current key
	 */
	function key()
	{
		$it = $this->ait[$this->count];
		return $it->key();
	}
	
	/** @return current element
	 */
	function current()
	{
		$it = $this->ait[$this->count];
		return $it->current();
	}
	
	/** Forward to next element
	 */
	function next()
	{
		while ($this->count) {
			$it = $this->ait[$this->count];
			if ($it->valid()) {
				if (!$it->recursed && $it->hasChildren()) {
					$it->recursed = true;
					$sub = $it->getChildren();
					$sub->recursed = false;
					$sub->rewind();
					if ($sub->valid()) {
						$this->ait[++$this->count] = $sub;
						if (!$sub instanceof RecursiveIterator) {
							throw new Exception(get_class($sub).'::getChildren() must return an object that implements RecursiveIterator');
						}
						$this->beginChildren();
						return;
					}
					unset($sub);
				}
				$it->next();
				$it->recursed = false;
				if ($it->valid()) {
					return;
				}
				$it->recursed = false;
			}
			if ($this->count) {
				unset($this->ait[$this->count--]);
				$it = $this->ait[$this->count];
				$this->endChildren();
			}
		}
	}

	/** @return Sub Iterator at given level or if unspecified the current sub 
	 *          Iterator
	 */
	function getSubIterator($level = NULL)
	{
		if (is_null($level)) {
			$level = $this->count;
		}
		return @$this->ait[$level];
	}

	/**
	 * @return The inner iterator
	 */	
	function getInnerIterator()
	{
		return $this->it;
	}

	/** @return Current Depth (Number of parents)
	 */
	function getDepth()
	{
		return $this->level;
	}
	
	/** Called right after calling getChildren()
	 */
	function beginChildren()
	{
	}
	
	/** Called after current child iterator is invalid
	 */
	function endChildren()
	{
	}
}

?>