1+ <?php
2+
3+ namespace Phperf \Pipeline \Page ;
4+
5+ use ArrayIterator ;
6+ use Iterator ;
7+
8+ class PageIterator implements Iterator
9+ {
10+ private $ iterators ;
11+ /**
12+ * @var Iterator
13+ */
14+ private $ currentIterator ;
15+
16+ function __construct (Iterator $ iterators )
17+ {
18+ $ this ->iterators = $ iterators ;
19+ }
20+
21+ /**
22+ * (PHP 5 >= 5.0.0)<br/>
23+ * Return the current element
24+ * @link http://php.net/manual/en/iterator.current.php
25+ * @return mixed Can return any type.
26+ */
27+ public function current ()
28+ {
29+ return $ this ->currentIterator ->current ();
30+ }
31+
32+ /**
33+ * (PHP 5 >= 5.0.0)<br/>
34+ * Move forward to next element
35+ * @link http://php.net/manual/en/iterator.next.php
36+ * @return void Any returned value is ignored.
37+ */
38+ public function next ()
39+ {
40+ $ this ->currentIterator ->next ();
41+ }
42+
43+ /**
44+ * (PHP 5 >= 5.0.0)<br/>
45+ * Return the key of the current element
46+ * @link http://php.net/manual/en/iterator.key.php
47+ * @return mixed scalar on success, or null on failure.
48+ */
49+ public function key ()
50+ {
51+ //echo 'k';
52+ return $ this ->currentIterator ->key ();
53+ }
54+
55+ /**
56+ * (PHP 5 >= 5.0.0)<br/>
57+ * Checks if current position is valid
58+ * @link http://php.net/manual/en/iterator.valid.php
59+ * @return boolean The return value will be casted to boolean and then evaluated.
60+ * Returns true on success or false on failure.
61+ */
62+ public function valid ()
63+ {
64+ $ v = $ this ->currentIterator ->valid ();
65+ if (!$ v ) {
66+ do {
67+ $ this ->iterators ->next ();
68+ if ($ this ->iterators ->valid ()) {
69+ $ this ->currentIterator = $ this ->iterators ->current ();
70+ $ this ->currentIterator ->rewind ();
71+ }
72+ } while ($ this ->iterators ->valid () && !$ this ->currentIterator ->valid ());
73+
74+ if (!$ this ->currentIterator ->valid ()) {
75+ return false ;
76+ } else {
77+ return true ;
78+ }
79+ }
80+
81+ return $ v ;
82+ }
83+
84+ /**
85+ * (PHP 5 >= 5.0.0)<br/>
86+ * Rewind the Iterator to the first element
87+ * @link http://php.net/manual/en/iterator.rewind.php
88+ * @return void Any returned value is ignored.
89+ */
90+ public function rewind ()
91+ {
92+ $ this ->iterators ->rewind ();
93+ if (!$ this ->iterators ->valid ()) {
94+ $ this ->currentIterator = new ArrayIterator (array ());
95+ } else {
96+ $ this ->currentIterator = $ this ->iterators ->current ();
97+ }
98+ $ this ->currentIterator ->rewind ();
99+ }
100+
101+ }
0 commit comments