xref: /PHP-8.4/ext/spl/tests/iterator_004.phpt (revision b3e08881)
1--TEST--
2SPL: SeekableIterator and string keys
3--FILE--
4<?php
5
6class NumericArrayIterator implements Iterator
7{
8    protected $a;
9    protected $i;
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
49class SeekableNumericArrayIterator extends NumericArrayIterator implements SeekableIterator
50{
51    public function seek($index): void
52    {
53        if ($index < count($this->a)) {
54            $this->i = $index;
55        }
56        echo __METHOD__ . '(' . $index . ")\n";
57    }
58}
59
60$a = array(1, 2, 3, 4, 5);
61foreach (new LimitIterator(new NumericArrayIterator($a), 1, 3) as $v)
62{
63    print "$v\n";
64}
65
66echo "===SEEKABLE===\n";
67$a = array(1, 2, 3, 4, 5);
68foreach(new LimitIterator(new SeekableNumericArrayIterator($a), 1, 3) as $v)
69{
70    print "$v\n";
71}
72
73echo "===SEEKING===\n";
74$a = array(1, 2, 3, 4, 5);
75$l = new LimitIterator(new SeekableNumericArrayIterator($a));
76for($i = 1; $i < 4; $i++)
77{
78    $l->seek($i);
79    print $l->current() . "\n";
80}
81
82?>
83--EXPECT--
84NumericArrayIterator::__construct
85NumericArrayIterator::rewind
86NumericArrayIterator::valid(true)
87NumericArrayIterator::next
88NumericArrayIterator::valid(true)
89NumericArrayIterator::valid(true)
90NumericArrayIterator::current
91NumericArrayIterator::key
922
93NumericArrayIterator::next
94NumericArrayIterator::valid(true)
95NumericArrayIterator::current
96NumericArrayIterator::key
973
98NumericArrayIterator::next
99NumericArrayIterator::valid(true)
100NumericArrayIterator::current
101NumericArrayIterator::key
1024
103NumericArrayIterator::next
104===SEEKABLE===
105NumericArrayIterator::__construct
106NumericArrayIterator::rewind
107SeekableNumericArrayIterator::seek(1)
108NumericArrayIterator::valid(true)
109NumericArrayIterator::current
110NumericArrayIterator::key
1112
112NumericArrayIterator::next
113NumericArrayIterator::valid(true)
114NumericArrayIterator::current
115NumericArrayIterator::key
1163
117NumericArrayIterator::next
118NumericArrayIterator::valid(true)
119NumericArrayIterator::current
120NumericArrayIterator::key
1214
122NumericArrayIterator::next
123===SEEKING===
124NumericArrayIterator::__construct
125SeekableNumericArrayIterator::seek(1)
126NumericArrayIterator::valid(true)
127NumericArrayIterator::current
128NumericArrayIterator::key
1292
130SeekableNumericArrayIterator::seek(2)
131NumericArrayIterator::valid(true)
132NumericArrayIterator::current
133NumericArrayIterator::key
1343
135SeekableNumericArrayIterator::seek(3)
136NumericArrayIterator::valid(true)
137NumericArrayIterator::current
138NumericArrayIterator::key
1394
140