|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Phperf\Pipeline\Rows; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Iterator; |
|
7
|
|
|
|
|
8
|
|
|
class ClosureIterator implements Iterator |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var callable |
|
12
|
|
|
*/ |
|
13
|
|
|
private $source; |
|
14
|
|
|
private $currentIterator; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(Closure $source) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->source = $source; |
|
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; |
|
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
|
|
|
$c = $this->source; |
|
41
|
|
|
$this->currentIterator = $c(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
|
46
|
|
|
* Return the key of the current element |
|
47
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
|
48
|
|
|
* @return mixed scalar on success, or null on failure. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function key() |
|
51
|
|
|
{ |
|
52
|
|
|
return ''; |
|
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
|
|
|
return (bool)$this->currentIterator; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
|
69
|
|
|
* Rewind the Iterator to the first element |
|
70
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
|
71
|
|
|
* @return void Any returned value is ignored. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function rewind() |
|
74
|
|
|
{ |
|
75
|
|
|
$c = $this->source; |
|
76
|
|
|
$this->currentIterator = $c(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |