4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Input;
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
15 use Symfony\Component\Console\Exception\InvalidOptionException;
18 * ArrayInput represents an input provided as an array.
22 * $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar'));
24 * @author Fabien Potencier <fabien@symfony.com>
26 class ArrayInput extends Input
30 public function __construct(array $parameters, InputDefinition $definition = null)
32 $this->parameters = $parameters;
34 parent::__construct($definition);
40 public function getFirstArgument()
42 foreach ($this->parameters as $key => $value) {
43 if ($key && '-' === $key[0]) {
54 public function hasParameterOption($values, $onlyParams = false)
56 $values = (array) $values;
58 foreach ($this->parameters as $k => $v) {
63 if ($onlyParams && '--' === $v) {
67 if (\in_array($v, $values)) {
78 public function getParameterOption($values, $default = false, $onlyParams = false)
80 $values = (array) $values;
82 foreach ($this->parameters as $k => $v) {
83 if ($onlyParams && ('--' === $k || (\is_int($k) && '--' === $v))) {
88 if (\in_array($v, $values)) {
91 } elseif (\in_array($k, $values)) {
100 * Returns a stringified representation of the args passed to the command.
104 public function __toString()
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) : '');
114 $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : '');
117 $params[] = \is_array($val) ? implode(' ', array_map(array($this, 'escapeToken'), $val)) : $this->escapeToken($val);
121 return implode(' ', $params);
127 protected function parse()
129 foreach ($this->parameters as $key => $value) {
133 if (0 === strpos($key, '--')) {
134 $this->addLongOption(substr($key, 2), $value);
135 } elseif ('-' === $key[0]) {
136 $this->addShortOption(substr($key, 1), $value);
138 $this->addArgument($key, $value);
144 * Adds a short option value.
146 * @param string $shortcut The short option key
147 * @param mixed $value The value for the option
149 * @throws InvalidOptionException When option given doesn't exist
151 private function addShortOption($shortcut, $value)
153 if (!$this->definition->hasShortcut($shortcut)) {
154 throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
157 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
161 * Adds a long option value.
163 * @param string $name The long option key
164 * @param mixed $value The value for the option
166 * @throws InvalidOptionException When option given doesn't exist
167 * @throws InvalidOptionException When a required value is missing
169 private function addLongOption($name, $value)
171 if (!$this->definition->hasOption($name)) {
172 throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name));
175 $option = $this->definition->getOption($name);
177 if (null === $value) {
178 if ($option->isValueRequired()) {
179 throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
182 if (!$option->isValueOptional()) {
187 $this->options[$name] = $value;
191 * Adds an argument value.
193 * @param string $name The argument name
194 * @param mixed $value The value for the argument
196 * @throws InvalidArgumentException When argument given doesn't exist
198 private function addArgument($name, $value)
200 if (!$this->definition->hasArgument($name)) {
201 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
204 $this->arguments[$name] = $value;