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