1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Modulate\Artisan\Interceptor; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputOption; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
|
9
|
|
|
class OptionBuilder |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name of the option |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected string $name = ''; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The shortcut(s) for the option |
19
|
|
|
* |
20
|
|
|
* @var string|array|null |
21
|
|
|
*/ |
22
|
|
|
protected string|array|null $shortcut = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The mode (mask) of the option |
26
|
|
|
* @var int|null |
27
|
|
|
*/ |
28
|
|
|
protected int|null $mode = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The description of the option |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected string $description = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The default value of the option (must be null for VALUE_NEGATABLE and VALUE_NONE modes) |
38
|
|
|
*/ |
39
|
|
|
protected string|bool|int|float|array|null $default = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* An array of suggested values or a closure returning the suggested values |
43
|
|
|
* @var array|Closure |
44
|
|
|
*/ |
45
|
|
|
protected array|Closure $suggestedValues = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $name the name of the option |
49
|
|
|
* |
50
|
|
|
* @return OptionBuilder |
51
|
|
|
*/ |
52
|
11 |
|
public function name($name): OptionBuilder |
53
|
|
|
{ |
54
|
11 |
|
$this->name = $name; |
55
|
|
|
|
56
|
11 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts |
61
|
|
|
*/ |
62
|
|
|
public function shortcut(string|array $shortcut = null): OptionBuilder |
63
|
|
|
{ |
64
|
|
|
$this->shortcut = $shortcut; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Adds a mode mask option to the option |
71
|
|
|
* See InputOption::VALUE_* |
72
|
|
|
* |
73
|
|
|
* @param integer $mode The mode to use |
74
|
|
|
* @param boolean $clear Whether to reset the existing mode (as to prevent an invalid exception being thrown by the InputOption) |
75
|
|
|
* @return OptionBuilder |
76
|
|
|
*/ |
77
|
10 |
|
public function mode(int $mode, $clear = false): OptionBuilder |
78
|
|
|
{ |
79
|
10 |
|
if ($clear === true) { |
80
|
1 |
|
$this->mode = 0; |
81
|
1 |
|
$this->mode |= $mode; |
82
|
|
|
} else { |
83
|
9 |
|
$this->mode |= $mode; |
84
|
|
|
} |
85
|
|
|
|
86
|
10 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Checks if the bit is active in the current mask |
91
|
|
|
* |
92
|
|
|
* @param integer $mode |
93
|
|
|
* @return boolean |
94
|
|
|
*/ |
95
|
1 |
|
public function hasMode(int $mode): bool |
96
|
|
|
{ |
97
|
1 |
|
return ($this->mode & $mode) ? true : false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Removes the mode from the current mask |
102
|
|
|
* |
103
|
|
|
* @param integer $mode |
104
|
|
|
* @return OptionBuilder |
105
|
|
|
*/ |
106
|
7 |
|
protected function removeMode(int $mode): OptionBuilder |
107
|
|
|
{ |
108
|
7 |
|
$this->mode &= ~$mode; |
109
|
7 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Mark the option as reqired |
114
|
|
|
* |
115
|
|
|
* @return OptionBuilder |
116
|
|
|
*/ |
117
|
4 |
|
public function required(): OptionBuilder |
118
|
|
|
{ |
119
|
4 |
|
$this->mode(InputOption::VALUE_REQUIRED); |
120
|
4 |
|
$this->removeMode(InputOption::VALUE_OPTIONAL); |
121
|
4 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Mark the option as required |
126
|
|
|
* |
127
|
|
|
* @return OptionBuilder |
128
|
|
|
*/ |
129
|
3 |
|
public function optional(): OptionBuilder |
130
|
|
|
{ |
131
|
3 |
|
$this->mode(InputOption::VALUE_OPTIONAL); |
132
|
3 |
|
$this->removeMode(InputOption::VALUE_REQUIRED); |
133
|
3 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Mark the option as accepting an array |
138
|
|
|
* |
139
|
|
|
* @return OptionBuilder |
140
|
|
|
*/ |
141
|
2 |
|
public function array(): OptionBuilder |
142
|
|
|
{ |
143
|
2 |
|
$this->mode(InputOption::VALUE_IS_ARRAY); |
144
|
2 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Provide a default value for the option |
149
|
|
|
* |
150
|
|
|
* @param array|bool|float|int|string|null $default |
151
|
|
|
* @return OptionBuilder |
152
|
|
|
*/ |
153
|
2 |
|
public function default(array|bool|float|int|string|null $default = null): OptionBuilder |
154
|
|
|
{ |
155
|
2 |
|
$this->default = $default; |
156
|
|
|
|
157
|
2 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Mark the option as negatable e.g. --foo and --no-foo |
162
|
|
|
* |
163
|
|
|
* @return OptionBuilder |
164
|
|
|
*/ |
165
|
1 |
|
public function negatable(): OptionBuilder |
166
|
|
|
{ |
167
|
1 |
|
$this->mode(InputOption::VALUE_NEGATABLE); |
168
|
1 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Mark the option as not accepting input and only being a flag e.g. --yes |
173
|
|
|
* |
174
|
|
|
* @return OptionBuilder |
175
|
|
|
*/ |
176
|
1 |
|
public function flag(): OptionBuilder |
177
|
|
|
{ |
178
|
1 |
|
$this->mode(InputOption::VALUE_NONE, true); |
179
|
1 |
|
$this->default = null; |
180
|
|
|
|
181
|
1 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Set a description for the option |
186
|
|
|
* |
187
|
|
|
* @param string $description |
188
|
|
|
* @return OptionBuilder |
189
|
|
|
*/ |
190
|
1 |
|
public function description(string $description): OptionBuilder |
191
|
|
|
{ |
192
|
1 |
|
$this->description = $description; |
193
|
|
|
|
194
|
1 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Return the built option |
200
|
|
|
* |
201
|
|
|
* @return InputOption |
202
|
|
|
*/ |
203
|
11 |
|
public function get(): InputOption |
204
|
|
|
{ |
205
|
11 |
|
return new InputOption($this->name, $this->shortcut, $this->mode, $this->description, $this->default, $this->suggestedValues); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
} |