Debian Codegraph @

vendor/symfony/console/Question/Question.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 use Symfony\Component\Console\Exception\LogicException;
16
17 /**
18 * Represents a Question.
19 *
20 * @author Fabien Potencier <[email protected]>
21 */
22 class Question
23 {
24 private ?int $attempts = null;
25 private bool $hidden = false;
26 private bool $hiddenFallback = true;
27 /**
28 * @var (\Closure(string):string[])|null
29 */
30 private ?\Closure $autocompleterCallback = null;
31 /**
32 * @var (\Closure(mixed):mixed)|null
33 */
34 private ?\Closure $validator = null;
35 /**
36 * @var (\Closure(mixed):mixed)|null
37 */
38 private ?\Closure $normalizer = null;
39 private bool $trimmable = true;
40 private bool $multiline = false;
41 private ?int $timeout = null;
42
43 /**
44 * @param string $question The question to ask to the user
45 * @param string|bool|int|float|null $default The default answer to return if the user enters nothing
46 */
47 public function __construct(
48 private string $question,
49 private string|bool|int|float|null $default = null,
50 ) {
51 }
52
53 /**
54 * Returns the question.
55 */
56 public function getQuestion(): string
57 {
58 return $this->question;
59 }
60
61 /**
62 * Returns the default answer.
63 */
64 public function getDefault(): string|bool|int|float|null
65 {
66 return $this->default;
67 }
68
69 /**
70 * Returns whether the user response accepts newline characters.
71 */
72 public function isMultiline(): bool
73 {
74 return $this->multiline;
75 }
76
77 /**
78 * Sets whether the user response should accept newline characters.
79 *
80 * @return $this
81 */
82 public function setMultiline(bool $multiline): static
83 {
84 $this->multiline = $multiline;
85
86 return $this;
87 }
88
89 /**
90 * Returns the timeout in seconds.
91 */
92 public function getTimeout(): ?int
93 {
94 return $this->timeout;
95 }
96
97 /**
98 * Sets the maximum time the user has to answer the question.
99 * If the user does not answer within this time, an exception will be thrown.
100 *
101 * @return $this
102 */
103 public function setTimeout(?int $seconds): static
104 {
105 $this->timeout = $seconds;
106
107 return $this;
108 }
109
110 /**
111 * Returns whether the user response must be hidden.
112 */
113 public function isHidden(): bool
114 {
115 return $this->hidden;
116 }
117
118 /**
119 * Sets whether the user response must be hidden or not.
120 *
121 * @return $this
122 *
123 * @throws LogicException In case the autocompleter is also used
124 */
125 public function setHidden(bool $hidden): static
126 {
127 if ($this->autocompleterCallback) {
128 throw new LogicException('A hidden question cannot use the autocompleter.');
129 }
130
131 $this->hidden = $hidden;
132
133 return $this;
134 }
135
136 /**
137 * In case the response cannot be hidden, whether to fallback on non-hidden question or not.
138 */
139 public function isHiddenFallback(): bool
140 {
141 return $this->hiddenFallback;
142 }
143
144 /**
145 * Sets whether to fallback on non-hidden question if the response cannot be hidden.
146 *
147 * @return $this
148 */
149 public function setHiddenFallback(bool $fallback): static
150 {
151 $this->hiddenFallback = $fallback;
152
153 return $this;
154 }
155
156 /**
157 * Gets values for the autocompleter.
158 */
159 public function getAutocompleterValues(): ?iterable
160 {
161 $callback = $this->getAutocompleterCallback();
162
163 return $callback ? $callback('') : null;
164 }
165
166 /**
167 * Sets values for the autocompleter.
168 *
169 * @return $this
170 *
171 * @throws LogicException
172 */
173 public function setAutocompleterValues(?iterable $values): static
174 {
175 if (\is_array($values)) {
176 $values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values);
177
178 $callback = static fn () => $values;
179 } elseif ($values instanceof \Traversable) {
180 $callback = static function () use ($values) {
181 static $valueCache;
182
183 return $valueCache ??= iterator_to_array($values, false);
184 };
185 } else {
186 $callback = null;
187 }
188
189 return $this->setAutocompleterCallback($callback);
190 }
191
192 /**
193 * Gets the callback function used for the autocompleter.
194 *
195 * @return (callable(string):string[])|null
196 */
197 public function getAutocompleterCallback(): ?callable
198 {
199 return $this->autocompleterCallback;
200 }
201
202 /**
203 * Sets the callback function used for the autocompleter.
204 *
205 * The callback is passed the user input as argument and should return an iterable of corresponding suggestions.
206 *
207 * @param (callable(string):string[])|null $callback
208 *
209 * @return $this
210 */
211 public function setAutocompleterCallback(?callable $callback): static
212 {
213 if ($this->hidden && null !== $callback) {
214 throw new LogicException('A hidden question cannot use the autocompleter.');
215 }
216
217 $this->autocompleterCallback = null === $callback ? null : $callback(...);
218
219 return $this;
220 }
221
222 /**
223 * Sets a validator for the question.
224 *
225 * @param (callable(mixed):mixed)|null $validator
226 *
227 * @return $this
228 */
229 public function setValidator(?callable $validator): static
230 {
231 $this->validator = null === $validator ? null : $validator(...);
232
233 return $this;
234 }
235
236 /**
237 * Gets the validator for the question.
238 *
239 * @return (callable(mixed):mixed)|null
240 */
241 public function getValidator(): ?callable
242 {
243 return $this->validator;
244 }
245
246 /**
247 * Sets the maximum number of attempts.
248 *
249 * Null means an unlimited number of attempts.
250 *
251 * @return $this
252 *
253 * @throws InvalidArgumentException in case the number of attempts is invalid
254 */
255 public function setMaxAttempts(?int $attempts): static
256 {
257 if (null !== $attempts && $attempts < 1) {
258 throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
259 }
260
261 $this->attempts = $attempts;
262
263 return $this;
264 }
265
266 /**
267 * Gets the maximum number of attempts.
268 *
269 * Null means an unlimited number of attempts.
270 */
271 public function getMaxAttempts(): ?int
272 {
273 return $this->attempts;
274 }
275
276 /**
277 * Sets a normalizer for the response.
278 *
279 * @param callable(mixed):mixed $normalizer
280 *
281 * @return $this
282 */
283 public function setNormalizer(callable $normalizer): static
284 {
285 $this->normalizer = $normalizer(...);
286
287 return $this;
288 }
289
290 /**
291 * Gets the normalizer for the response.
292 *
293 * @return (callable(mixed):mixed)|null
294 */
295 public function getNormalizer(): ?callable
296 {
297 return $this->normalizer;
298 }
299
300 protected function isAssoc(array $array): bool
301 {
302 return (bool) \count(array_filter(array_keys($array), 'is_string'));
303 }
304
305 public function isTrimmable(): bool
306 {
307 return $this->trimmable;
308 }
309
310 /**
311 * @return $this
312 */
313 public function setTrimmable(bool $trimmable): static
314 {
315 $this->trimmable = $trimmable;
316
317 return $this;
318 }
319 }