1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\File\Pipeline; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class StepsIterator |
9
|
|
|
* |
10
|
|
|
* @package Ktomk\Pipelines\File\Pipeline |
11
|
|
|
* |
12
|
|
|
* @template-implements \Iterator<int, Step> |
13
|
|
|
*/ |
14
|
|
|
class StepsIterator implements \Iterator |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Iterator |
18
|
|
|
*/ |
19
|
|
|
private $inner; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var null|int |
23
|
|
|
*/ |
24
|
|
|
private $index; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var null|Step |
28
|
|
|
*/ |
29
|
|
|
private $current; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool override trigger: manual in iteration |
33
|
|
|
*/ |
34
|
|
|
private $noManual = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* StepsIterator constructor. |
38
|
|
|
* |
39
|
|
|
* @param \Iterator $iterator |
40
|
|
|
*/ |
41
|
3 |
|
public function __construct(\Iterator $iterator) |
42
|
|
|
{ |
43
|
3 |
|
$this->inner = $iterator; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return null|int |
48
|
|
|
*/ |
49
|
1 |
|
public function getIndex() |
50
|
|
|
{ |
51
|
1 |
|
return $this->index; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Index of the pipeline step being iterated |
56
|
|
|
* |
57
|
|
|
* Undefined behaviour if the iteration has not yet been |
58
|
|
|
* started (e.g. the iterator has not yet been rewound) |
59
|
|
|
* |
60
|
|
|
* @return null|int |
61
|
|
|
*/ |
62
|
1 |
|
public function getStepIndex() |
63
|
|
|
{ |
64
|
1 |
|
return isset($this->current) ? $this->current->getIndex() : null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Iteration might stop at a manual step. If |
69
|
|
|
* that is the case, isManual() will be true |
70
|
|
|
* *after* the iteration. |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
2 |
|
public function isManual() |
75
|
|
|
{ |
76
|
2 |
|
return 0 !== $this->index |
77
|
2 |
|
&& !$this->noManual |
78
|
2 |
|
&& $this->current() |
79
|
2 |
|
&& isset($this->current) |
80
|
2 |
|
&& $this->current->isManual(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @see \Iterator * */ |
84
|
|
|
|
85
|
2 |
|
#[\ReturnTypeWillChange] |
86
|
|
|
public function next() |
87
|
|
|
{ |
88
|
2 |
|
$this->index++; |
89
|
2 |
|
$this->inner->next(); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
#[\ReturnTypeWillChange] |
93
|
|
|
/** |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
|
|
public function key() |
97
|
|
|
{ |
98
|
2 |
|
return $this->inner->key(); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
#[\ReturnTypeWillChange] |
102
|
|
|
/** |
103
|
|
|
* @return Step |
104
|
|
|
*/ |
105
|
|
|
public function current() |
106
|
|
|
{ |
107
|
2 |
|
return $this->current = $this->inner->current(); |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
#[\ReturnTypeWillChange] |
111
|
|
|
public function valid() |
112
|
|
|
{ |
113
|
2 |
|
if ($this->isManual()) { |
114
|
1 |
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
return $this->inner->valid(); |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
#[\ReturnTypeWillChange] |
121
|
|
|
public function rewind() |
122
|
|
|
{ |
123
|
2 |
|
$this->index = 0; |
124
|
2 |
|
$this->inner->rewind(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param bool $noManual |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
1 |
|
public function setNoManual($noManual) |
133
|
|
|
{ |
134
|
1 |
|
$this->noManual = (bool)$noManual; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|