4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Question;
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
15 use Symfony\Component\Console\Exception\LogicException;
18 * Represents a Question.
20 * @author Fabien Potencier <fabien@symfony.com>
26 private $hidden = false;
27 private $hiddenFallback = true;
28 private $autocompleterValues;
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
37 public function __construct($question, $default = null)
39 $this->question = $question;
40 $this->default = $default;
44 * Returns the question.
48 public function getQuestion()
50 return $this->question;
54 * Returns the default answer.
58 public function getDefault()
60 return $this->default;
64 * Returns whether the user response must be hidden.
68 public function isHidden()
74 * Sets whether the user response must be hidden or not.
80 * @throws LogicException In case the autocompleter is also used
82 public function setHidden($hidden)
84 if ($this->autocompleterValues) {
85 throw new LogicException('A hidden question cannot use the autocompleter.');
88 $this->hidden = (bool) $hidden;
94 * In case the response can not be hidden, whether to fallback on non-hidden question or not.
98 public function isHiddenFallback()
100 return $this->hiddenFallback;
104 * Sets whether to fallback on non-hidden question if the response can not be hidden.
106 * @param bool $fallback
110 public function setHiddenFallback($fallback)
112 $this->hiddenFallback = (bool) $fallback;
118 * Gets values for the autocompleter.
120 * @return null|iterable
122 public function getAutocompleterValues()
124 return $this->autocompleterValues;
128 * Sets values for the autocompleter.
130 * @param null|iterable $values
134 * @throws InvalidArgumentException
135 * @throws LogicException
137 public function setAutocompleterValues($values)
139 if (is_array($values)) {
140 $values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values);
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.');
148 throw new LogicException('A hidden question cannot use the autocompleter.');
151 $this->autocompleterValues = $values;
157 * Sets a validator for the question.
159 * @param null|callable $validator
163 public function setValidator(callable $validator = null)
165 $this->validator = $validator;
171 * Gets the validator for the question.
173 * @return null|callable
175 public function getValidator()
177 return $this->validator;
181 * Sets the maximum number of attempts.
183 * Null means an unlimited number of attempts.
185 * @param null|int $attempts
189 * @throws InvalidArgumentException in case the number of attempts is invalid
191 public function setMaxAttempts($attempts)
193 if (null !== $attempts && $attempts < 1) {
194 throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
197 $this->attempts = $attempts;
203 * Gets the maximum number of attempts.
205 * Null means an unlimited number of attempts.
209 public function getMaxAttempts()
211 return $this->attempts;
215 * Sets a normalizer for the response.
217 * The normalizer can be a callable (a string), a closure or a class implementing __invoke.
219 * @param callable $normalizer
223 public function setNormalizer(callable $normalizer)
225 $this->normalizer = $normalizer;
231 * Gets the normalizer for the response.
233 * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
237 public function getNormalizer()
239 return $this->normalizer;
242 protected function isAssoc($array)
244 return (bool) count(array_filter(array_keys($array), 'is_string'));