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