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