comparison vendor/symfony/console/Input/ArrayInput.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
25 */ 25 */
26 class ArrayInput extends Input 26 class ArrayInput extends Input
27 { 27 {
28 private $parameters; 28 private $parameters;
29 29
30 /**
31 * Constructor.
32 *
33 * @param array $parameters An array of parameters
34 * @param InputDefinition|null $definition A InputDefinition instance
35 */
36 public function __construct(array $parameters, InputDefinition $definition = null) 30 public function __construct(array $parameters, InputDefinition $definition = null)
37 { 31 {
38 $this->parameters = $parameters; 32 $this->parameters = $parameters;
39 33
40 parent::__construct($definition); 34 parent::__construct($definition);
64 foreach ($this->parameters as $k => $v) { 58 foreach ($this->parameters as $k => $v) {
65 if (!is_int($k)) { 59 if (!is_int($k)) {
66 $v = $k; 60 $v = $k;
67 } 61 }
68 62
69 if ($onlyParams && $v === '--') { 63 if ($onlyParams && '--' === $v) {
70 return false; 64 return false;
71 } 65 }
72 66
73 if (in_array($v, $values)) { 67 if (in_array($v, $values)) {
74 return true; 68 return true;
84 public function getParameterOption($values, $default = false, $onlyParams = false) 78 public function getParameterOption($values, $default = false, $onlyParams = false)
85 { 79 {
86 $values = (array) $values; 80 $values = (array) $values;
87 81
88 foreach ($this->parameters as $k => $v) { 82 foreach ($this->parameters as $k => $v) {
89 if ($onlyParams && ($k === '--' || (is_int($k) && $v === '--'))) { 83 if ($onlyParams && ('--' === $k || (is_int($k) && '--' === $v))) {
90 return false; 84 return false;
91 } 85 }
92 86
93 if (is_int($k)) { 87 if (is_int($k)) {
94 if (in_array($v, $values)) { 88 if (in_array($v, $values)) {
110 public function __toString() 104 public function __toString()
111 { 105 {
112 $params = array(); 106 $params = array();
113 foreach ($this->parameters as $param => $val) { 107 foreach ($this->parameters as $param => $val) {
114 if ($param && '-' === $param[0]) { 108 if ($param && '-' === $param[0]) {
115 $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : ''); 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 { 116 } else {
117 $params[] = $this->escapeToken($val); 117 $params[] = is_array($val) ? implode(' ', array_map(array($this, 'escapeToken'), $val)) : $this->escapeToken($val);
118 } 118 }
119 } 119 }
120 120
121 return implode(' ', $params); 121 return implode(' ', $params);
122 } 122 }
125 * {@inheritdoc} 125 * {@inheritdoc}
126 */ 126 */
127 protected function parse() 127 protected function parse()
128 { 128 {
129 foreach ($this->parameters as $key => $value) { 129 foreach ($this->parameters as $key => $value) {
130 if ($key === '--') { 130 if ('--' === $key) {
131 return; 131 return;
132 } 132 }
133 if (0 === strpos($key, '--')) { 133 if (0 === strpos($key, '--')) {
134 $this->addLongOption(substr($key, 2), $value); 134 $this->addLongOption(substr($key, 2), $value);
135 } elseif ('-' === $key[0]) { 135 } elseif ('-' === $key[0]) {
177 if (null === $value) { 177 if (null === $value) {
178 if ($option->isValueRequired()) { 178 if ($option->isValueRequired()) {
179 throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name)); 179 throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
180 } 180 }
181 181
182 $value = $option->isValueOptional() ? $option->getDefault() : true; 182 if (!$option->isValueOptional()) {
183 $value = true;
184 }
183 } 185 }
184 186
185 $this->options[$name] = $value; 187 $this->options[$name] = $value;
186 } 188 }
187 189