Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / symfony / console / Input / InputDefinition.php
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(array(
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 = array())
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 = array();
52         $options = array();
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($arguments = array())
71     {
72         $this->arguments = array();
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($arguments = array())
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 = array();
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($options = array())
208     {
209         $this->options = array();
210         $this->shortcuts = array();
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($options = array())
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      * @param string $name The InputOption name
255      *
256      * @return InputOption A InputOption object
257      *
258      * @throws InvalidArgumentException When option given doesn't exist
259      */
260     public function getOption($name)
261     {
262         if (!$this->hasOption($name)) {
263             throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
264         }
265
266         return $this->options[$name];
267     }
268
269     /**
270      * Returns true if an InputOption object exists by name.
271      *
272      * This method can't be used to check if the user included the option when
273      * executing the command (use getOption() instead).
274      *
275      * @param string $name The InputOption name
276      *
277      * @return bool true if the InputOption object exists, false otherwise
278      */
279     public function hasOption($name)
280     {
281         return isset($this->options[$name]);
282     }
283
284     /**
285      * Gets the array of InputOption objects.
286      *
287      * @return InputOption[] An array of InputOption objects
288      */
289     public function getOptions()
290     {
291         return $this->options;
292     }
293
294     /**
295      * Returns true if an InputOption object exists by shortcut.
296      *
297      * @param string $name The InputOption shortcut
298      *
299      * @return bool true if the InputOption object exists, false otherwise
300      */
301     public function hasShortcut($name)
302     {
303         return isset($this->shortcuts[$name]);
304     }
305
306     /**
307      * Gets an InputOption by shortcut.
308      *
309      * @param string $shortcut The Shortcut name
310      *
311      * @return InputOption An InputOption object
312      */
313     public function getOptionForShortcut($shortcut)
314     {
315         return $this->getOption($this->shortcutToName($shortcut));
316     }
317
318     /**
319      * Gets an array of default values.
320      *
321      * @return array An array of all default values
322      */
323     public function getOptionDefaults()
324     {
325         $values = array();
326         foreach ($this->options as $option) {
327             $values[$option->getName()] = $option->getDefault();
328         }
329
330         return $values;
331     }
332
333     /**
334      * Returns the InputOption name given a shortcut.
335      *
336      * @param string $shortcut The shortcut
337      *
338      * @return string The InputOption name
339      *
340      * @throws InvalidArgumentException When option given does not exist
341      */
342     private function shortcutToName($shortcut)
343     {
344         if (!isset($this->shortcuts[$shortcut])) {
345             throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
346         }
347
348         return $this->shortcuts[$shortcut];
349     }
350
351     /**
352      * Gets the synopsis.
353      *
354      * @param bool $short Whether to return the short version (with options folded) or not
355      *
356      * @return string The synopsis
357      */
358     public function getSynopsis($short = false)
359     {
360         $elements = array();
361
362         if ($short && $this->getOptions()) {
363             $elements[] = '[options]';
364         } elseif (!$short) {
365             foreach ($this->getOptions() as $option) {
366                 $value = '';
367                 if ($option->acceptValue()) {
368                     $value = sprintf(
369                         ' %s%s%s',
370                         $option->isValueOptional() ? '[' : '',
371                         strtoupper($option->getName()),
372                         $option->isValueOptional() ? ']' : ''
373                     );
374                 }
375
376                 $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
377                 $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
378             }
379         }
380
381         if (\count($elements) && $this->getArguments()) {
382             $elements[] = '[--]';
383         }
384
385         foreach ($this->getArguments() as $argument) {
386             $element = '<'.$argument->getName().'>';
387             if (!$argument->isRequired()) {
388                 $element = '['.$element.']';
389             } elseif ($argument->isArray()) {
390                 $element .= ' ('.$element.')';
391             }
392
393             if ($argument->isArray()) {
394                 $element .= '...';
395             }
396
397             $elements[] = $element;
398         }
399
400         return implode(' ', $elements);
401     }
402 }