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;
17 * Represents a choice question.
19 * @author Fabien Potencier <fabien@symfony.com>
21 class ChoiceQuestion extends Question
24 private $multiselect = false;
25 private $prompt = ' > ';
26 private $errorMessage = 'Value "%s" is invalid';
31 * @param string $question The question to ask to the user
32 * @param array $choices The list of available choices
33 * @param mixed $default The default answer to return
35 public function __construct($question, array $choices, $default = null)
38 throw new \LogicException('Choice question must have at least 1 choice available.');
41 parent::__construct($question, $default);
43 $this->choices = $choices;
44 $this->setValidator($this->getDefaultValidator());
45 $this->setAutocompleterValues($choices);
49 * Returns available choices.
53 public function getChoices()
55 return $this->choices;
59 * Sets multiselect option.
61 * When multiselect is set to true, multiple choices can be answered.
63 * @param bool $multiselect
67 public function setMultiselect($multiselect)
69 $this->multiselect = $multiselect;
70 $this->setValidator($this->getDefaultValidator());
76 * Returns whether the choices are multiselect.
80 public function isMultiselect()
82 return $this->multiselect;
86 * Gets the prompt for choices.
90 public function getPrompt()
96 * Sets the prompt for choices.
98 * @param string $prompt
102 public function setPrompt($prompt)
104 $this->prompt = $prompt;
110 * Sets the error message for invalid values.
112 * The error message has a string placeholder (%s) for the invalid value.
114 * @param string $errorMessage
118 public function setErrorMessage($errorMessage)
120 $this->errorMessage = $errorMessage;
121 $this->setValidator($this->getDefaultValidator());
127 * Returns the default answer validator.
131 private function getDefaultValidator()
133 $choices = $this->choices;
134 $errorMessage = $this->errorMessage;
135 $multiselect = $this->multiselect;
136 $isAssoc = $this->isAssoc($choices);
138 return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
139 // Collapse all spaces.
140 $selectedChoices = str_replace(' ', '', $selected);
143 // Check for a separated comma values
144 if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selectedChoices, $matches)) {
145 throw new InvalidArgumentException(sprintf($errorMessage, $selected));
147 $selectedChoices = explode(',', $selectedChoices);
149 $selectedChoices = array($selected);
152 $multiselectChoices = array();
153 foreach ($selectedChoices as $value) {
155 foreach ($choices as $key => $choice) {
156 if ($choice === $value) {
161 if (count($results) > 1) {
162 throw new InvalidArgumentException(sprintf('The provided answer is ambiguous. Value should be one of %s.', implode(' or ', $results)));
165 $result = array_search($value, $choices);
168 if (false !== $result) {
169 $result = $choices[$result];
170 } elseif (isset($choices[$value])) {
171 $result = $choices[$value];
173 } elseif (false === $result && isset($choices[$value])) {
177 if (false === $result) {
178 throw new InvalidArgumentException(sprintf($errorMessage, $value));
181 $multiselectChoices[] = (string) $result;
185 return $multiselectChoices;
188 return current($multiselectChoices);