comparison vendor/symfony/console/Question/ChoiceQuestion.php @ 0:4c8ae668cc8c

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