1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Cli\Args; |
6
|
|
|
|
7
|
|
|
use Iterator as PhpIterator; |
8
|
|
|
|
9
|
|
|
class Iterator extends Args implements PhpIterator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Args |
13
|
|
|
*/ |
14
|
|
|
private $args; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
private $index = 0; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $indexes = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Iterator constructor. |
28
|
|
|
* |
29
|
|
|
* @param Args $args |
30
|
|
|
*/ |
31
|
5 |
|
public function __construct(Args $args) |
32
|
|
|
{ |
33
|
5 |
|
$this->args = $args; |
34
|
5 |
|
$this->rewind(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* get next argument (look ahead) |
39
|
|
|
* |
40
|
|
|
* @return null|string next argument, null if there is no next argument |
41
|
|
|
*/ |
42
|
1 |
|
public function getNext() |
43
|
|
|
{ |
44
|
1 |
|
return $this->getByIndex($this->index + 1); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/* Iterator */ |
48
|
|
|
|
49
|
2 |
|
#[\ReturnTypeWillChange] |
50
|
|
|
/** |
51
|
|
|
* Return the current element |
52
|
|
|
* |
53
|
|
|
* @return null|string |
54
|
|
|
*/ |
55
|
|
|
public function current() |
56
|
|
|
{ |
57
|
2 |
|
if (!$this->valid()) { |
58
|
1 |
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
return $this->getByIndex($this->index); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
#[\ReturnTypeWillChange] |
65
|
|
|
/** |
66
|
|
|
* Move forward to next option |
67
|
|
|
*/ |
68
|
|
|
public function next() |
69
|
|
|
{ |
70
|
3 |
|
if (!$this->valid()) { |
71
|
1 |
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$this->index++; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
#[\ReturnTypeWillChange] |
78
|
|
|
/** |
79
|
|
|
* Return the key of the current option |
80
|
|
|
* |
81
|
|
|
* @return null|int index, zero based, null if no such index/key |
82
|
|
|
*/ |
83
|
|
|
public function key() |
84
|
|
|
{ |
85
|
1 |
|
return $this->getArgsIndex($this->index); |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
#[\ReturnTypeWillChange] |
89
|
|
|
/** |
90
|
|
|
* Checks if current position is valid |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function valid() |
95
|
|
|
{ |
96
|
4 |
|
$current = $this->getByIndex($this->index); |
97
|
|
|
|
98
|
4 |
|
return !(null === $current); |
99
|
|
|
} |
100
|
|
|
|
101
|
5 |
|
#[\ReturnTypeWillChange] |
102
|
|
|
/** |
103
|
|
|
* @return void Any returned value is ignored. |
104
|
|
|
*/ |
105
|
|
|
public function rewind() |
106
|
|
|
{ |
107
|
5 |
|
$this->index = 0; |
108
|
5 |
|
$this->indexes = array_keys($this->args->arguments); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param int $index |
113
|
|
|
* |
114
|
|
|
* @return null|string argument string or null if not found |
115
|
|
|
*/ |
116
|
4 |
|
private function getByIndex($index) |
117
|
|
|
{ |
118
|
4 |
|
$argsIndex = $this->getArgsIndex($index); |
119
|
4 |
|
if (null === $argsIndex) { |
120
|
3 |
|
return null; |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
if (!isset($this->args->arguments[$argsIndex])) { |
124
|
1 |
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
3 |
|
return $this->args->arguments[$argsIndex]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param int $index to retrieve index for |
132
|
|
|
* |
133
|
|
|
* @return null|int integer index of the argument, null if there is none |
134
|
|
|
*/ |
135
|
4 |
|
private function getArgsIndex($index) |
136
|
|
|
{ |
137
|
4 |
|
if (!isset($this->indexes[$index])) { |
138
|
3 |
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
3 |
|
return $this->indexes[$index]; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|