1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\CLImate\Argument; |
4
|
|
|
|
5
|
|
|
class Argument |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* An argument's name. |
9
|
|
|
* |
10
|
|
|
* Use this name when internally referring to the argument. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $name; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* An argument's short representation. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $prefix; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* An argument's long representation. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $longPrefix; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* An argument's description. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $description; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Whether or not an argument is required. |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $required = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Whether or not an argument only needs to be defined to have a value. |
46
|
|
|
* |
47
|
|
|
* These arguments have the value true when they are defined on the command |
48
|
|
|
* line. |
49
|
|
|
* |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
protected $noValue = false; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Which data type to cast an argument's value to. |
56
|
|
|
* |
57
|
|
|
* Valid data types are "string", "int", "float", and "bool". |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $castTo = 'string'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* An argument's default value. |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected $defaultValue = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* An argument's value, after type casting. |
72
|
|
|
* |
73
|
|
|
* @var string[]|int[]|float[]|bool[] |
74
|
|
|
*/ |
75
|
|
|
protected $values = []; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Build a new command argument. |
79
|
|
|
* |
80
|
|
|
* @param string $name |
81
|
|
|
*/ |
82
|
48 |
|
public function __construct($name) |
83
|
|
|
{ |
84
|
48 |
|
$this->setName($name); |
85
|
48 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Build a new command argument from an array. |
89
|
|
|
* |
90
|
|
|
* @param string $name |
91
|
|
|
* @param array $params |
92
|
|
|
* |
93
|
|
|
* @return Argument |
94
|
|
|
*/ |
95
|
48 |
|
public static function createFromArray($name, array $params) |
96
|
|
|
{ |
97
|
48 |
|
$argument = new Argument($name); |
98
|
48 |
|
$params = self::getSettableArgumentParams($params); |
99
|
|
|
|
100
|
48 |
|
foreach ($params as $key => $value) { |
101
|
48 |
|
$method = 'set' . ucwords($key); |
102
|
48 |
|
$argument->{$method}($value); |
103
|
44 |
|
} |
104
|
|
|
|
105
|
44 |
|
return $argument; |
106
|
8 |
|
} |
107
|
8 |
|
|
108
|
|
|
/** |
109
|
44 |
|
* Get argument params based on settable properties |
110
|
|
|
* |
111
|
|
|
* @param array $params |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
protected static function getSettableArgumentParams(array $params) |
116
|
|
|
{ |
117
|
|
|
$allowed = [ |
118
|
|
|
'prefix', |
119
|
48 |
|
'longPrefix', |
120
|
|
|
'description', |
121
|
|
|
'required', |
122
|
48 |
|
'noValue', |
123
|
48 |
|
'castTo', |
124
|
48 |
|
'defaultValue', |
125
|
48 |
|
]; |
126
|
48 |
|
|
127
|
48 |
|
return array_intersect_key($params, array_flip($allowed)); |
128
|
48 |
|
} |
129
|
48 |
|
|
130
|
|
|
/** |
131
|
48 |
|
* Retrieve an argument's name. |
132
|
|
|
* |
133
|
|
|
* Use this name when internally referring to the argument. |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function name() |
138
|
|
|
{ |
139
|
|
|
return $this->name; |
140
|
|
|
} |
141
|
24 |
|
|
142
|
|
|
/** |
143
|
24 |
|
* Set an argument's name. |
144
|
|
|
* |
145
|
|
|
* Use this name when internally referring to the argument. |
146
|
|
|
* |
147
|
|
|
* @param string $name |
148
|
|
|
*/ |
149
|
|
|
protected function setName($name) |
150
|
|
|
{ |
151
|
|
|
$this->name = trim($name); |
152
|
|
|
} |
153
|
48 |
|
|
154
|
|
|
/** |
155
|
48 |
|
* Retrieve an argument's short form. |
156
|
48 |
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function prefix() |
160
|
|
|
{ |
161
|
|
|
return $this->prefix; |
162
|
|
|
} |
163
|
24 |
|
|
164
|
|
|
/** |
165
|
24 |
|
* Set an argument's short form. |
166
|
|
|
* |
167
|
|
|
* @param string $prefix |
168
|
|
|
*/ |
169
|
|
|
protected function setPrefix($prefix) |
170
|
|
|
{ |
171
|
|
|
$this->prefix = trim($prefix); |
172
|
|
|
} |
173
|
20 |
|
|
174
|
|
|
/** |
175
|
20 |
|
* Retrieve an argument's long form. |
176
|
20 |
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function longPrefix() |
180
|
|
|
{ |
181
|
|
|
return $this->longPrefix; |
182
|
|
|
} |
183
|
24 |
|
|
184
|
|
|
/** |
185
|
24 |
|
* Set an argument's short form. |
186
|
|
|
* |
187
|
|
|
* @param string $longPrefix |
188
|
|
|
*/ |
189
|
|
|
protected function setLongPrefix($longPrefix) |
190
|
|
|
{ |
191
|
|
|
$this->longPrefix = trim($longPrefix); |
192
|
|
|
} |
193
|
20 |
|
|
194
|
|
|
/** |
195
|
20 |
|
* Determine if an argument has a prefix. |
196
|
20 |
|
* |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
|
|
public function hasPrefix() |
200
|
|
|
{ |
201
|
|
|
return $this->prefix() || $this->longPrefix(); |
202
|
|
|
} |
203
|
20 |
|
|
204
|
|
|
/** |
205
|
20 |
|
* Retrieve an argument's description. |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function description() |
210
|
|
|
{ |
211
|
|
|
return $this->description; |
212
|
|
|
} |
213
|
4 |
|
|
214
|
|
|
/** |
215
|
4 |
|
* Set an argument's description. |
216
|
|
|
* |
217
|
|
|
* @param string $description |
218
|
|
|
*/ |
219
|
|
|
protected function setDescription($description) |
220
|
|
|
{ |
221
|
|
|
$this->description = trim($description); |
222
|
|
|
} |
223
|
4 |
|
|
224
|
|
|
/** |
225
|
4 |
|
* Determine whether or not an argument is required. |
226
|
4 |
|
* |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
|
|
public function isRequired() |
230
|
|
|
{ |
231
|
|
|
return $this->required; |
232
|
|
|
} |
233
|
20 |
|
|
234
|
|
|
/** |
235
|
20 |
|
* Set whether an argument is required or not. |
236
|
|
|
* |
237
|
|
|
* @param bool $required |
238
|
|
|
*/ |
239
|
|
|
protected function setRequired($required) |
240
|
|
|
{ |
241
|
|
|
$this->required = (bool) $required; |
242
|
|
|
} |
243
|
8 |
|
|
244
|
|
|
/** |
245
|
8 |
|
* Determine whether or not an argument only needs to be defined to have a |
246
|
8 |
|
* value. |
247
|
|
|
* |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
|
|
public function noValue() |
251
|
|
|
{ |
252
|
|
|
return $this->noValue; |
253
|
|
|
} |
254
|
40 |
|
|
255
|
|
|
/** |
256
|
40 |
|
* Set whether or not an argument only needs to be defined to have a value. |
257
|
|
|
* |
258
|
|
|
* @param bool $noValue |
259
|
|
|
*/ |
260
|
|
|
protected function setNoValue($noValue) |
261
|
|
|
{ |
262
|
|
|
$this->setCastTo('bool'); |
263
|
|
|
$this->noValue = (bool) $noValue; |
264
|
12 |
|
} |
265
|
|
|
|
266
|
12 |
|
/** |
267
|
12 |
|
* Retrieve the data type to cast an argument's value to. |
268
|
12 |
|
* |
269
|
|
|
* @return string |
270
|
|
|
*/ |
271
|
|
|
public function castTo() |
272
|
|
|
{ |
273
|
|
|
return $this->castTo; |
274
|
|
|
} |
275
|
4 |
|
|
276
|
|
|
/** |
277
|
4 |
|
* Set the data type to cast an argument's value to. |
278
|
|
|
* |
279
|
|
|
* Valid data types are "string", "int", "float", and "bool". |
280
|
|
|
* |
281
|
|
|
* @throws \Exception if $castTo is not a valid data type. |
282
|
|
|
* @param string $castTo |
283
|
|
|
*/ |
284
|
|
|
protected function setCastTo($castTo) |
285
|
|
|
{ |
286
|
|
|
if (!in_array($castTo, ['string', 'int', 'float', 'bool'])) { |
287
|
|
|
throw new \Exception( |
288
|
32 |
|
"An argument may only be cast to the data type " |
289
|
|
|
. "'string', 'int', 'float', or 'bool'." |
290
|
32 |
|
); |
291
|
4 |
|
} |
292
|
|
|
|
293
|
|
|
$this->castTo = $this->noValue() ? 'bool' : $castTo; |
294
|
4 |
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
28 |
|
* Retrieve an argument's default values. |
298
|
28 |
|
* |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
|
|
public function defaultValue() |
302
|
|
|
{ |
303
|
|
|
return $this->defaultValue; |
304
|
|
|
} |
305
|
44 |
|
|
306
|
|
|
/** |
307
|
44 |
|
* Set an argument's default value. |
308
|
|
|
* |
309
|
|
|
* @param string $defaultValue |
310
|
|
|
*/ |
311
|
|
|
public function setDefaultValue($defaultValue) |
312
|
|
|
{ |
313
|
|
|
$this->defaultValue = (array) $defaultValue; |
|
|
|
|
314
|
|
|
} |
315
|
8 |
|
|
316
|
|
|
/** |
317
|
8 |
|
* Retrieve an argument's value. |
318
|
8 |
|
* |
319
|
|
|
* Argument values are type cast based on the value of $castTo. |
320
|
|
|
* |
321
|
|
|
* @return string|int|float|bool |
322
|
|
|
*/ |
323
|
|
|
public function value() |
324
|
|
|
{ |
325
|
|
|
if ($this->values) { |
|
|
|
|
326
|
|
|
return end($this->values); |
327
|
28 |
|
} |
328
|
|
|
$cast_method = 'castTo' . ucwords($this->castTo); |
329
|
28 |
|
return $this->{$cast_method}(current($this->defaultValue())); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Retrieve an argument's value. |
334
|
|
|
* |
335
|
|
|
* Argument values are type cast based on the value of $castTo. |
336
|
|
|
* |
337
|
|
|
* @return string[]|int[]|float[]|bool[] |
338
|
|
|
*/ |
339
|
36 |
|
public function valueArray() |
340
|
|
|
{ |
341
|
36 |
|
if ($this->values) { |
|
|
|
|
342
|
36 |
|
return $this->values; |
343
|
36 |
|
} |
344
|
|
|
$cast_method = 'castTo' . ucwords($this->castTo); |
345
|
|
|
return array_map([$this, $cast_method], $this->defaultValue()); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Set an argument's value based on its command line entry. |
350
|
24 |
|
* |
351
|
|
|
* Argument values are type cast based on the value of $castTo. |
352
|
24 |
|
* |
353
|
|
|
* @param string|bool $value |
354
|
|
|
*/ |
355
|
|
|
public function setValue($value) |
356
|
|
|
{ |
357
|
|
|
$cast_method = 'castTo' . ucwords($this->castTo); |
358
|
|
|
$this->values[] = $this->{$cast_method}($value); |
359
|
|
|
} |
360
|
4 |
|
|
361
|
|
|
/** |
362
|
4 |
|
* @param string $value |
363
|
|
|
* |
364
|
|
|
* @return string |
365
|
|
|
*/ |
366
|
|
|
protected function castToString($value) |
367
|
|
|
{ |
368
|
|
|
return (string) $value; |
369
|
|
|
} |
370
|
4 |
|
|
371
|
|
|
/** |
372
|
4 |
|
* @param string $value |
373
|
|
|
* |
374
|
|
|
* @return int |
375
|
|
|
*/ |
376
|
|
|
protected function castToInt($value) |
377
|
|
|
{ |
378
|
|
|
return (int) $value; |
379
|
|
|
} |
380
|
8 |
|
|
381
|
|
|
/** |
382
|
8 |
|
* @param string $value |
383
|
|
|
* |
384
|
|
|
* @return float |
385
|
|
|
*/ |
386
|
|
|
protected function castToFloat($value) |
387
|
|
|
{ |
388
|
|
|
return (float) $value; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @param string $value |
393
|
|
|
* |
394
|
|
|
* @return bool |
395
|
|
|
*/ |
396
|
|
|
protected function castToBool($value) |
397
|
|
|
{ |
398
|
|
|
return (bool) $value; |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..