xref: /PHP-8.3/ext/spl/tests/iterator_007.phpt (revision b3e08881)
1--TEST--
2SPL: NoRewindIterator
3--FILE--
4<?php
5
6class ArrayIteratorEx extends ArrayIterator
7{
8    function rewind(): void
9    {
10        echo __METHOD__ . "\n";
11        parent::rewind();
12    }
13    function valid(): bool
14    {
15        echo __METHOD__ . "\n";
16        return parent::valid();
17    }
18    function current(): mixed
19    {
20        echo __METHOD__ . "\n";
21        return parent::current();
22    }
23    function key(): string|int|null
24    {
25        echo __METHOD__ . "\n";
26        return parent::key();
27    }
28    function next(): void
29    {
30        echo __METHOD__ . "\n";
31        parent::next();
32    }
33}
34
35class NoRewindIteratorEx extends NoRewindIterator
36{
37    function rewind(): void
38    {
39        echo __METHOD__ . "\n";
40        parent::rewind();
41    }
42    function valid(): bool
43    {
44        echo __METHOD__ . "\n";
45        return parent::valid();
46    }
47    function current(): mixed
48    {
49        echo __METHOD__ . "\n";
50        return parent::current();
51    }
52    function key(): mixed
53    {
54        echo __METHOD__ . "\n";
55        return parent::key();
56    }
57    function next(): void
58    {
59        echo __METHOD__ . "\n";
60        parent::next();
61    }
62}
63
64$it = new NoRewindIteratorEx(new ArrayIteratorEx(range(0,3)));
65
66echo "===0===\n";
67foreach ($it->getInnerIterator() as $v) {
68    var_dump($v);
69}
70
71echo "===1===\n";
72foreach ($it as $v) {
73    var_dump($v);
74}
75
76$pos =0;
77
78$it = new NoRewindIteratorEx(new ArrayIteratorEx(range(0,3)));
79
80echo "===2===\n";
81foreach ($it as $v) {
82    var_dump($v);
83    if ($pos++ > 1) {
84        break;
85    }
86}
87
88echo "===3===\n";
89foreach ($it as $v) {
90    var_dump($v);
91}
92
93echo "===4===\n";
94foreach ($it as $v) {
95    var_dump($v);
96}
97?>
98--EXPECT--
99===0===
100ArrayIteratorEx::rewind
101ArrayIteratorEx::valid
102ArrayIteratorEx::current
103int(0)
104ArrayIteratorEx::next
105ArrayIteratorEx::valid
106ArrayIteratorEx::current
107int(1)
108ArrayIteratorEx::next
109ArrayIteratorEx::valid
110ArrayIteratorEx::current
111int(2)
112ArrayIteratorEx::next
113ArrayIteratorEx::valid
114ArrayIteratorEx::current
115int(3)
116ArrayIteratorEx::next
117ArrayIteratorEx::valid
118===1===
119NoRewindIteratorEx::rewind
120NoRewindIteratorEx::valid
121ArrayIteratorEx::valid
122===2===
123NoRewindIteratorEx::rewind
124NoRewindIteratorEx::valid
125ArrayIteratorEx::valid
126NoRewindIteratorEx::current
127ArrayIteratorEx::current
128int(0)
129NoRewindIteratorEx::next
130ArrayIteratorEx::next
131NoRewindIteratorEx::valid
132ArrayIteratorEx::valid
133NoRewindIteratorEx::current
134ArrayIteratorEx::current
135int(1)
136NoRewindIteratorEx::next
137ArrayIteratorEx::next
138NoRewindIteratorEx::valid
139ArrayIteratorEx::valid
140NoRewindIteratorEx::current
141ArrayIteratorEx::current
142int(2)
143===3===
144NoRewindIteratorEx::rewind
145NoRewindIteratorEx::valid
146ArrayIteratorEx::valid
147NoRewindIteratorEx::current
148int(2)
149NoRewindIteratorEx::next
150ArrayIteratorEx::next
151NoRewindIteratorEx::valid
152ArrayIteratorEx::valid
153NoRewindIteratorEx::current
154ArrayIteratorEx::current
155int(3)
156NoRewindIteratorEx::next
157ArrayIteratorEx::next
158NoRewindIteratorEx::valid
159ArrayIteratorEx::valid
160===4===
161NoRewindIteratorEx::rewind
162NoRewindIteratorEx::valid
163ArrayIteratorEx::valid
164