xref: /PHP-8.0/ext/spl/tests/iterator_039.phpt (revision f8d79582)
1--TEST--
2SPL: LimitIterator and backward seeking
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 valid()
18    {
19        echo __METHOD__ . "\n";
20        return $this->i < count($this->a);
21    }
22
23    public function rewind()
24    {
25        echo __METHOD__ . "\n";
26        $this->i = 0;
27    }
28
29    public function key()
30    {
31        echo __METHOD__ . "\n";
32        return $this->i;
33    }
34
35    public function current()
36    {
37        echo __METHOD__ . "\n";
38        return $this->a[$this->i];
39    }
40
41    public function next()
42    {
43        echo __METHOD__ . "\n";
44        $this->i++;
45    }
46}
47
48$it = new LimitIterator(new NumericArrayIterator(array(12, 25, 42, 56)));
49
50foreach($it as $k => $v)
51{
52    var_dump($k);
53    var_dump($v);
54}
55
56echo "===SEEK===\n";
57
58$it->seek(2);
59
60echo "===LOOP===\n";
61
62foreach(new NoRewindIterator($it) as $k => $v)
63{
64    var_dump($k);
65    var_dump($v);
66}
67
68?>
69--EXPECT--
70NumericArrayIterator::__construct
71NumericArrayIterator::rewind
72NumericArrayIterator::valid
73NumericArrayIterator::valid
74NumericArrayIterator::current
75NumericArrayIterator::key
76int(0)
77int(12)
78NumericArrayIterator::next
79NumericArrayIterator::valid
80NumericArrayIterator::current
81NumericArrayIterator::key
82int(1)
83int(25)
84NumericArrayIterator::next
85NumericArrayIterator::valid
86NumericArrayIterator::current
87NumericArrayIterator::key
88int(2)
89int(42)
90NumericArrayIterator::next
91NumericArrayIterator::valid
92NumericArrayIterator::current
93NumericArrayIterator::key
94int(3)
95int(56)
96NumericArrayIterator::next
97NumericArrayIterator::valid
98===SEEK===
99NumericArrayIterator::rewind
100NumericArrayIterator::valid
101NumericArrayIterator::next
102NumericArrayIterator::valid
103NumericArrayIterator::next
104NumericArrayIterator::valid
105NumericArrayIterator::valid
106NumericArrayIterator::current
107NumericArrayIterator::key
108===LOOP===
109int(2)
110int(42)
111NumericArrayIterator::next
112NumericArrayIterator::valid
113NumericArrayIterator::current
114NumericArrayIterator::key
115int(3)
116int(56)
117NumericArrayIterator::next
118NumericArrayIterator::valid
119