summaryrefslogtreecommitdiff
path: root/ext/spl/examples/infiniteiterator.inc
blob: 5f27633eb35cf599fdf4e210e1c7245119ed800b (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
<?php

/** @file infiniteiterator.inc
 * @ingroup Examples
 * @brief class InfiniteIterator
 * @author  Marcus Boerger
 * @date    2003 - 2004
 *
 * SPL - Standard PHP Library
 */

/** @ingroup Examples
 * @brief   An infinite Iterator
 * @author  Marcus Boerger
 * @version 1.0
 *
 * This Iterator takes another Iterator and infinitvely iterates it by
 * rewinding it when its end is reached.
 *
 * \note Even an InfiniteIterator stops if its inner Iterator is empty.
 *
 \verbatim
 $it       = new ArrayIterator(array(1,2,3));
 $infinite = new InfiniteIterator($it);
 $limit    = new LimitIterator($infinite, 0, 5);
 foreach($limit as $val=>$key)
 {
 	echo "$val=>$key\n";
 }
 \endverbatim
 */
class InfiniteIterator implements Iterator
{
	/** @internal 
	 * The inner Iterator. */
	private $it;

	/** Construct from another Iterator.
	 * @param $it the inner Iterator.
	 */
	function __construct(Iterator $it)
	{
		$this->it = $it;
	}

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

	/** Rewind the inner iterator.
	 * @return void
	 */
	function rewind()
	{
		$this->it->rewind();
	}

	/** @return whether the current element is valid
	 */
	function valid()
	{
		return $this->it->valid();
	}

	/** @return the current value
	 */
	function current()
	{
		return $this->it->current();
	}

	/** @return the current key
	 */
	function key()
	{
		return $this->it->key();
	}

	/** Move the inner Iterator forward to its next element or rewind it.
	 * @return void
	 */
	function next()
	{
		$this->it->next();
		if (!$this->it->valid())
		{
			$this->it->rewind();
		}
	}

	/** Aggregates the inner iterator
	 */	
	function __call($func, $params)
	{
		return call_user_func_array(array($this->it, $func), $params);
	}
}

?>