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