Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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\Input;
13
 
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Completion\CompletionInput;
16
use Symfony\Component\Console\Completion\CompletionSuggestions;
17
use Symfony\Component\Console\Completion\Suggestion;
18
use Symfony\Component\Console\Exception\InvalidArgumentException;
19
use Symfony\Component\Console\Exception\LogicException;
20
 
21
/**
22
 * Represents a command line argument.
23
 *
24
 * @author Fabien Potencier <fabien@symfony.com>
25
 */
26
class InputArgument
27
{
28
    public const REQUIRED = 1;
29
    public const OPTIONAL = 2;
30
    public const IS_ARRAY = 4;
31
 
32
    private string $name;
33
    private int $mode;
34
    private string|int|bool|array|null|float $default;
35
    private array|\Closure $suggestedValues;
36
    private string $description;
37
 
38
    /**
688 lars 39
     * @param string                                                                        $name            The argument name
40
     * @param int|null                                                                      $mode            The argument mode: self::REQUIRED or self::OPTIONAL
41
     * @param string                                                                        $description     A description text
42
     * @param string|bool|int|float|array|null                                              $default         The default value (for self::OPTIONAL mode only)
148 lars 43
     * @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
44
     *
45
     * @throws InvalidArgumentException When argument mode is not valid
46
     */
47
    public function __construct(string $name, int $mode = null, string $description = '', string|bool|int|float|array $default = null, \Closure|array $suggestedValues = [])
48
    {
49
        if (null === $mode) {
50
            $mode = self::OPTIONAL;
51
        } elseif ($mode > 7 || $mode < 1) {
52
            throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
53
        }
54
 
55
        $this->name = $name;
56
        $this->mode = $mode;
57
        $this->description = $description;
58
        $this->suggestedValues = $suggestedValues;
59
 
60
        $this->setDefault($default);
61
    }
62
 
63
    /**
64
     * Returns the argument name.
65
     */
66
    public function getName(): string
67
    {
68
        return $this->name;
69
    }
70
 
71
    /**
72
     * Returns true if the argument is required.
73
     *
74
     * @return bool true if parameter mode is self::REQUIRED, false otherwise
75
     */
76
    public function isRequired(): bool
77
    {
78
        return self::REQUIRED === (self::REQUIRED & $this->mode);
79
    }
80
 
81
    /**
82
     * Returns true if the argument can take multiple values.
83
     *
84
     * @return bool true if mode is self::IS_ARRAY, false otherwise
85
     */
86
    public function isArray(): bool
87
    {
88
        return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
89
    }
90
 
91
    /**
92
     * Sets the default value.
93
     *
94
     * @throws LogicException When incorrect default value is given
95
     */
96
    public function setDefault(string|bool|int|float|array $default = null)
97
    {
98
        if (1 > \func_num_args()) {
99
            trigger_deprecation('symfony/console', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
100
        }
101
        if ($this->isRequired() && null !== $default) {
102
            throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
103
        }
104
 
105
        if ($this->isArray()) {
106
            if (null === $default) {
107
                $default = [];
108
            } elseif (!\is_array($default)) {
109
                throw new LogicException('A default value for an array argument must be an array.');
110
            }
111
        }
112
 
113
        $this->default = $default;
114
    }
115
 
116
    /**
117
     * Returns the default value.
118
     */
119
    public function getDefault(): string|bool|int|float|array|null
120
    {
121
        return $this->default;
122
    }
123
 
124
    public function hasCompletion(): bool
125
    {
126
        return [] !== $this->suggestedValues;
127
    }
128
 
129
    /**
130
     * Adds suggestions to $suggestions for the current completion input.
131
     *
132
     * @see Command::complete()
133
     */
134
    public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
135
    {
136
        $values = $this->suggestedValues;
137
        if ($values instanceof \Closure && !\is_array($values = $values($input))) {
138
            throw new LogicException(sprintf('Closure for argument "%s" must return an array. Got "%s".', $this->name, get_debug_type($values)));
139
        }
140
        if ($values) {
141
            $suggestions->suggestValues($values);
142
        }
143
    }
144
 
145
    /**
146
     * Returns the description text.
147
     */
148
    public function getDescription(): string
149
    {
150
        return $this->description;
151
    }
152
}