TYPO3  7.6
ArrayInput.php
Go to the documentation of this file.
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 
25 class ArrayInput extends Input
26 {
27  private $parameters;
28 
37  public function __construct(array $parameters, InputDefinition $definition = null)
38  {
39  $this->parameters = $parameters;
40 
41  parent::__construct($definition);
42  }
43 
49  public function getFirstArgument()
50  {
51  foreach ($this->parameters as $key => $value) {
52  if ($key && '-' === $key[0]) {
53  continue;
54  }
55 
56  return $value;
57  }
58  }
59 
70  public function hasParameterOption($values)
71  {
72  $values = (array) $values;
73 
74  foreach ($this->parameters as $k => $v) {
75  if (!is_int($k)) {
76  $v = $k;
77  }
78 
79  if (in_array($v, $values)) {
80  return true;
81  }
82  }
83 
84  return false;
85  }
86 
98  public function getParameterOption($values, $default = false)
99  {
100  $values = (array) $values;
101 
102  foreach ($this->parameters as $k => $v) {
103  if (is_int($k)) {
104  if (in_array($v, $values)) {
105  return true;
106  }
107  } elseif (in_array($k, $values)) {
108  return $v;
109  }
110  }
111 
112  return $default;
113  }
114 
120  public function __toString()
121  {
122  $params = array();
123  foreach ($this->parameters as $param => $val) {
124  if ($param && '-' === $param[0]) {
125  $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : '');
126  } else {
127  $params[] = $this->escapeToken($val);
128  }
129  }
130 
131  return implode(' ', $params);
132  }
133 
137  protected function parse()
138  {
139  foreach ($this->parameters as $key => $value) {
140  if (0 === strpos($key, '--')) {
141  $this->addLongOption(substr($key, 2), $value);
142  } elseif ('-' === $key[0]) {
143  $this->addShortOption(substr($key, 1), $value);
144  } else {
145  $this->addArgument($key, $value);
146  }
147  }
148  }
149 
158  private function addShortOption($shortcut, $value)
159  {
160  if (!$this->definition->hasShortcut($shortcut)) {
161  throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
162  }
163 
164  $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
165  }
166 
176  private function addLongOption($name, $value)
177  {
178  if (!$this->definition->hasOption($name)) {
179  throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
180  }
181 
182  $option = $this->definition->getOption($name);
183 
184  if (null === $value) {
185  if ($option->isValueRequired()) {
186  throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name));
187  }
188 
189  $value = $option->isValueOptional() ? $option->getDefault() : true;
190  }
191 
192  $this->options[$name] = $value;
193  }
194 
203  private function addArgument($name, $value)
204  {
205  if (!$this->definition->hasArgument($name)) {
206  throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
207  }
208 
209  $this->arguments[$name] = $value;
210  }
211 }