[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <[email protected]> 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 <[email protected]> 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 $glue = ('-' === $param[1]) ? '=' : ' '; 112 if (\is_array($val)) { 113 foreach ($val as $v) { 114 $params[] = $param.('' != $v ? $glue.$this->escapeToken($v) : ''); 115 } 116 } else { 117 $params[] = $param.('' != $val ? $glue.$this->escapeToken($val) : ''); 118 } 119 } else { 120 $params[] = \is_array($val) ? implode(' ', array_map([$this, 'escapeToken'], $val)) : $this->escapeToken($val); 121 } 122 } 123 124 return implode(' ', $params); 125 } 126 127 /** 128 * {@inheritdoc} 129 */ 130 protected function parse() 131 { 132 foreach ($this->parameters as $key => $value) { 133 if ('--' === $key) { 134 return; 135 } 136 if (str_starts_with($key, '--')) { 137 $this->addLongOption(substr($key, 2), $value); 138 } elseif (str_starts_with($key, '-')) { 139 $this->addShortOption(substr($key, 1), $value); 140 } else { 141 $this->addArgument($key, $value); 142 } 143 } 144 } 145 146 /** 147 * Adds a short option value. 148 * 149 * @throws InvalidOptionException When option given doesn't exist 150 */ 151 private function addShortOption(string $shortcut, $value) 152 { 153 if (!$this->definition->hasShortcut($shortcut)) { 154 throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut)); 155 } 156 157 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); 158 } 159 160 /** 161 * Adds a long option value. 162 * 163 * @throws InvalidOptionException When option given doesn't exist 164 * @throws InvalidOptionException When a required value is missing 165 */ 166 private function addLongOption(string $name, $value) 167 { 168 if (!$this->definition->hasOption($name)) { 169 if (!$this->definition->hasNegation($name)) { 170 throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name)); 171 } 172 173 $optionName = $this->definition->negationToName($name); 174 $this->options[$optionName] = false; 175 176 return; 177 } 178 179 $option = $this->definition->getOption($name); 180 181 if (null === $value) { 182 if ($option->isValueRequired()) { 183 throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name)); 184 } 185 186 if (!$option->isValueOptional()) { 187 $value = true; 188 } 189 } 190 191 $this->options[$name] = $value; 192 } 193 194 /** 195 * Adds an argument value. 196 * 197 * @param string|int $name The argument name 198 * @param mixed $value The value for the argument 199 * 200 * @throws InvalidArgumentException When argument given doesn't exist 201 */ 202 private function addArgument($name, $value) 203 { 204 if (!$this->definition->hasArgument($name)) { 205 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); 206 } 207 208 $this->arguments[$name] = $value; 209 } 210 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |