Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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