Security update for Core, with self-updated composer
[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, $onlyParams = false)
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 ($onlyParams && $v === '--') {
70                 return false;
71             }
72
73             if (in_array($v, $values)) {
74                 return true;
75             }
76         }
77
78         return false;
79     }
80
81     /**
82      * {@inheritdoc}
83      */
84     public function getParameterOption($values, $default = false, $onlyParams = false)
85     {
86         $values = (array) $values;
87
88         foreach ($this->parameters as $k => $v) {
89             if ($onlyParams && ($k === '--' || (is_int($k) && $v === '--'))) {
90                 return false;
91             }
92
93             if (is_int($k)) {
94                 if (in_array($v, $values)) {
95                     return true;
96                 }
97             } elseif (in_array($k, $values)) {
98                 return $v;
99             }
100         }
101
102         return $default;
103     }
104
105     /**
106      * Returns a stringified representation of the args passed to the command.
107      *
108      * @return string
109      */
110     public function __toString()
111     {
112         $params = array();
113         foreach ($this->parameters as $param => $val) {
114             if ($param && '-' === $param[0]) {
115                 $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : '');
116             } else {
117                 $params[] = $this->escapeToken($val);
118             }
119         }
120
121         return implode(' ', $params);
122     }
123
124     /**
125      * {@inheritdoc}
126      */
127     protected function parse()
128     {
129         foreach ($this->parameters as $key => $value) {
130             if ($key === '--') {
131                 return;
132             }
133             if (0 === strpos($key, '--')) {
134                 $this->addLongOption(substr($key, 2), $value);
135             } elseif ('-' === $key[0]) {
136                 $this->addShortOption(substr($key, 1), $value);
137             } else {
138                 $this->addArgument($key, $value);
139             }
140         }
141     }
142
143     /**
144      * Adds a short option value.
145      *
146      * @param string $shortcut The short option key
147      * @param mixed  $value    The value for the option
148      *
149      * @throws InvalidOptionException When option given doesn't exist
150      */
151     private function addShortOption($shortcut, $value)
152     {
153         if (!$this->definition->hasShortcut($shortcut)) {
154             throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
155         }
156
157         $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
158     }
159
160     /**
161      * Adds a long option value.
162      *
163      * @param string $name  The long option key
164      * @param mixed  $value The value for the option
165      *
166      * @throws InvalidOptionException When option given doesn't exist
167      * @throws InvalidOptionException When a required value is missing
168      */
169     private function addLongOption($name, $value)
170     {
171         if (!$this->definition->hasOption($name)) {
172             throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name));
173         }
174
175         $option = $this->definition->getOption($name);
176
177         if (null === $value) {
178             if ($option->isValueRequired()) {
179                 throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
180             }
181
182             $value = $option->isValueOptional() ? $option->getDefault() : true;
183         }
184
185         $this->options[$name] = $value;
186     }
187
188     /**
189      * Adds an argument value.
190      *
191      * @param string $name  The argument name
192      * @param mixed  $value The value for the argument
193      *
194      * @throws InvalidArgumentException When argument given doesn't exist
195      */
196     private function addArgument($name, $value)
197     {
198         if (!$this->definition->hasArgument($name)) {
199             throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
200         }
201
202         $this->arguments[$name] = $value;
203     }
204 }