xref: /PHP-7.0/ext/spl/tests/iterator_004.phpt (revision 610c7fbe)
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()
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
49class SeekableNumericArrayIterator extends NumericArrayIterator implements SeekableIterator
50{
51	public function seek($index)
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===DONE===
84<?php exit(0); ?>
85--EXPECT--
86NumericArrayIterator::__construct
87NumericArrayIterator::rewind
88NumericArrayIterator::valid(true)
89NumericArrayIterator::next
90NumericArrayIterator::valid(true)
91NumericArrayIterator::valid(true)
92NumericArrayIterator::current
93NumericArrayIterator::key
942
95NumericArrayIterator::next
96NumericArrayIterator::valid(true)
97NumericArrayIterator::current
98NumericArrayIterator::key
993
100NumericArrayIterator::next
101NumericArrayIterator::valid(true)
102NumericArrayIterator::current
103NumericArrayIterator::key
1044
105NumericArrayIterator::next
106===SEEKABLE===
107NumericArrayIterator::__construct
108NumericArrayIterator::rewind
109SeekableNumericArrayIterator::seek(1)
110NumericArrayIterator::valid(true)
111NumericArrayIterator::current
112NumericArrayIterator::key
1132
114NumericArrayIterator::next
115NumericArrayIterator::valid(true)
116NumericArrayIterator::current
117NumericArrayIterator::key
1183
119NumericArrayIterator::next
120NumericArrayIterator::valid(true)
121NumericArrayIterator::current
122NumericArrayIterator::key
1234
124NumericArrayIterator::next
125===SEEKING===
126NumericArrayIterator::__construct
127SeekableNumericArrayIterator::seek(1)
128NumericArrayIterator::valid(true)
129NumericArrayIterator::current
130NumericArrayIterator::key
1312
132SeekableNumericArrayIterator::seek(2)
133NumericArrayIterator::valid(true)
134NumericArrayIterator::current
135NumericArrayIterator::key
1363
137SeekableNumericArrayIterator::seek(3)
138NumericArrayIterator::valid(true)
139NumericArrayIterator::current
140NumericArrayIterator::key
1414
142===DONE===
143