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