PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / symfony / console / Question / Question.php

Question.php in Depicter — Popup & Slider Builder 1.2.0, at vendor/symfony/console/Question/Question.php

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