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)
37 parent::__construct($question, $default);
39 $this->choices = $choices;
40 $this->setValidator($this->getDefaultValidator());
41 $this->setAutocompleterValues($choices);
45 * Returns available choices.
49 public function getChoices()
51 return $this->choices;
55 * Sets multiselect option.
57 * When multiselect is set to true, multiple choices can be answered.
59 * @param bool $multiselect
63 public function setMultiselect($multiselect)
65 $this->multiselect = $multiselect;
66 $this->setValidator($this->getDefaultValidator());
72 * Returns whether the choices are multiselect.
76 public function isMultiselect()
78 return $this->multiselect;
82 * Gets the prompt for choices.
86 public function getPrompt()
92 * Sets the prompt for choices.
94 * @param string $prompt
98 public function setPrompt($prompt)
100 $this->prompt = $prompt;
106 * Sets the error message for invalid values.
108 * The error message has a string placeholder (%s) for the invalid value.
110 * @param string $errorMessage
114 public function setErrorMessage($errorMessage)
116 $this->errorMessage = $errorMessage;
117 $this->setValidator($this->getDefaultValidator());
123 * Returns the default answer validator.
127 private function getDefaultValidator()
129 $choices = $this->choices;
130 $errorMessage = $this->errorMessage;
131 $multiselect = $this->multiselect;
132 $isAssoc = $this->isAssoc($choices);
134 return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
135 // Collapse all spaces.
136 $selectedChoices = str_replace(' ', '', $selected);
139 // Check for a separated comma values
140 if (!preg_match('/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/', $selectedChoices, $matches)) {
141 throw new InvalidArgumentException(sprintf($errorMessage, $selected));
143 $selectedChoices = explode(',', $selectedChoices);
145 $selectedChoices = array($selected);
148 $multiselectChoices = array();
149 foreach ($selectedChoices as $value) {
151 foreach ($choices as $key => $choice) {
152 if ($choice === $value) {
157 if (count($results) > 1) {
158 throw new InvalidArgumentException(sprintf('The provided answer is ambiguous. Value should be one of %s.', implode(' or ', $results)));
161 $result = array_search($value, $choices);
164 if (false !== $result) {
165 $result = $choices[$result];
166 } elseif (isset($choices[$value])) {
167 $result = $choices[$value];
169 } elseif (false === $result && isset($choices[$value])) {
173 if (false === $result) {
174 throw new InvalidArgumentException(sprintf($errorMessage, $value));
177 $multiselectChoices[] = (string) $result;
181 return $multiselectChoices;
184 return current($multiselectChoices);