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