Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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     public function __construct(array $parameters, InputDefinition $definition = null)
31     {
32         $this->parameters = $parameters;
33
34         parent::__construct($definition);
35     }
36
37     /**
38      * {@inheritdoc}
39      */
40     public function getFirstArgument()
41     {
42         foreach ($this->parameters as $key => $value) {
43             if ($key && '-' === $key[0]) {
44                 continue;
45             }
46
47             return $value;
48         }
49     }
50
51     /**
52      * {@inheritdoc}
53      */
54     public function hasParameterOption($values, $onlyParams = false)
55     {
56         $values = (array) $values;
57
58         foreach ($this->parameters as $k => $v) {
59             if (!\is_int($k)) {
60                 $v = $k;
61             }
62
63             if ($onlyParams && '--' === $v) {
64                 return false;
65             }
66
67             if (\in_array($v, $values)) {
68                 return true;
69             }
70         }
71
72         return false;
73     }
74
75     /**
76      * {@inheritdoc}
77      */
78     public function getParameterOption($values, $default = false, $onlyParams = false)
79     {
80         $values = (array) $values;
81
82         foreach ($this->parameters as $k => $v) {
83             if ($onlyParams && ('--' === $k || (\is_int($k) && '--' === $v))) {
84                 return $default;
85             }
86
87             if (\is_int($k)) {
88                 if (\in_array($v, $values)) {
89                     return true;
90                 }
91             } elseif (\in_array($k, $values)) {
92                 return $v;
93             }
94         }
95
96         return $default;
97     }
98
99     /**
100      * Returns a stringified representation of the args passed to the command.
101      *
102      * @return string
103      */
104     public function __toString()
105     {
106         $params = array();
107         foreach ($this->parameters as $param => $val) {
108             if ($param && '-' === $param[0]) {
109                 if (\is_array($val)) {
110                     foreach ($val as $v) {
111                         $params[] = $param.('' != $v ? '='.$this->escapeToken($v) : '');
112                     }
113                 } else {
114                     $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : '');
115                 }
116             } else {
117                 $params[] = \is_array($val) ? implode(' ', array_map(array($this, 'escapeToken'), $val)) : $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             if (!$option->isValueOptional()) {
183                 $value = true;
184             }
185         }
186
187         $this->options[$name] = $value;
188     }
189
190     /**
191      * Adds an argument value.
192      *
193      * @param string $name  The argument name
194      * @param mixed  $value The value for the argument
195      *
196      * @throws InvalidArgumentException When argument given doesn't exist
197      */
198     private function addArgument($name, $value)
199     {
200         if (!$this->definition->hasArgument($name)) {
201             throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
202         }
203
204         $this->arguments[$name] = $value;
205     }
206 }