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';
29 * @param string $question The question to ask to the user
30 * @param array $choices The list of available choices
31 * @param mixed $default The default answer to return
33 public function __construct($question, array $choices, $default = null)
36 throw new \LogicException('Choice question must have at least 1 choice available.');
39 parent::__construct($question, $default);
41 $this->choices = $choices;
42 $this->setValidator($this->getDefaultValidator());
43 $this->setAutocompleterValues($choices);
47 * Returns available choices.
51 public function getChoices()
53 return $this->choices;
57 * Sets multiselect option.
59 * When multiselect is set to true, multiple choices can be answered.
61 * @param bool $multiselect
65 public function setMultiselect($multiselect)
67 $this->multiselect = $multiselect;
68 $this->setValidator($this->getDefaultValidator());
74 * Returns whether the choices are multiselect.
78 public function isMultiselect()
80 return $this->multiselect;
84 * Gets the prompt for choices.
88 public function getPrompt()
94 * Sets the prompt for choices.
96 * @param string $prompt
100 public function setPrompt($prompt)
102 $this->prompt = $prompt;
108 * Sets the error message for invalid values.
110 * The error message has a string placeholder (%s) for the invalid value.
112 * @param string $errorMessage
116 public function setErrorMessage($errorMessage)
118 $this->errorMessage = $errorMessage;
119 $this->setValidator($this->getDefaultValidator());
125 * Returns the default answer validator.
129 private function getDefaultValidator()
131 $choices = $this->choices;
132 $errorMessage = $this->errorMessage;
133 $multiselect = $this->multiselect;
134 $isAssoc = $this->isAssoc($choices);
136 return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
137 // Collapse all spaces.
138 $selectedChoices = str_replace(' ', '', $selected);
141 // Check for a separated comma values
142 if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selectedChoices, $matches)) {
143 throw new InvalidArgumentException(sprintf($errorMessage, $selected));
145 $selectedChoices = explode(',', $selectedChoices);
147 $selectedChoices = array($selected);
150 $multiselectChoices = array();
151 foreach ($selectedChoices as $value) {
153 foreach ($choices as $key => $choice) {
154 if ($choice === $value) {
159 if (\count($results) > 1) {
160 throw new InvalidArgumentException(sprintf('The provided answer is ambiguous. Value should be one of %s.', implode(' or ', $results)));
163 $result = array_search($value, $choices);
166 if (false !== $result) {
167 $result = $choices[$result];
168 } elseif (isset($choices[$value])) {
169 $result = $choices[$value];
171 } elseif (false === $result && isset($choices[$value])) {
175 if (false === $result) {
176 throw new InvalidArgumentException(sprintf($errorMessage, $value));
179 $multiselectChoices[] = (string) $result;
183 return $multiselectChoices;
186 return current($multiselectChoices);