xref: /PHP-8.2/ext/spl/tests/iterator_001.phpt (revision b3e08881)
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(): void
18    {
19        echo __METHOD__ . "\n";
20        $this->i = 0;
21    }
22
23    public function valid(): bool
24    {
25        $ret = $this->i < count($this->a);
26        echo __METHOD__ . '(' . ($ret ? 'true' : 'false') . ")\n";
27        return $ret;
28    }
29
30    public function key(): mixed
31    {
32        echo __METHOD__ . "\n";
33        return $this->i;
34    }
35
36    public function current(): mixed
37    {
38        echo __METHOD__ . "\n";
39        return $this->a[$this->i];
40    }
41
42    public function next(): void
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): void
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--EXPECT--
92NumericArrayIterator::__construct
93NumericArrayIterator::rewind
94NumericArrayIterator::valid(true)
95NumericArrayIterator::next
96NumericArrayIterator::valid(true)
97NumericArrayIterator::valid(true)
98NumericArrayIterator::current
99NumericArrayIterator::key
100NumericArrayIterator::greaterThan(2)
101NumericArrayIterator::current
1022 is less than or equal 2
103NumericArrayIterator::next
104NumericArrayIterator::valid(true)
105NumericArrayIterator::current
106NumericArrayIterator::key
107NumericArrayIterator::greaterThan(2)
108NumericArrayIterator::current
1093 is greater than 2
110NumericArrayIterator::next
111NumericArrayIterator::valid(true)
112NumericArrayIterator::current
113NumericArrayIterator::key
114NumericArrayIterator::greaterThan(2)
115NumericArrayIterator::current
1164 is greater than 2
117NumericArrayIterator::next
118===SEEKABLE===
119NumericArrayIterator::__construct
120NumericArrayIterator::rewind
121SeekableNumericArrayIterator::seek(1)
122NumericArrayIterator::valid(true)
123NumericArrayIterator::current
124NumericArrayIterator::key
125SeekableNumericArrayIterator::greaterThan(2)
126NumericArrayIterator::current
1272 is less than or equal 2
128NumericArrayIterator::next
129NumericArrayIterator::valid(true)
130NumericArrayIterator::current
131NumericArrayIterator::key
132SeekableNumericArrayIterator::greaterThan(2)
133NumericArrayIterator::current
1343 is greater than 2
135NumericArrayIterator::next
136NumericArrayIterator::valid(true)
137NumericArrayIterator::current
138NumericArrayIterator::key
139SeekableNumericArrayIterator::greaterThan(2)
140NumericArrayIterator::current
1414 is greater than 2
142NumericArrayIterator::next
143===STACKED===
144Shows '2 is greater than 2' because the test is actually done with the current value which is 3.
145NumericArrayIterator::__construct
146NumericArrayIterator::rewind
147SeekableNumericArrayIterator::seek(1)
148NumericArrayIterator::valid(true)
149NumericArrayIterator::current
150NumericArrayIterator::key
151NumericArrayIterator::next
152NumericArrayIterator::valid(true)
153NumericArrayIterator::current
154NumericArrayIterator::key
155SeekableNumericArrayIterator::greaterThan(2)
156NumericArrayIterator::current
1572 is greater than 2
158NumericArrayIterator::next
159NumericArrayIterator::valid(true)
160NumericArrayIterator::current
161NumericArrayIterator::key
162SeekableNumericArrayIterator::greaterThan(2)
163NumericArrayIterator::current
1643 is greater than 2
165NumericArrayIterator::next
166SeekableNumericArrayIterator::greaterThan(2)
167NumericArrayIterator::current
1684 is greater than 2
169