-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputOption.php
150 lines (116 loc) · 3.8 KB
/
InputOption.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types=1);
namespace Snicco\Component\BetterWPCLI\Synopsis;
use InvalidArgumentException;
use LogicException;
use Snicco\Component\BetterWPCLI\Check;
use function in_array;
use function sprintf;
final class InputOption implements InputDefinition
{
/**
* @var int
*/
public const OPTIONAL = 1;
/**
* @var int
*/
public const REQUIRED = 2;
/**
* @var int
*/
public const COMMA_SEPERATED = 4;
private string $name;
private string $description;
private bool $optional;
private bool $comma_seperated;
private ?string $default;
/**
* @var non-empty-list<string>|null
*/
private ?array $allowed_values;
/**
* @param non-empty-string $name
* @param non-empty-list<string>|null $allowed_values
*/
public function __construct(
string $name,
string $description = '',
int $flags = self::OPTIONAL,
string $default = null,
array $allowed_values = null
) {
/** @psalm-suppress TypeDoesNotContainType */
if ('' === $name) {
throw new InvalidArgumentException('name can not be empty');
}
$this->name = $name;
$this->description = $description;
if ($flags > 7 || $flags < 1) {
throw new InvalidArgumentException(sprintf('Flag [%s] is not valid.', $flags));
}
$optional = self::OPTIONAL === (self::OPTIONAL & $flags);
$required = self::REQUIRED === (self::REQUIRED & $flags);
if ($optional && $required) {
throw new LogicException('Input option can not be required and optional.');
}
if (! $optional && ! $required) {
throw new LogicException('Input option must be either required or optional.');
}
$this->comma_seperated = self::COMMA_SEPERATED === (self::COMMA_SEPERATED & $flags);
$this->optional = $optional;
if (null !== $allowed_values) {
if (! Check::isListOfStrings($allowed_values)) {
throw new InvalidArgumentException('allowed values must be a list of strings.');
}
if (Check::isEmpty($allowed_values)) {
throw new InvalidArgumentException('allowed values must not be empty.');
}
}
$this->allowed_values = $allowed_values;
if (null === $default) {
$this->default = $default;
return;
}
if ($required) {
throw new LogicException('A required argument can not have a default value.');
}
if (null !== $this->allowed_values && ! in_array($default, $this->allowed_values, true)) {
throw new InvalidArgumentException(
sprintf('Default value [%s] is not in list of allowed values.', $default)
);
}
$this->default = $default;
}
/**
* @interal
*
* @return array{type: string, name: string, description: string, optional: bool, repeating?: true, default?:string, options?: string[]}
*
* @psalm-internal Snicco\Component\BetterWPCLI
*/
public function toArray(): array
{
$data = [
'type' => 'assoc',
'name' => $this->name,
'description' => $this->description,
'optional' => $this->optional,
];
if ($this->comma_seperated) {
$data['description'] .= ' (supports multiple comma-seperated values)';
$data['repeating'] = true;
}
if (null !== $this->default) {
$data['default'] = $this->default;
}
if (null !== $this->allowed_values) {
$data['options'] = $this->allowed_values;
}
return $data;
}
public function name(): string
{
return $this->name;
}
}