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