PluginProbe
Depicter — Popup & Slider Builder / 1.3.3
Depicter — Popup & Slider Builder v1.3.3
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / symfony / console / Input / ArrayInput.php

ArrayInput.php in Depicter — Popup & Slider Builder 1.3.3, at vendor/symfony/console/Input/ArrayInput.php

203 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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(['command' => 'foo:bar', 'foo' => 'bar', '--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 $param => $value) {
43 if ($param && \is_string($param) && '-' === $param[0]) {
44 continue;
45 }
46
47 return $value;
48 }
49
50 return null;
51 }
52
53 /**
54 * {@inheritdoc}
55 */
56 public function hasParameterOption($values, bool $onlyParams = false)
57 {
58 $values = (array) $values;
59
60 foreach ($this->parameters as $k => $v) {
61 if (!\is_int($k)) {
62 $v = $k;
63 }
64
65 if ($onlyParams && '--' === $v) {
66 return false;
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, bool $onlyParams = false)
81 {
82 $values = (array) $values;
83
84 foreach ($this->parameters as $k => $v) {
85 if ($onlyParams && ('--' === $k || (\is_int($k) && '--' === $v))) {
86 return $default;
87 }
88
89 if (\is_int($k)) {
90 if (\in_array($v, $values)) {
91 return true;
92 }
93 } elseif (\in_array($k, $values)) {
94 return $v;
95 }
96 }
97
98 return $default;
99 }
100
101 /**
102 * Returns a stringified representation of the args passed to the command.
103 *
104 * @return string
105 */
106 public function __toString()
107 {
108 $params = [];
109 foreach ($this->parameters as $param => $val) {
110 if ($param && \is_string($param) && '-' === $param[0]) {
111 if (\is_array($val)) {
112 foreach ($val as $v) {
113 $params[] = $param.('' != $v ? '='.$this->escapeToken($v) : '');
114 }
115 } else {
116 $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : '');
117 }
118 } else {
119 $params[] = \is_array($val) ? implode(' ', array_map([$this, 'escapeToken'], $val)) : $this->escapeToken($val);
120 }
121 }
122
123 return implode(' ', $params);
124 }
125
126 /**
127 * {@inheritdoc}
128 */
129 protected function parse()
130 {
131 foreach ($this->parameters as $key => $value) {
132 if ('--' === $key) {
133 return;
134 }
135 if (0 === strpos($key, '--')) {
136 $this->addLongOption(substr($key, 2), $value);
137 } elseif (0 === strpos($key, '-')) {
138 $this->addShortOption(substr($key, 1), $value);
139 } else {
140 $this->addArgument($key, $value);
141 }
142 }
143 }
144
145 /**
146 * Adds a short option value.
147 *
148 * @throws InvalidOptionException When option given doesn't exist
149 */
150 private function addShortOption(string $shortcut, $value)
151 {
152 if (!$this->definition->hasShortcut($shortcut)) {
153 throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
154 }
155
156 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
157 }
158
159 /**
160 * Adds a long option value.
161 *
162 * @throws InvalidOptionException When option given doesn't exist
163 * @throws InvalidOptionException When a required value is missing
164 */
165 private function addLongOption(string $name, $value)
166 {
167 if (!$this->definition->hasOption($name)) {
168 throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name));
169 }
170
171 $option = $this->definition->getOption($name);
172
173 if (null === $value) {
174 if ($option->isValueRequired()) {
175 throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
176 }
177
178 if (!$option->isValueOptional()) {
179 $value = true;
180 }
181 }
182
183 $this->options[$name] = $value;
184 }
185
186 /**
187 * Adds an argument value.
188 *
189 * @param string|int $name The argument name
190 * @param mixed $value The value for the argument
191 *
192 * @throws InvalidArgumentException When argument given doesn't exist
193 */
194 private function addArgument($name, $value)
195 {
196 if (!$this->definition->hasArgument($name)) {
197 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
198 }
199
200 $this->arguments[$name] = $value;
201 }
202 }
203