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