Question.php
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace IAWP_SCOPED\Symfony\Component\Console\Question; |
| 12 | |
| 13 | use IAWP_SCOPED\Symfony\Component\Console\Exception\InvalidArgumentException; |
| 14 | use IAWP_SCOPED\Symfony\Component\Console\Exception\LogicException; |
| 15 | /** |
| 16 | * Represents a Question. |
| 17 | * |
| 18 | * @author Fabien Potencier <fabien@symfony.com> |
| 19 | */ |
| 20 | class Question |
| 21 | { |
| 22 | private $question; |
| 23 | private $attempts; |
| 24 | private $hidden = \false; |
| 25 | private $hiddenFallback = \true; |
| 26 | private $autocompleterCallback; |
| 27 | private $validator; |
| 28 | private $default; |
| 29 | private $normalizer; |
| 30 | private $trimmable = \true; |
| 31 | private $multiline = \false; |
| 32 | /** |
| 33 | * @param string $question The question to ask to the user |
| 34 | * @param string|bool|int|float|null $default The default answer to return if the user enters nothing |
| 35 | */ |
| 36 | public function __construct(string $question, $default = null) |
| 37 | { |
| 38 | $this->question = $question; |
| 39 | $this->default = $default; |
| 40 | } |
| 41 | /** |
| 42 | * Returns the question. |
| 43 | * |
| 44 | * @return string |
| 45 | */ |
| 46 | public function getQuestion() |
| 47 | { |
| 48 | return $this->question; |
| 49 | } |
| 50 | /** |
| 51 | * Returns the default answer. |
| 52 | * |
| 53 | * @return string|bool|int|float|null |
| 54 | */ |
| 55 | public function getDefault() |
| 56 | { |
| 57 | return $this->default; |
| 58 | } |
| 59 | /** |
| 60 | * Returns whether the user response accepts newline characters. |
| 61 | */ |
| 62 | public function isMultiline() : bool |
| 63 | { |
| 64 | return $this->multiline; |
| 65 | } |
| 66 | /** |
| 67 | * Sets whether the user response should accept newline characters. |
| 68 | * |
| 69 | * @return $this |
| 70 | */ |
| 71 | public function setMultiline(bool $multiline) : self |
| 72 | { |
| 73 | $this->multiline = $multiline; |
| 74 | return $this; |
| 75 | } |
| 76 | /** |
| 77 | * Returns whether the user response must be hidden. |
| 78 | * |
| 79 | * @return bool |
| 80 | */ |
| 81 | public function isHidden() |
| 82 | { |
| 83 | return $this->hidden; |
| 84 | } |
| 85 | /** |
| 86 | * Sets whether the user response must be hidden or not. |
| 87 | * |
| 88 | * @return $this |
| 89 | * |
| 90 | * @throws LogicException In case the autocompleter is also used |
| 91 | */ |
| 92 | public function setHidden(bool $hidden) |
| 93 | { |
| 94 | if ($this->autocompleterCallback) { |
| 95 | throw new LogicException('A hidden question cannot use the autocompleter.'); |
| 96 | } |
| 97 | $this->hidden = $hidden; |
| 98 | return $this; |
| 99 | } |
| 100 | /** |
| 101 | * In case the response cannot be hidden, whether to fallback on non-hidden question or not. |
| 102 | * |
| 103 | * @return bool |
| 104 | */ |
| 105 | public function isHiddenFallback() |
| 106 | { |
| 107 | return $this->hiddenFallback; |
| 108 | } |
| 109 | /** |
| 110 | * Sets whether to fallback on non-hidden question if the response cannot be hidden. |
| 111 | * |
| 112 | * @return $this |
| 113 | */ |
| 114 | public function setHiddenFallback(bool $fallback) |
| 115 | { |
| 116 | $this->hiddenFallback = $fallback; |
| 117 | return $this; |
| 118 | } |
| 119 | /** |
| 120 | * Gets values for the autocompleter. |
| 121 | * |
| 122 | * @return iterable|null |
| 123 | */ |
| 124 | public function getAutocompleterValues() |
| 125 | { |
| 126 | $callback = $this->getAutocompleterCallback(); |
| 127 | return $callback ? $callback('') : null; |
| 128 | } |
| 129 | /** |
| 130 | * Sets values for the autocompleter. |
| 131 | * |
| 132 | * @return $this |
| 133 | * |
| 134 | * @throws LogicException |
| 135 | */ |
| 136 | public function setAutocompleterValues(?iterable $values) |
| 137 | { |
| 138 | if (\is_array($values)) { |
| 139 | $values = $this->isAssoc($values) ? \array_merge(\array_keys($values), \array_values($values)) : \array_values($values); |
| 140 | $callback = static function () use($values) { |
| 141 | return $values; |
| 142 | }; |
| 143 | } elseif ($values instanceof \Traversable) { |
| 144 | $valueCache = null; |
| 145 | $callback = static function () use($values, &$valueCache) { |
| 146 | return $valueCache ?? ($valueCache = \iterator_to_array($values, \false)); |
| 147 | }; |
| 148 | } else { |
| 149 | $callback = null; |
| 150 | } |
| 151 | return $this->setAutocompleterCallback($callback); |
| 152 | } |
| 153 | /** |
| 154 | * Gets the callback function used for the autocompleter. |
| 155 | */ |
| 156 | public function getAutocompleterCallback() : ?callable |
| 157 | { |
| 158 | return $this->autocompleterCallback; |
| 159 | } |
| 160 | /** |
| 161 | * Sets the callback function used for the autocompleter. |
| 162 | * |
| 163 | * The callback is passed the user input as argument and should return an iterable of corresponding suggestions. |
| 164 | * |
| 165 | * @return $this |
| 166 | */ |
| 167 | public function setAutocompleterCallback(callable $callback = null) : self |
| 168 | { |
| 169 | if ($this->hidden && null !== $callback) { |
| 170 | throw new LogicException('A hidden question cannot use the autocompleter.'); |
| 171 | } |
| 172 | $this->autocompleterCallback = $callback; |
| 173 | return $this; |
| 174 | } |
| 175 | /** |
| 176 | * Sets a validator for the question. |
| 177 | * |
| 178 | * @return $this |
| 179 | */ |
| 180 | public function setValidator(callable $validator = null) |
| 181 | { |
| 182 | $this->validator = $validator; |
| 183 | return $this; |
| 184 | } |
| 185 | /** |
| 186 | * Gets the validator for the question. |
| 187 | * |
| 188 | * @return callable|null |
| 189 | */ |
| 190 | public function getValidator() |
| 191 | { |
| 192 | return $this->validator; |
| 193 | } |
| 194 | /** |
| 195 | * Sets the maximum number of attempts. |
| 196 | * |
| 197 | * Null means an unlimited number of attempts. |
| 198 | * |
| 199 | * @return $this |
| 200 | * |
| 201 | * @throws InvalidArgumentException in case the number of attempts is invalid |
| 202 | */ |
| 203 | public function setMaxAttempts(?int $attempts) |
| 204 | { |
| 205 | if (null !== $attempts && $attempts < 1) { |
| 206 | throw new InvalidArgumentException('Maximum number of attempts must be a positive value.'); |
| 207 | } |
| 208 | $this->attempts = $attempts; |
| 209 | return $this; |
| 210 | } |
| 211 | /** |
| 212 | * Gets the maximum number of attempts. |
| 213 | * |
| 214 | * Null means an unlimited number of attempts. |
| 215 | * |
| 216 | * @return int|null |
| 217 | */ |
| 218 | public function getMaxAttempts() |
| 219 | { |
| 220 | return $this->attempts; |
| 221 | } |
| 222 | /** |
| 223 | * Sets a normalizer for the response. |
| 224 | * |
| 225 | * The normalizer can be a callable (a string), a closure or a class implementing __invoke. |
| 226 | * |
| 227 | * @return $this |
| 228 | */ |
| 229 | public function setNormalizer(callable $normalizer) |
| 230 | { |
| 231 | $this->normalizer = $normalizer; |
| 232 | return $this; |
| 233 | } |
| 234 | /** |
| 235 | * Gets the normalizer for the response. |
| 236 | * |
| 237 | * The normalizer can ba a callable (a string), a closure or a class implementing __invoke. |
| 238 | * |
| 239 | * @return callable|null |
| 240 | */ |
| 241 | public function getNormalizer() |
| 242 | { |
| 243 | return $this->normalizer; |
| 244 | } |
| 245 | protected function isAssoc(array $array) |
| 246 | { |
| 247 | return (bool) \count(\array_filter(\array_keys($array), 'is_string')); |
| 248 | } |
| 249 | public function isTrimmable() : bool |
| 250 | { |
| 251 | return $this->trimmable; |
| 252 | } |
| 253 | /** |
| 254 | * @return $this |
| 255 | */ |
| 256 | public function setTrimmable(bool $trimmable) : self |
| 257 | { |
| 258 | $this->trimmable = $trimmable; |
| 259 | return $this; |
| 260 | } |
| 261 | } |
| 262 |