MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
ArrayInput.php
Ir para a documentação deste ficheiro.
1<?php
2
4
5/*
6 * This file is part of the Symfony framework.
7 *
8 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
9 *
10 * This source file is subject to the MIT license that is bundled
11 * with this source code in the file LICENSE.
12 */
13
23class ArrayInput extends Input
24{
25 protected $parameters;
26
33 public function __construct(array $parameters, InputDefinition $definition = null)
34 {
35 $this->parameters = $parameters;
36
37 parent::__construct($definition);
38 }
39
45 public function getFirstArgument()
46 {
47 foreach ($this->parameters as $key => $value) {
48 if ($key && '-' === $key[0]) {
49 continue;
50 }
51
52 return $value;
53 }
54 }
55
66 public function hasParameterOption($values)
67 {
68 if (!is_array($values)) {
69 $values = array($values);
70 }
71
72 foreach ($this->parameters as $k => $v) {
73 if (!is_int($k)) {
74 $v = $k;
75 }
76
77 if (in_array($v, $values)) {
78 return true;
79 }
80 }
81
82 return false;
83 }
84
88 protected function parse()
89 {
90 foreach ($this->parameters as $key => $value) {
91 if ('--' === substr($key, 0, 2)) {
92 $this->addLongOption(substr($key, 2), $value);
93 } elseif ('-' === $key[0]) {
94 $this->addShortOption(substr($key, 1), $value);
95 } else {
96 $this->addArgument($key, $value);
97 }
98 }
99 }
100
109 protected function addShortOption($shortcut, $value)
110 {
111 if (!$this->definition->hasShortcut($shortcut)) {
112 throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
113 }
114
115 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
116 }
117
127 protected function addLongOption($name, $value)
128 {
129 if (!$this->definition->hasOption($name)) {
130 throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
131 }
132
133 $option = $this->definition->getOption($name);
134
135 if (null === $value) {
136 if ($option->isValueRequired()) {
137 throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name));
138 }
139
140 $value = $option->isValueOptional() ? $option->getDefault() : true;
141 }
142
143 $this->options[$name] = $value;
144 }
145
154 protected function addArgument($name, $value)
155 {
156 if (!$this->definition->hasArgument($name)) {
157 throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
158 }
159
160 $this->arguments[$name] = $value;
161 }
162}
__construct(array $parameters, InputDefinition $definition=null)
Definição ArrayInput.php:33