diff options
Diffstat (limited to 'vendor/symfony/console/Input/ArrayInput.php')
-rw-r--r-- | vendor/symfony/console/Input/ArrayInput.php | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/vendor/symfony/console/Input/ArrayInput.php b/vendor/symfony/console/Input/ArrayInput.php new file mode 100644 index 0000000..d27ff41 --- /dev/null +++ b/vendor/symfony/console/Input/ArrayInput.php | |||
@@ -0,0 +1,191 @@ | |||
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\Exception\InvalidArgumentException; | ||
15 | use Symfony\Component\Console\Exception\InvalidOptionException; | ||
16 | |||
17 | /** | ||
18 | * ArrayInput represents an input provided as an array. | ||
19 | * | ||
20 | * Usage: | ||
21 | * | ||
22 | * $input = new ArrayInput(['command' => 'foo:bar', 'foo' => 'bar', '--bar' => 'foobar']); | ||
23 | * | ||
24 | * @author Fabien Potencier <fabien@symfony.com> | ||
25 | */ | ||
26 | class ArrayInput extends Input | ||
27 | { | ||
28 | public function __construct( | ||
29 | private array $parameters, | ||
30 | ?InputDefinition $definition = null, | ||
31 | ) { | ||
32 | parent::__construct($definition); | ||
33 | } | ||
34 | |||
35 | public function getFirstArgument(): ?string | ||
36 | { | ||
37 | foreach ($this->parameters as $param => $value) { | ||
38 | if ($param && \is_string($param) && '-' === $param[0]) { | ||
39 | continue; | ||
40 | } | ||
41 | |||
42 | return $value; | ||
43 | } | ||
44 | |||
45 | return null; | ||
46 | } | ||
47 | |||
48 | public function hasParameterOption(string|array $values, bool $onlyParams = false): bool | ||
49 | { | ||
50 | $values = (array) $values; | ||
51 | |||
52 | foreach ($this->parameters as $k => $v) { | ||
53 | if (!\is_int($k)) { | ||
54 | $v = $k; | ||
55 | } | ||
56 | |||
57 | if ($onlyParams && '--' === $v) { | ||
58 | return false; | ||
59 | } | ||
60 | |||
61 | if (\in_array($v, $values)) { | ||
62 | return true; | ||
63 | } | ||
64 | } | ||
65 | |||
66 | return false; | ||
67 | } | ||
68 | |||
69 | public function getParameterOption(string|array $values, string|bool|int|float|array|null $default = false, bool $onlyParams = false): mixed | ||
70 | { | ||
71 | $values = (array) $values; | ||
72 | |||
73 | foreach ($this->parameters as $k => $v) { | ||
74 | if ($onlyParams && ('--' === $k || (\is_int($k) && '--' === $v))) { | ||
75 | return $default; | ||
76 | } | ||
77 | |||
78 | if (\is_int($k)) { | ||
79 | if (\in_array($v, $values)) { | ||
80 | return true; | ||
81 | } | ||
82 | } elseif (\in_array($k, $values)) { | ||
83 | return $v; | ||
84 | } | ||
85 | } | ||
86 | |||
87 | return $default; | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Returns a stringified representation of the args passed to the command. | ||
92 | */ | ||
93 | public function __toString(): string | ||
94 | { | ||
95 | $params = []; | ||
96 | foreach ($this->parameters as $param => $val) { | ||
97 | if ($param && \is_string($param) && '-' === $param[0]) { | ||
98 | $glue = ('-' === $param[1]) ? '=' : ' '; | ||
99 | if (\is_array($val)) { | ||
100 | foreach ($val as $v) { | ||
101 | $params[] = $param.('' != $v ? $glue.$this->escapeToken($v) : ''); | ||
102 | } | ||
103 | } else { | ||
104 | $params[] = $param.('' != $val ? $glue.$this->escapeToken($val) : ''); | ||
105 | } | ||
106 | } else { | ||
107 | $params[] = \is_array($val) ? implode(' ', array_map($this->escapeToken(...), $val)) : $this->escapeToken($val); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | return implode(' ', $params); | ||
112 | } | ||
113 | |||
114 | protected function parse(): void | ||
115 | { | ||
116 | foreach ($this->parameters as $key => $value) { | ||
117 | if ('--' === $key) { | ||
118 | return; | ||
119 | } | ||
120 | if (str_starts_with($key, '--')) { | ||
121 | $this->addLongOption(substr($key, 2), $value); | ||
122 | } elseif (str_starts_with($key, '-')) { | ||
123 | $this->addShortOption(substr($key, 1), $value); | ||
124 | } else { | ||
125 | $this->addArgument($key, $value); | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | |||
130 | /** | ||
131 | * Adds a short option value. | ||
132 | * | ||
133 | * @throws InvalidOptionException When option given doesn't exist | ||
134 | */ | ||
135 | private function addShortOption(string $shortcut, mixed $value): void | ||
136 | { | ||
137 | if (!$this->definition->hasShortcut($shortcut)) { | ||
138 | throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut)); | ||
139 | } | ||
140 | |||
141 | $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); | ||
142 | } | ||
143 | |||
144 | /** | ||
145 | * Adds a long option value. | ||
146 | * | ||
147 | * @throws InvalidOptionException When option given doesn't exist | ||
148 | * @throws InvalidOptionException When a required value is missing | ||
149 | */ | ||
150 | private function addLongOption(string $name, mixed $value): void | ||
151 | { | ||
152 | if (!$this->definition->hasOption($name)) { | ||
153 | if (!$this->definition->hasNegation($name)) { | ||
154 | throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name)); | ||
155 | } | ||
156 | |||
157 | $optionName = $this->definition->negationToName($name); | ||
158 | $this->options[$optionName] = false; | ||
159 | |||
160 | return; | ||
161 | } | ||
162 | |||
163 | $option = $this->definition->getOption($name); | ||
164 | |||
165 | if (null === $value) { | ||
166 | if ($option->isValueRequired()) { | ||
167 | throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name)); | ||
168 | } | ||
169 | |||
170 | if (!$option->isValueOptional()) { | ||
171 | $value = true; | ||
172 | } | ||
173 | } | ||
174 | |||
175 | $this->options[$name] = $value; | ||
176 | } | ||
177 | |||
178 | /** | ||
179 | * Adds an argument value. | ||
180 | * | ||
181 | * @throws InvalidArgumentException When argument given doesn't exist | ||
182 | */ | ||
183 | private function addArgument(string|int $name, mixed $value): void | ||
184 | { | ||
185 | if (!$this->definition->hasArgument($name)) { | ||
186 | throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); | ||
187 | } | ||
188 | |||
189 | $this->arguments[$name] = $value; | ||
190 | } | ||
191 | } | ||