xref: /PHP-5.3/ext/spl/tests/iterator_001.phpt (revision 610c7fbe)
1--TEST--
2SPL: Iterator aggregating inner iterator's methods
3--FILE--
4<?php
5
6class NumericArrayIterator implements Iterator
7{
8	protected $a;
9	protected $i = 0;
10
11	public function __construct($a)
12	{
13		echo __METHOD__ . "\n";
14		$this->a = $a;
15	}
16
17	public function rewind()
18	{
19		echo __METHOD__ . "\n";
20		$this->i = 0;
21	}
22
23	public function valid()
24	{
25		$ret = $this->i < count($this->a);
26		echo __METHOD__ . '(' . ($ret ? 'true' : 'false') . ")\n";
27		return $ret;
28	}
29
30	public function key()
31	{
32		echo __METHOD__ . "\n";
33		return $this->i;
34	}
35
36	public function current()
37	{
38		echo __METHOD__ . "\n";
39		return $this->a[$this->i];
40	}
41
42	public function next()
43	{
44		echo __METHOD__ . "\n";
45		$this->i++;
46	}
47
48	public function greaterThan($comp)
49	{
50		echo get_class($this) . '::' . __FUNCTION__ . '(' . $comp . ")\n";
51		return $this->current() > $comp;
52	}
53}
54
55class SeekableNumericArrayIterator extends NumericArrayIterator implements SeekableIterator
56{
57	public function seek($index)
58	{
59		if ($index < count($this->a)) {
60			$this->i = $index;
61		}
62		echo __METHOD__ . '(' . $index . ")\n";
63	}
64}
65
66$a = array(1, 2, 3, 4, 5);
67$it = new LimitIterator(new NumericArrayIterator($a), 1, 3);
68foreach ($it as $v)
69{
70	print $v . ' is ' . ($it->greaterThan(2) ? 'greater than 2' : 'less than or equal 2') . "\n";
71}
72
73echo "===SEEKABLE===\n";
74$a = array(1, 2, 3, 4, 5);
75$it = new LimitIterator(new SeekableNumericArrayIterator($a), 1, 3);
76foreach($it as $v)
77{
78	print $v . ' is ' . ($it->greaterThan(2) ? 'greater than 2' : 'less than or equal 2') . "\n";
79}
80
81echo "===STACKED===\n";
82echo "Shows '2 is greater than 2' because the test is actually done with the current value which is 3.\n";
83$a = array(1, 2, 3, 4, 5);
84$it = new CachingIterator(new LimitIterator(new SeekableNumericArrayIterator($a), 1, 3));
85foreach($it as $v)
86{
87	print $v . ' is ' . ($it->greaterThan(2) ? 'greater than 2' : 'less than or equal 2') . "\n";
88}
89
90?>
91===DONE===
92<?php exit(0); ?>
93--EXPECT--
94NumericArrayIterator::__construct
95NumericArrayIterator::rewind
96NumericArrayIterator::valid(true)
97NumericArrayIterator::next
98NumericArrayIterator::valid(true)
99NumericArrayIterator::valid(true)
100NumericArrayIterator::current
101NumericArrayIterator::key
102NumericArrayIterator::greaterThan(2)
103NumericArrayIterator::current
1042 is less than or equal 2
105NumericArrayIterator::next
106NumericArrayIterator::valid(true)
107NumericArrayIterator::current
108NumericArrayIterator::key
109NumericArrayIterator::greaterThan(2)
110NumericArrayIterator::current
1113 is greater than 2
112NumericArrayIterator::next
113NumericArrayIterator::valid(true)
114NumericArrayIterator::current
115NumericArrayIterator::key
116NumericArrayIterator::greaterThan(2)
117NumericArrayIterator::current
1184 is greater than 2
119NumericArrayIterator::next
120===SEEKABLE===
121NumericArrayIterator::__construct
122NumericArrayIterator::rewind
123SeekableNumericArrayIterator::seek(1)
124NumericArrayIterator::valid(true)
125NumericArrayIterator::current
126NumericArrayIterator::key
127SeekableNumericArrayIterator::greaterThan(2)
128NumericArrayIterator::current
1292 is less than or equal 2
130NumericArrayIterator::next
131NumericArrayIterator::valid(true)
132NumericArrayIterator::current
133NumericArrayIterator::key
134SeekableNumericArrayIterator::greaterThan(2)
135NumericArrayIterator::current
1363 is greater than 2
137NumericArrayIterator::next
138NumericArrayIterator::valid(true)
139NumericArrayIterator::current
140NumericArrayIterator::key
141SeekableNumericArrayIterator::greaterThan(2)
142NumericArrayIterator::current
1434 is greater than 2
144NumericArrayIterator::next
145===STACKED===
146Shows '2 is greater than 2' because the test is actually done with the current value which is 3.
147NumericArrayIterator::__construct
148NumericArrayIterator::rewind
149SeekableNumericArrayIterator::seek(1)
150NumericArrayIterator::valid(true)
151NumericArrayIterator::current
152NumericArrayIterator::key
153NumericArrayIterator::next
154NumericArrayIterator::valid(true)
155NumericArrayIterator::current
156NumericArrayIterator::key
157SeekableNumericArrayIterator::greaterThan(2)
158NumericArrayIterator::current
1592 is greater than 2
160NumericArrayIterator::next
161NumericArrayIterator::valid(true)
162NumericArrayIterator::current
163NumericArrayIterator::key
164SeekableNumericArrayIterator::greaterThan(2)
165NumericArrayIterator::current
1663 is greater than 2
167NumericArrayIterator::next
168SeekableNumericArrayIterator::greaterThan(2)
169NumericArrayIterator::current
1704 is greater than 2
171===DONE===
172