@@ -9,9 +9,20 @@ final class CommandArgument
9
9
private $ defaultValue ;
10
10
private ?string $ description ;
11
11
12
- public function __construct (string $ name , bool $ isRequired = false , $ defaultValue = null , ?string $ description = null )
12
+ public function __construct (string $ name , bool $ isRequired = false , $ defaultValue = null , ?string $ description = null )
13
13
{
14
- $ this ->name = $ name ;
14
+ if ($ name === '' ) {
15
+ throw new \InvalidArgumentException ("Option name cannot be empty. " );
16
+ }
17
+ if (!ctype_alpha ($ name )) {
18
+ throw new \InvalidArgumentException ("Option name must contain only letters. ' $ name' is invalid. " );
19
+ }
20
+
21
+ if ($ isRequired && $ defaultValue !== null ) {
22
+ throw new \LogicException ("Argument ' $ name' cannot be required and have a default value. " );
23
+ }
24
+
25
+ $ this ->name = strtolower ($ name );
15
26
$ this ->isRequired = $ isRequired ;
16
27
$ this ->defaultValue = $ defaultValue ;
17
28
$ this ->description = $ description ;
@@ -47,4 +58,13 @@ public function getDescription(): ?string
47
58
return $ this ->description ;
48
59
}
49
60
61
+ public static function required (string $ name , ?string $ description = null ): self
62
+ {
63
+ return new self ($ name , true , null , $ description );
64
+ }
65
+
66
+ public static function optional (string $ name , $ default = null , ?string $ description = null ): self
67
+ {
68
+ return new self ($ name , false , $ default , $ description );
69
+ }
50
70
}
0 commit comments