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 use Symfony\Component\Console\Exception\LogicException;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Represents a Question.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
21 */
|
Chris@0
|
22 class Question
|
Chris@0
|
23 {
|
Chris@0
|
24 private $question;
|
Chris@0
|
25 private $attempts;
|
Chris@0
|
26 private $hidden = false;
|
Chris@0
|
27 private $hiddenFallback = true;
|
Chris@0
|
28 private $autocompleterValues;
|
Chris@0
|
29 private $validator;
|
Chris@0
|
30 private $default;
|
Chris@0
|
31 private $normalizer;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * @param string $question The question to ask to the user
|
Chris@0
|
35 * @param mixed $default The default answer to return if the user enters nothing
|
Chris@0
|
36 */
|
Chris@0
|
37 public function __construct($question, $default = null)
|
Chris@0
|
38 {
|
Chris@0
|
39 $this->question = $question;
|
Chris@0
|
40 $this->default = $default;
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Returns the question.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @return string
|
Chris@0
|
47 */
|
Chris@0
|
48 public function getQuestion()
|
Chris@0
|
49 {
|
Chris@0
|
50 return $this->question;
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Returns the default answer.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @return mixed
|
Chris@0
|
57 */
|
Chris@0
|
58 public function getDefault()
|
Chris@0
|
59 {
|
Chris@0
|
60 return $this->default;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * Returns whether the user response must be hidden.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @return bool
|
Chris@0
|
67 */
|
Chris@0
|
68 public function isHidden()
|
Chris@0
|
69 {
|
Chris@0
|
70 return $this->hidden;
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * Sets whether the user response must be hidden or not.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @param bool $hidden
|
Chris@0
|
77 *
|
Chris@0
|
78 * @return $this
|
Chris@0
|
79 *
|
Chris@0
|
80 * @throws LogicException In case the autocompleter is also used
|
Chris@0
|
81 */
|
Chris@0
|
82 public function setHidden($hidden)
|
Chris@0
|
83 {
|
Chris@0
|
84 if ($this->autocompleterValues) {
|
Chris@0
|
85 throw new LogicException('A hidden question cannot use the autocompleter.');
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 $this->hidden = (bool) $hidden;
|
Chris@0
|
89
|
Chris@0
|
90 return $this;
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * In case the response can not be hidden, whether to fallback on non-hidden question or not.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @return bool
|
Chris@0
|
97 */
|
Chris@0
|
98 public function isHiddenFallback()
|
Chris@0
|
99 {
|
Chris@0
|
100 return $this->hiddenFallback;
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Sets whether to fallback on non-hidden question if the response can not be hidden.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @param bool $fallback
|
Chris@0
|
107 *
|
Chris@0
|
108 * @return $this
|
Chris@0
|
109 */
|
Chris@0
|
110 public function setHiddenFallback($fallback)
|
Chris@0
|
111 {
|
Chris@0
|
112 $this->hiddenFallback = (bool) $fallback;
|
Chris@0
|
113
|
Chris@0
|
114 return $this;
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * Gets values for the autocompleter.
|
Chris@0
|
119 *
|
Chris@17
|
120 * @return iterable|null
|
Chris@0
|
121 */
|
Chris@0
|
122 public function getAutocompleterValues()
|
Chris@0
|
123 {
|
Chris@0
|
124 return $this->autocompleterValues;
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * Sets values for the autocompleter.
|
Chris@0
|
129 *
|
Chris@17
|
130 * @param iterable|null $values
|
Chris@0
|
131 *
|
Chris@0
|
132 * @return $this
|
Chris@0
|
133 *
|
Chris@0
|
134 * @throws InvalidArgumentException
|
Chris@0
|
135 * @throws LogicException
|
Chris@0
|
136 */
|
Chris@0
|
137 public function setAutocompleterValues($values)
|
Chris@0
|
138 {
|
Chris@17
|
139 if (\is_array($values)) {
|
Chris@0
|
140 $values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values);
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@17
|
143 if (null !== $values && !\is_array($values) && !$values instanceof \Traversable) {
|
Chris@14
|
144 throw new InvalidArgumentException('Autocompleter values can be either an array, `null` or a `Traversable` object.');
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 if ($this->hidden) {
|
Chris@0
|
148 throw new LogicException('A hidden question cannot use the autocompleter.');
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 $this->autocompleterValues = $values;
|
Chris@0
|
152
|
Chris@0
|
153 return $this;
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * Sets a validator for the question.
|
Chris@0
|
158 *
|
Chris@17
|
159 * @param callable|null $validator
|
Chris@0
|
160 *
|
Chris@0
|
161 * @return $this
|
Chris@0
|
162 */
|
Chris@0
|
163 public function setValidator(callable $validator = null)
|
Chris@0
|
164 {
|
Chris@0
|
165 $this->validator = $validator;
|
Chris@0
|
166
|
Chris@0
|
167 return $this;
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 /**
|
Chris@0
|
171 * Gets the validator for the question.
|
Chris@0
|
172 *
|
Chris@17
|
173 * @return callable|null
|
Chris@0
|
174 */
|
Chris@0
|
175 public function getValidator()
|
Chris@0
|
176 {
|
Chris@0
|
177 return $this->validator;
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 /**
|
Chris@0
|
181 * Sets the maximum number of attempts.
|
Chris@0
|
182 *
|
Chris@0
|
183 * Null means an unlimited number of attempts.
|
Chris@0
|
184 *
|
Chris@17
|
185 * @param int|null $attempts
|
Chris@0
|
186 *
|
Chris@0
|
187 * @return $this
|
Chris@0
|
188 *
|
Chris@14
|
189 * @throws InvalidArgumentException in case the number of attempts is invalid
|
Chris@0
|
190 */
|
Chris@0
|
191 public function setMaxAttempts($attempts)
|
Chris@0
|
192 {
|
Chris@0
|
193 if (null !== $attempts && $attempts < 1) {
|
Chris@0
|
194 throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 $this->attempts = $attempts;
|
Chris@0
|
198
|
Chris@0
|
199 return $this;
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 /**
|
Chris@0
|
203 * Gets the maximum number of attempts.
|
Chris@0
|
204 *
|
Chris@0
|
205 * Null means an unlimited number of attempts.
|
Chris@0
|
206 *
|
Chris@17
|
207 * @return int|null
|
Chris@0
|
208 */
|
Chris@0
|
209 public function getMaxAttempts()
|
Chris@0
|
210 {
|
Chris@0
|
211 return $this->attempts;
|
Chris@0
|
212 }
|
Chris@0
|
213
|
Chris@0
|
214 /**
|
Chris@0
|
215 * Sets a normalizer for the response.
|
Chris@0
|
216 *
|
Chris@0
|
217 * The normalizer can be a callable (a string), a closure or a class implementing __invoke.
|
Chris@0
|
218 *
|
Chris@0
|
219 * @param callable $normalizer
|
Chris@0
|
220 *
|
Chris@0
|
221 * @return $this
|
Chris@0
|
222 */
|
Chris@0
|
223 public function setNormalizer(callable $normalizer)
|
Chris@0
|
224 {
|
Chris@0
|
225 $this->normalizer = $normalizer;
|
Chris@0
|
226
|
Chris@0
|
227 return $this;
|
Chris@0
|
228 }
|
Chris@0
|
229
|
Chris@0
|
230 /**
|
Chris@0
|
231 * Gets the normalizer for the response.
|
Chris@0
|
232 *
|
Chris@0
|
233 * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
|
Chris@0
|
234 *
|
Chris@0
|
235 * @return callable
|
Chris@0
|
236 */
|
Chris@0
|
237 public function getNormalizer()
|
Chris@0
|
238 {
|
Chris@0
|
239 return $this->normalizer;
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 protected function isAssoc($array)
|
Chris@0
|
243 {
|
Chris@17
|
244 return (bool) \count(array_filter(array_keys($array), 'is_string'));
|
Chris@0
|
245 }
|
Chris@0
|
246 }
|