Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Question; Chris@0: Chris@0: use Symfony\Component\Console\Exception\InvalidArgumentException; Chris@0: use Symfony\Component\Console\Exception\LogicException; Chris@0: Chris@0: /** Chris@0: * Represents a Question. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class Question Chris@0: { Chris@0: private $question; Chris@0: private $attempts; Chris@0: private $hidden = false; Chris@0: private $hiddenFallback = true; Chris@0: private $autocompleterValues; Chris@0: private $validator; Chris@0: private $default; Chris@0: private $normalizer; Chris@0: Chris@0: /** Chris@0: * @param string $question The question to ask to the user Chris@0: * @param mixed $default The default answer to return if the user enters nothing Chris@0: */ Chris@0: public function __construct($question, $default = null) Chris@0: { Chris@0: $this->question = $question; Chris@0: $this->default = $default; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the question. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getQuestion() Chris@0: { Chris@0: return $this->question; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the default answer. Chris@0: * Chris@0: * @return mixed Chris@0: */ Chris@0: public function getDefault() Chris@0: { Chris@0: return $this->default; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns whether the user response must be hidden. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isHidden() Chris@0: { Chris@0: return $this->hidden; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets whether the user response must be hidden or not. Chris@0: * Chris@0: * @param bool $hidden Chris@0: * Chris@0: * @return $this Chris@0: * Chris@0: * @throws LogicException In case the autocompleter is also used Chris@0: */ Chris@0: public function setHidden($hidden) Chris@0: { Chris@0: if ($this->autocompleterValues) { Chris@0: throw new LogicException('A hidden question cannot use the autocompleter.'); Chris@0: } Chris@0: Chris@0: $this->hidden = (bool) $hidden; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * In case the response can not be hidden, whether to fallback on non-hidden question or not. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isHiddenFallback() Chris@0: { Chris@0: return $this->hiddenFallback; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets whether to fallback on non-hidden question if the response can not be hidden. Chris@0: * Chris@0: * @param bool $fallback Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setHiddenFallback($fallback) Chris@0: { Chris@0: $this->hiddenFallback = (bool) $fallback; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets values for the autocompleter. Chris@0: * Chris@17: * @return iterable|null Chris@0: */ Chris@0: public function getAutocompleterValues() Chris@0: { Chris@0: return $this->autocompleterValues; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets values for the autocompleter. Chris@0: * Chris@17: * @param iterable|null $values Chris@0: * Chris@0: * @return $this Chris@0: * Chris@0: * @throws InvalidArgumentException Chris@0: * @throws LogicException Chris@0: */ Chris@0: public function setAutocompleterValues($values) Chris@0: { Chris@17: if (\is_array($values)) { Chris@0: $values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values); Chris@0: } Chris@0: Chris@17: if (null !== $values && !\is_array($values) && !$values instanceof \Traversable) { Chris@14: throw new InvalidArgumentException('Autocompleter values can be either an array, `null` or a `Traversable` object.'); Chris@0: } Chris@0: Chris@0: if ($this->hidden) { Chris@0: throw new LogicException('A hidden question cannot use the autocompleter.'); Chris@0: } Chris@0: Chris@0: $this->autocompleterValues = $values; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets a validator for the question. Chris@0: * Chris@17: * @param callable|null $validator Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setValidator(callable $validator = null) Chris@0: { Chris@0: $this->validator = $validator; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the validator for the question. Chris@0: * Chris@17: * @return callable|null Chris@0: */ Chris@0: public function getValidator() Chris@0: { Chris@0: return $this->validator; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the maximum number of attempts. Chris@0: * Chris@0: * Null means an unlimited number of attempts. Chris@0: * Chris@17: * @param int|null $attempts Chris@0: * Chris@0: * @return $this Chris@0: * Chris@14: * @throws InvalidArgumentException in case the number of attempts is invalid Chris@0: */ Chris@0: public function setMaxAttempts($attempts) Chris@0: { Chris@0: if (null !== $attempts && $attempts < 1) { Chris@0: throw new InvalidArgumentException('Maximum number of attempts must be a positive value.'); Chris@0: } Chris@0: Chris@0: $this->attempts = $attempts; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the maximum number of attempts. Chris@0: * Chris@0: * Null means an unlimited number of attempts. Chris@0: * Chris@17: * @return int|null Chris@0: */ Chris@0: public function getMaxAttempts() Chris@0: { Chris@0: return $this->attempts; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets a normalizer for the response. Chris@0: * Chris@0: * The normalizer can be a callable (a string), a closure or a class implementing __invoke. Chris@0: * Chris@0: * @param callable $normalizer Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setNormalizer(callable $normalizer) Chris@0: { Chris@0: $this->normalizer = $normalizer; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the normalizer for the response. Chris@0: * Chris@0: * The normalizer can ba a callable (a string), a closure or a class implementing __invoke. Chris@0: * Chris@0: * @return callable Chris@0: */ Chris@0: public function getNormalizer() Chris@0: { Chris@0: return $this->normalizer; Chris@0: } Chris@0: Chris@0: protected function isAssoc($array) Chris@0: { Chris@17: return (bool) \count(array_filter(array_keys($array), 'is_string')); Chris@0: } Chris@0: }