PluginProbe
Depicter — Popup & Slider Builder / 1.3.2
Depicter — Popup & Slider Builder v1.3.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / symfony / console / Input / InputDefinition.php

InputDefinition.php in Depicter — Popup & Slider Builder 1.3.2, at vendor/symfony/console/Input/InputDefinition.php

391 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\LogicException;
16
17 /**
18 * A InputDefinition represents a set of valid command line arguments and options.
19 *
20 * Usage:
21 *
22 * $definition = new InputDefinition([
23 * new InputArgument('name', InputArgument::REQUIRED),
24 * new InputOption('foo', 'f', InputOption::VALUE_REQUIRED),
25 * ]);
26 *
27 * @author Fabien Potencier <fabien@symfony.com>
28 */
29 class InputDefinition
30 {
31 private $arguments;
32 private $requiredCount;
33 private $hasAnArrayArgument = false;
34 private $hasOptional;
35 private $options;
36 private $shortcuts;
37
38 /**
39 * @param array $definition An array of InputArgument and InputOption instance
40 */
41 public function __construct(array $definition = [])
42 {
43 $this->setDefinition($definition);
44 }
45
46 /**
47 * Sets the definition of the input.
48 */
49 public function setDefinition(array $definition)
50 {
51 $arguments = [];
52 $options = [];
53 foreach ($definition as $item) {
54 if ($item instanceof InputOption) {
55 $options[] = $item;
56 } else {
57 $arguments[] = $item;
58 }
59 }
60
61 $this->setArguments($arguments);
62 $this->setOptions($options);
63 }
64
65 /**
66 * Sets the InputArgument objects.
67 *
68 * @param InputArgument[] $arguments An array of InputArgument objects
69 */
70 public function setArguments(array $arguments = [])
71 {
72 $this->arguments = [];
73 $this->requiredCount = 0;
74 $this->hasOptional = false;
75 $this->hasAnArrayArgument = false;
76 $this->addArguments($arguments);
77 }
78
79 /**
80 * Adds an array of InputArgument objects.
81 *
82 * @param InputArgument[] $arguments An array of InputArgument objects
83 */
84 public function addArguments(?array $arguments = [])
85 {
86 if (null !== $arguments) {
87 foreach ($arguments as $argument) {
88 $this->addArgument($argument);
89 }
90 }
91 }
92
93 /**
94 * @throws LogicException When incorrect argument is given
95 */
96 public function addArgument(InputArgument $argument)
97 {
98 if (isset($this->arguments[$argument->getName()])) {
99 throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
100 }
101
102 if ($this->hasAnArrayArgument) {
103 throw new LogicException('Cannot add an argument after an array argument.');
104 }
105
106 if ($argument->isRequired() && $this->hasOptional) {
107 throw new LogicException('Cannot add a required argument after an optional one.');
108 }
109
110 if ($argument->isArray()) {
111 $this->hasAnArrayArgument = true;
112 }
113
114 if ($argument->isRequired()) {
115 ++$this->requiredCount;
116 } else {
117 $this->hasOptional = true;
118 }
119
120 $this->arguments[$argument->getName()] = $argument;
121 }
122
123 /**
124 * Returns an InputArgument by name or by position.
125 *
126 * @param string|int $name The InputArgument name or position
127 *
128 * @return InputArgument An InputArgument object
129 *
130 * @throws InvalidArgumentException When argument given doesn't exist
131 */
132 public function getArgument($name)
133 {
134 if (!$this->hasArgument($name)) {
135 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
136 }
137
138 $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
139
140 return $arguments[$name];
141 }
142
143 /**
144 * Returns true if an InputArgument object exists by name or position.
145 *
146 * @param string|int $name The InputArgument name or position
147 *
148 * @return bool true if the InputArgument object exists, false otherwise
149 */
150 public function hasArgument($name)
151 {
152 $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
153
154 return isset($arguments[$name]);
155 }
156
157 /**
158 * Gets the array of InputArgument objects.
159 *
160 * @return InputArgument[] An array of InputArgument objects
161 */
162 public function getArguments()
163 {
164 return $this->arguments;
165 }
166
167 /**
168 * Returns the number of InputArguments.
169 *
170 * @return int The number of InputArguments
171 */
172 public function getArgumentCount()
173 {
174 return $this->hasAnArrayArgument ? \PHP_INT_MAX : \count($this->arguments);
175 }
176
177 /**
178 * Returns the number of required InputArguments.
179 *
180 * @return int The number of required InputArguments
181 */
182 public function getArgumentRequiredCount()
183 {
184 return $this->requiredCount;
185 }
186
187 /**
188 * Gets the default values.
189 *
190 * @return array An array of default values
191 */
192 public function getArgumentDefaults()
193 {
194 $values = [];
195 foreach ($this->arguments as $argument) {
196 $values[$argument->getName()] = $argument->getDefault();
197 }
198
199 return $values;
200 }
201
202 /**
203 * Sets the InputOption objects.
204 *
205 * @param InputOption[] $options An array of InputOption objects
206 */
207 public function setOptions(array $options = [])
208 {
209 $this->options = [];
210 $this->shortcuts = [];
211 $this->addOptions($options);
212 }
213
214 /**
215 * Adds an array of InputOption objects.
216 *
217 * @param InputOption[] $options An array of InputOption objects
218 */
219 public function addOptions(array $options = [])
220 {
221 foreach ($options as $option) {
222 $this->addOption($option);
223 }
224 }
225
226 /**
227 * @throws LogicException When option given already exist
228 */
229 public function addOption(InputOption $option)
230 {
231 if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
232 throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
233 }
234
235 if ($option->getShortcut()) {
236 foreach (explode('|', $option->getShortcut()) as $shortcut) {
237 if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
238 throw new LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
239 }
240 }
241 }
242
243 $this->options[$option->getName()] = $option;
244 if ($option->getShortcut()) {
245 foreach (explode('|', $option->getShortcut()) as $shortcut) {
246 $this->shortcuts[$shortcut] = $option->getName();
247 }
248 }
249 }
250
251 /**
252 * Returns an InputOption by name.
253 *
254 * @return InputOption A InputOption object
255 *
256 * @throws InvalidArgumentException When option given doesn't exist
257 */
258 public function getOption(string $name)
259 {
260 if (!$this->hasOption($name)) {
261 throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
262 }
263
264 return $this->options[$name];
265 }
266
267 /**
268 * Returns true if an InputOption object exists by name.
269 *
270 * This method can't be used to check if the user included the option when
271 * executing the command (use getOption() instead).
272 *
273 * @return bool true if the InputOption object exists, false otherwise
274 */
275 public function hasOption(string $name)
276 {
277 return isset($this->options[$name]);
278 }
279
280 /**
281 * Gets the array of InputOption objects.
282 *
283 * @return InputOption[] An array of InputOption objects
284 */
285 public function getOptions()
286 {
287 return $this->options;
288 }
289
290 /**
291 * Returns true if an InputOption object exists by shortcut.
292 *
293 * @return bool true if the InputOption object exists, false otherwise
294 */
295 public function hasShortcut(string $name)
296 {
297 return isset($this->shortcuts[$name]);
298 }
299
300 /**
301 * Gets an InputOption by shortcut.
302 *
303 * @return InputOption An InputOption object
304 */
305 public function getOptionForShortcut(string $shortcut)
306 {
307 return $this->getOption($this->shortcutToName($shortcut));
308 }
309
310 /**
311 * Gets an array of default values.
312 *
313 * @return array An array of all default values
314 */
315 public function getOptionDefaults()
316 {
317 $values = [];
318 foreach ($this->options as $option) {
319 $values[$option->getName()] = $option->getDefault();
320 }
321
322 return $values;
323 }
324
325 /**
326 * Returns the InputOption name given a shortcut.
327 *
328 * @throws InvalidArgumentException When option given does not exist
329 *
330 * @internal
331 */
332 public function shortcutToName(string $shortcut): string
333 {
334 if (!isset($this->shortcuts[$shortcut])) {
335 throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
336 }
337
338 return $this->shortcuts[$shortcut];
339 }
340
341 /**
342 * Gets the synopsis.
343 *
344 * @return string The synopsis
345 */
346 public function getSynopsis(bool $short = false)
347 {
348 $elements = [];
349
350 if ($short && $this->getOptions()) {
351 $elements[] = '[options]';
352 } elseif (!$short) {
353 foreach ($this->getOptions() as $option) {
354 $value = '';
355 if ($option->acceptValue()) {
356 $value = sprintf(
357 ' %s%s%s',
358 $option->isValueOptional() ? '[' : '',
359 strtoupper($option->getName()),
360 $option->isValueOptional() ? ']' : ''
361 );
362 }
363
364 $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
365 $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
366 }
367 }
368
369 if (\count($elements) && $this->getArguments()) {
370 $elements[] = '[--]';
371 }
372
373 $tail = '';
374 foreach ($this->getArguments() as $argument) {
375 $element = '<'.$argument->getName().'>';
376 if ($argument->isArray()) {
377 $element .= '...';
378 }
379
380 if (!$argument->isRequired()) {
381 $element = '['.$element;
382 $tail .= ']';
383 }
384
385 $elements[] = $element;
386 }
387
388 return implode(' ', $elements).$tail;
389 }
390 }
391