summaryrefslogtreecommitdiff
path: root/ext/spl/tests/iterator_039.phpt
blob: 17c9bc1ca59ce5664b3b2f3b04467a6cbe617fb7 (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
--TEST--
SPL: LimitIterator and backward seeking
--FILE--
<?php

class NumericArrayIterator implements Iterator
{
	protected $a;
	protected $i = 0;

	public function __construct($a)
	{
		echo __METHOD__ . "\n";
		$this->a = $a;
	}

	public function valid()
	{
		echo __METHOD__ . "\n";
		return $this->i < count($this->a);
	}

	public function rewind()
	{
		echo __METHOD__ . "\n";
		$this->i = 0;
	}

	public function key()
	{
		echo __METHOD__ . "\n";
		return $this->i;
	}

	public function current()
	{
		echo __METHOD__ . "\n";
		return $this->a[$this->i];
	}

	public function next()
	{
		echo __METHOD__ . "\n";
		$this->i++;
	}
}

$it = new LimitIterator(new NumericArrayIterator(array(12, 25, 42, 56)));

foreach($it as $k => $v)
{
	var_dump($k);
	var_dump($v);
}

echo "===SEEK===\n";

$it->seek(2);

echo "===LOOP===\n";

foreach(new NoRewindIterator($it) as $k => $v)
{
	var_dump($k);
	var_dump($v);
}

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
NumericArrayIterator::__construct
NumericArrayIterator::rewind
NumericArrayIterator::valid
NumericArrayIterator::valid
NumericArrayIterator::current
NumericArrayIterator::key
int(0)
int(12)
NumericArrayIterator::next
NumericArrayIterator::valid
NumericArrayIterator::current
NumericArrayIterator::key
int(1)
int(25)
NumericArrayIterator::next
NumericArrayIterator::valid
NumericArrayIterator::current
NumericArrayIterator::key
int(2)
int(42)
NumericArrayIterator::next
NumericArrayIterator::valid
NumericArrayIterator::current
NumericArrayIterator::key
int(3)
int(56)
NumericArrayIterator::next
NumericArrayIterator::valid
===SEEK===
NumericArrayIterator::rewind
NumericArrayIterator::valid
NumericArrayIterator::next
NumericArrayIterator::valid
NumericArrayIterator::next
NumericArrayIterator::valid
NumericArrayIterator::valid
NumericArrayIterator::current
NumericArrayIterator::key
===LOOP===
int(2)
int(42)
NumericArrayIterator::next
NumericArrayIterator::valid
NumericArrayIterator::current
NumericArrayIterator::key
int(3)
int(56)
NumericArrayIterator::next
NumericArrayIterator::valid
===DONE===