Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Console\Question;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Represents a choice question.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
20 */
|
Chris@0
|
21 class ChoiceQuestion extends Question
|
Chris@0
|
22 {
|
Chris@0
|
23 private $choices;
|
Chris@0
|
24 private $multiselect = false;
|
Chris@0
|
25 private $prompt = ' > ';
|
Chris@0
|
26 private $errorMessage = 'Value "%s" is invalid';
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * @param string $question The question to ask to the user
|
Chris@0
|
30 * @param array $choices The list of available choices
|
Chris@0
|
31 * @param mixed $default The default answer to return
|
Chris@0
|
32 */
|
Chris@0
|
33 public function __construct($question, array $choices, $default = null)
|
Chris@0
|
34 {
|
Chris@12
|
35 if (!$choices) {
|
Chris@12
|
36 throw new \LogicException('Choice question must have at least 1 choice available.');
|
Chris@12
|
37 }
|
Chris@12
|
38
|
Chris@0
|
39 parent::__construct($question, $default);
|
Chris@0
|
40
|
Chris@0
|
41 $this->choices = $choices;
|
Chris@0
|
42 $this->setValidator($this->getDefaultValidator());
|
Chris@0
|
43 $this->setAutocompleterValues($choices);
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Returns available choices.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @return array
|
Chris@0
|
50 */
|
Chris@0
|
51 public function getChoices()
|
Chris@0
|
52 {
|
Chris@0
|
53 return $this->choices;
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * Sets multiselect option.
|
Chris@0
|
58 *
|
Chris@0
|
59 * When multiselect is set to true, multiple choices can be answered.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @param bool $multiselect
|
Chris@0
|
62 *
|
Chris@0
|
63 * @return $this
|
Chris@0
|
64 */
|
Chris@0
|
65 public function setMultiselect($multiselect)
|
Chris@0
|
66 {
|
Chris@0
|
67 $this->multiselect = $multiselect;
|
Chris@0
|
68 $this->setValidator($this->getDefaultValidator());
|
Chris@0
|
69
|
Chris@0
|
70 return $this;
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * Returns whether the choices are multiselect.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @return bool
|
Chris@0
|
77 */
|
Chris@0
|
78 public function isMultiselect()
|
Chris@0
|
79 {
|
Chris@0
|
80 return $this->multiselect;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * Gets the prompt for choices.
|
Chris@0
|
85 *
|
Chris@0
|
86 * @return string
|
Chris@0
|
87 */
|
Chris@0
|
88 public function getPrompt()
|
Chris@0
|
89 {
|
Chris@0
|
90 return $this->prompt;
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * Sets the prompt for choices.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @param string $prompt
|
Chris@0
|
97 *
|
Chris@0
|
98 * @return $this
|
Chris@0
|
99 */
|
Chris@0
|
100 public function setPrompt($prompt)
|
Chris@0
|
101 {
|
Chris@0
|
102 $this->prompt = $prompt;
|
Chris@0
|
103
|
Chris@0
|
104 return $this;
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 /**
|
Chris@0
|
108 * Sets the error message for invalid values.
|
Chris@0
|
109 *
|
Chris@0
|
110 * The error message has a string placeholder (%s) for the invalid value.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @param string $errorMessage
|
Chris@0
|
113 *
|
Chris@0
|
114 * @return $this
|
Chris@0
|
115 */
|
Chris@0
|
116 public function setErrorMessage($errorMessage)
|
Chris@0
|
117 {
|
Chris@0
|
118 $this->errorMessage = $errorMessage;
|
Chris@0
|
119 $this->setValidator($this->getDefaultValidator());
|
Chris@0
|
120
|
Chris@0
|
121 return $this;
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 /**
|
Chris@0
|
125 * Returns the default answer validator.
|
Chris@0
|
126 *
|
Chris@0
|
127 * @return callable
|
Chris@0
|
128 */
|
Chris@0
|
129 private function getDefaultValidator()
|
Chris@0
|
130 {
|
Chris@0
|
131 $choices = $this->choices;
|
Chris@0
|
132 $errorMessage = $this->errorMessage;
|
Chris@0
|
133 $multiselect = $this->multiselect;
|
Chris@0
|
134 $isAssoc = $this->isAssoc($choices);
|
Chris@0
|
135
|
Chris@0
|
136 return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
|
Chris@0
|
137 // Collapse all spaces.
|
Chris@0
|
138 $selectedChoices = str_replace(' ', '', $selected);
|
Chris@0
|
139
|
Chris@0
|
140 if ($multiselect) {
|
Chris@0
|
141 // Check for a separated comma values
|
Chris@12
|
142 if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selectedChoices, $matches)) {
|
Chris@0
|
143 throw new InvalidArgumentException(sprintf($errorMessage, $selected));
|
Chris@0
|
144 }
|
Chris@0
|
145 $selectedChoices = explode(',', $selectedChoices);
|
Chris@0
|
146 } else {
|
Chris@17
|
147 $selectedChoices = [$selected];
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@17
|
150 $multiselectChoices = [];
|
Chris@0
|
151 foreach ($selectedChoices as $value) {
|
Chris@17
|
152 $results = [];
|
Chris@0
|
153 foreach ($choices as $key => $choice) {
|
Chris@0
|
154 if ($choice === $value) {
|
Chris@0
|
155 $results[] = $key;
|
Chris@0
|
156 }
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@17
|
159 if (\count($results) > 1) {
|
Chris@0
|
160 throw new InvalidArgumentException(sprintf('The provided answer is ambiguous. Value should be one of %s.', implode(' or ', $results)));
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 $result = array_search($value, $choices);
|
Chris@0
|
164
|
Chris@0
|
165 if (!$isAssoc) {
|
Chris@0
|
166 if (false !== $result) {
|
Chris@0
|
167 $result = $choices[$result];
|
Chris@0
|
168 } elseif (isset($choices[$value])) {
|
Chris@0
|
169 $result = $choices[$value];
|
Chris@0
|
170 }
|
Chris@0
|
171 } elseif (false === $result && isset($choices[$value])) {
|
Chris@0
|
172 $result = $value;
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 if (false === $result) {
|
Chris@0
|
176 throw new InvalidArgumentException(sprintf($errorMessage, $value));
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 $multiselectChoices[] = (string) $result;
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 if ($multiselect) {
|
Chris@0
|
183 return $multiselectChoices;
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 return current($multiselectChoices);
|
Chris@0
|
187 };
|
Chris@0
|
188 }
|
Chris@0
|
189 }
|