-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameterAbstract.php
More file actions
245 lines (187 loc) · 6.92 KB
/
ParameterAbstract.php
File metadata and controls
245 lines (187 loc) · 6.92 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
declare(strict_types=1);
namespace MagicPush\CliToolkit\Parametizer\Config\Parameter;
use MagicPush\CliToolkit\Parametizer\Config\Config;
use MagicPush\CliToolkit\Parametizer\Exception\ConfigException;
use MagicPush\CliToolkit\Parametizer\HelpFormatter;
use Traversable;
abstract class ParameterAbstract {
protected readonly string $name;
protected string $description = '';
protected bool $isRequired = false;
protected bool $isArray = false;
protected bool $isSubcommandSwitch = false;
protected int $visibilityBitmask = Config::VISIBILITY_BITMASK_ALL;
protected mixed $default = null;
/** @var callable|null */
protected $callback;
/** @var callable|string|null callback / regexp */
protected $validator;
protected ?string $validatorCustomMessage;
/** @var callable|string[]|null */
protected $completion;
/** @var string[]|null[] */
protected array $allowedValues = [];
protected bool $areAllowedValuesHiddenFromHelp = false;
public function __construct(string $name) {
$errorMessagePrefix = "'" . HelpFormatter::createForStdErr()->paramTitle($name) . "' >>> Config error:";
if (mb_strlen($name) < 2) {
throw new ConfigException("{$errorMessagePrefix} too short param name; must contain at least 2 symbols.");
}
if (!preg_match('/^[a-z][a-z\d_\-]+$/u', $name)) {
throw new ConfigException(
"{$errorMessagePrefix} invalid characters. Must start with latin (lower);"
. ' the rest symbols may be of latin (lower), digit, underscore or hyphen.',
);
}
$this->name = $name;
}
public final function getName(): string {
return $this->name;
}
public function description(string $description): static {
$this->description = $description;
return $this;
}
public function getDescription(): string {
return $this->description;
}
public function require(bool $isRequired = true): static {
$this->isRequired = $isRequired;
return $this;
}
public function isRequired(): bool {
return $this->isRequired;
}
public function setIsArray(bool $isArray = true): static {
$this->isArray = $isArray;
return $this;
}
public function isArray(): bool {
return $this->isArray;
}
public function setIsSubcommandSwitch(bool $isSubcommandsName = true): static {
$this->isSubcommandSwitch = $isSubcommandsName;
return $this;
}
public function isSubcommandSwitch(): bool {
return $this->isSubcommandSwitch;
}
public function isVisibleIn(int $visibilityBitValue): bool {
return (bool) ($this->getVisibilityBitmask() & $visibilityBitValue);
}
public function visibilityBitmask(int $visibilityBitmask): static {
$this->visibilityBitmask = $visibilityBitmask;
return $this;
}
public function getVisibilityBitmask(): int {
return $this->visibilityBitmask;
}
public function callback(?callable $callback): static {
$this->callback = $callback;
return $this;
}
public function getCallback(): ?callable {
return $this->callback;
}
public function validator(callable|string|null $validator, ?string $validatorCustomMessage = null): static {
$this->validator = $validator;
$this->validatorCustomMessage = $validatorCustomMessage;
return $this;
}
public function getValidator(): callable|string|null {
return $this->validator;
}
public function getValidatorCustomMessage(): ?string {
return $this->validatorCustomMessage;
}
/**
* @param callable|string[]|null $completion
*/
public function completion(callable|array|null $completion): static {
$this->completion = $completion;
return $this;
}
/**
* @return callable|string[]|null
*/
public function getCompletion(): callable|array|null {
return $this->completion;
}
/**
* @param string[]|null[] $allowedValues
*/
public function allowedValues(array $allowedValues, bool $areHiddenFromHelp = false): static {
if (!empty($allowedValues)) {
$this->validator(fn($value) => array_key_exists($value, $allowedValues));
$this->completion(array_keys($allowedValues));
} elseif (!empty($this->allowedValues)) {
$this->validator(null);
$this->completion(null);
}
$this->allowedValues = $allowedValues;
$this->areAllowedValuesHiddenFromHelp = $areHiddenFromHelp;
return $this;
}
/**
* @return string[]|null[] value => null or (string) description
*/
public function getAllowedValues(): array {
return $this->allowedValues;
}
public function areAllowedValuesHiddenFromHelp(): bool {
return $this->areAllowedValuesHiddenFromHelp;
}
public function default(mixed $default): static {
$this->default = $default;
return $this;
}
public function getDefault(): mixed {
return $this->default;
}
public function validate(mixed &$value): mixed {
$validator = $this->getValidator();
if (null !== $validator) {
if (is_callable($validator)) {
return call_user_func_array($validator, [&$value]);
}
if (is_string($validator)) {
$matchResult = preg_match($validator, $value);
if (false !== $matchResult) {
return $matchResult;
}
}
throw new ConfigException(
"'" . HelpFormatter::createForStdErr()->paramTitle($this->getName())
. "' >>> Config error: invalid validator"
);
}
return true;
}
public function runCallback(): void {
if (null !== $this->getCallback()) {
if (!is_callable($this->getCallback())) {
throw new ConfigException(
"'" . HelpFormatter::createForStdErr()->paramTitle($this->getName())
. "' >>> Config error: non-callable callback",
);
}
$args = func_get_args();
call_user_func_array($this->getCallback(), $args);
}
}
/**
* @return mixed[]|Traversable Any type here.
*/
public function complete(string $enteredValue): iterable {
$completion = $this->getCompletion();
if (!is_array($completion) && is_callable($completion)) {
$completion = call_user_func($completion, $enteredValue);
}
if (is_array($completion) || ($completion instanceof Traversable)) {
return $completion;
}
return [];
}
abstract public function getTitleForHelp(): string;
}