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;
36 * @param string $question The question to ask to the user
37 * @param mixed $default The default answer to return if the user enters nothing
39 public function __construct($question, $default = null)
41 $this->question = $question;
42 $this->default = $default;
46 * Returns the question.
50 public function getQuestion()
52 return $this->question;
56 * Returns the default answer.
60 public function getDefault()
62 return $this->default;
66 * Returns whether the user response must be hidden.
70 public function isHidden()
76 * Sets whether the user response must be hidden or not.
82 * @throws LogicException In case the autocompleter is also used
84 public function setHidden($hidden)
86 if ($this->autocompleterValues) {
87 throw new LogicException('A hidden question cannot use the autocompleter.');
90 $this->hidden = (bool) $hidden;
96 * In case the response can not be hidden, whether to fallback on non-hidden question or not.
100 public function isHiddenFallback()
102 return $this->hiddenFallback;
106 * Sets whether to fallback on non-hidden question if the response can not be hidden.
108 * @param bool $fallback
112 public function setHiddenFallback($fallback)
114 $this->hiddenFallback = (bool) $fallback;
120 * Gets values for the autocompleter.
122 * @return null|array|\Traversable
124 public function getAutocompleterValues()
126 return $this->autocompleterValues;
130 * Sets values for the autocompleter.
132 * @param null|array|\Traversable $values
136 * @throws InvalidArgumentException
137 * @throws LogicException
139 public function setAutocompleterValues($values)
141 if (is_array($values)) {
142 $values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values);
145 if (null !== $values && !is_array($values)) {
146 if (!$values instanceof \Traversable || !$values instanceof \Countable) {
147 throw new InvalidArgumentException('Autocompleter values can be either an array, `null` or an object implementing both `Countable` and `Traversable` interfaces.');
152 throw new LogicException('A hidden question cannot use the autocompleter.');
155 $this->autocompleterValues = $values;
161 * Sets a validator for the question.
163 * @param null|callable $validator
167 public function setValidator($validator)
169 $this->validator = $validator;
175 * Gets the validator for the question.
177 * @return null|callable
179 public function getValidator()
181 return $this->validator;
185 * Sets the maximum number of attempts.
187 * Null means an unlimited number of attempts.
189 * @param null|int $attempts
193 * @throws InvalidArgumentException In case the number of attempts is invalid.
195 public function setMaxAttempts($attempts)
197 if (null !== $attempts && $attempts < 1) {
198 throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
201 $this->attempts = $attempts;
207 * Gets the maximum number of attempts.
209 * Null means an unlimited number of attempts.
213 public function getMaxAttempts()
215 return $this->attempts;
219 * Sets a normalizer for the response.
221 * The normalizer can be a callable (a string), a closure or a class implementing __invoke.
223 * @param callable $normalizer
227 public function setNormalizer($normalizer)
229 $this->normalizer = $normalizer;
235 * Gets the normalizer for the response.
237 * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
241 public function getNormalizer()
243 return $this->normalizer;
246 protected function isAssoc($array)
248 return (bool) count(array_filter(array_keys($array), 'is_string'));