| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Question; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\Console\Exception\LogicException; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class Question |
| 23 |
{ |
| 24 |
private ?int $attempts = null; |
| 25 |
private bool $hidden = false; |
| 26 |
private bool $hiddenFallback = true; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private ?\Closure $autocompleterCallback = null; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private ?\Closure $validator = null; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private ?\Closure $normalizer = null; |
| 39 |
private bool $trimmable = true; |
| 40 |
private bool $multiline = false; |
| 41 |
private ?int $timeout = null; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function __construct( |
| 48 |
private string $question, |
| 49 |
private string|bool|int|float|null $default = null, |
| 50 |
) { |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function getQuestion(): string |
| 57 |
{ |
| 58 |
return $this->question; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function getDefault(): string|bool|int|float|null |
| 65 |
{ |
| 66 |
return $this->default; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function isMultiline(): bool |
| 73 |
{ |
| 74 |
return $this->multiline; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function setMultiline(bool $multiline): static |
| 83 |
{ |
| 84 |
$this->multiline = $multiline; |
| 85 |
|
| 86 |
return $this; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function getTimeout(): ?int |
| 93 |
{ |
| 94 |
return $this->timeout; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function setTimeout(?int $seconds): static |
| 104 |
{ |
| 105 |
$this->timeout = $seconds; |
| 106 |
|
| 107 |
return $this; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function isHidden(): bool |
| 114 |
{ |
| 115 |
return $this->hidden; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function setHidden(bool $hidden): static |
| 126 |
{ |
| 127 |
if ($this->autocompleterCallback) { |
| 128 |
throw new LogicException('A hidden question cannot use the autocompleter.'); |
| 129 |
} |
| 130 |
|
| 131 |
$this->hidden = $hidden; |
| 132 |
|
| 133 |
return $this; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function isHiddenFallback(): bool |
| 140 |
{ |
| 141 |
return $this->hiddenFallback; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function setHiddenFallback(bool $fallback): static |
| 150 |
{ |
| 151 |
$this->hiddenFallback = $fallback; |
| 152 |
|
| 153 |
return $this; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function getAutocompleterValues(): ?iterable |
| 160 |
{ |
| 161 |
$callback = $this->getAutocompleterCallback(); |
| 162 |
|
| 163 |
return $callback ? $callback('') : null; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function setAutocompleterValues(?iterable $values): static |
| 174 |
{ |
| 175 |
if (\is_array($values)) { |
| 176 |
$values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values); |
| 177 |
|
| 178 |
$callback = static fn () => $values; |
| 179 |
} elseif ($values instanceof \Traversable) { |
| 180 |
$callback = static function () use ($values) { |
| 181 |
static $valueCache; |
| 182 |
|
| 183 |
return $valueCache ??= iterator_to_array($values, false); |
| 184 |
}; |
| 185 |
} else { |
| 186 |
$callback = null; |
| 187 |
} |
| 188 |
|
| 189 |
return $this->setAutocompleterCallback($callback); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public function getAutocompleterCallback(): ?callable |
| 198 |
{ |
| 199 |
return $this->autocompleterCallback; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function setAutocompleterCallback(?callable $callback): static |
| 212 |
{ |
| 213 |
if ($this->hidden && null !== $callback) { |
| 214 |
throw new LogicException('A hidden question cannot use the autocompleter.'); |
| 215 |
} |
| 216 |
|
| 217 |
$this->autocompleterCallback = null === $callback ? null : $callback(...); |
| 218 |
|
| 219 |
return $this; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public function setValidator(?callable $validator): static |
| 230 |
{ |
| 231 |
$this->validator = null === $validator ? null : $validator(...); |
| 232 |
|
| 233 |
return $this; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public function getValidator(): ?callable |
| 242 |
{ |
| 243 |
return $this->validator; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public function setMaxAttempts(?int $attempts): static |
| 256 |
{ |
| 257 |
if (null !== $attempts && $attempts < 1) { |
| 258 |
throw new InvalidArgumentException('Maximum number of attempts must be a positive value.'); |
| 259 |
} |
| 260 |
|
| 261 |
$this->attempts = $attempts; |
| 262 |
|
| 263 |
return $this; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function getMaxAttempts(): ?int |
| 272 |
{ |
| 273 |
return $this->attempts; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public function setNormalizer(callable $normalizer): static |
| 284 |
{ |
| 285 |
$this->normalizer = $normalizer(...); |
| 286 |
|
| 287 |
return $this; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public function getNormalizer(): ?callable |
| 296 |
{ |
| 297 |
return $this->normalizer; |
| 298 |
} |
| 299 |
|
| 300 |
protected function isAssoc(array $array): bool |
| 301 |
{ |
| 302 |
return (bool) \count(array_filter(array_keys($array), 'is_string')); |
| 303 |
} |
| 304 |
|
| 305 |
public function isTrimmable(): bool |
| 306 |
{ |
| 307 |
return $this->trimmable; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public function setTrimmable(bool $trimmable): static |
| 314 |
{ |
| 315 |
$this->trimmable = $trimmable; |
| 316 |
|
| 317 |
return $this; |
| 318 |
} |
| 319 |
} |