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