[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
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 14 /** 15 * ArrayInput represents an input provided as an array. 16 * 17 * Usage: 18 * 19 * $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar')); 20 * 21 * @author Fabien Potencier <fabien@symfony.com> 22 */ 23 class ArrayInput extends Input 24 { 25 private $parameters; 26 27 /** 28 * Constructor. 29 * 30 * @param array $parameters An array of parameters 31 * @param InputDefinition $definition A InputDefinition instance 32 */ 33 public function __construct(array $parameters, InputDefinition $definition = null) 34 { 35 $this->parameters = $parameters; 36 37 parent::__construct($definition); 38 } 39 40 /** 41 * Returns the first argument from the raw parameters (not parsed). 42 * 43 * @return string The value of the first argument or null otherwise 44 */ 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 56 /** 57 * Returns true if the raw parameters (not parsed) contain a value. 58 * 59 * This method is to be used to introspect the input parameters 60 * before they have been validated. It must be used carefully. 61 * 62 * @param string|array $values The values to look for in the raw parameters (can be an array) 63 * 64 * @return bool true if the value is contained in the raw parameters 65 */ 66 public function hasParameterOption($values) 67 { 68 $values = (array) $values; 69 70 foreach ($this->parameters as $k => $v) { 71 if (!is_int($k)) { 72 $v = $k; 73 } 74 75 if (in_array($v, $values)) { 76 return true; 77 } 78 } 79 80 return false; 81 } 82 83 /** 84 * Returns the value of a raw option (not parsed). 85 * 86 * This method is to be used to introspect the input parameters 87 * before they have been validated. It must be used carefully. 88 * 89 * @param string|array $values The value(s) to look for in the raw parameters (can be an array) 90 * @param mixed $default The default value to return if no result is found 91 * 92 * @return mixed The option value 93 */ 94 public function getParameterOption($values, $default = false) 95 { 96 $values = (array) $values; 97 98 foreach ($this->parameters as $k => $v) { 99 if (is_int($k)) { 100 if (in_array($v, $values)) { 101 return true; 102 } 103 } elseif (in_array($k, $values)) { 104 return $v; 105 } 106 } 107 108 return $default; 109 } 110 111 /** 112 * Returns a stringified representation of the args passed to the command. 113 * 114 * @return string 115 */ 116 public function __toString() 117 { 118 $params = array(); 119 foreach ($this->parameters as $param => $val) { 120 if ($param && '-' === $param[0]) { 121 $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : ''); 122 } else { 123 $params[] = $this->escapeToken($val); 124 } 125 } 126 127 return implode(' ', $params); 128 } 129 130 /** 131 * Processes command line arguments. 132 */ 133 protected function parse() 134 { 135 foreach ($this->parameters as $key => $value) { 136 if (0 === strpos($key, '--')) { 137 $this->addLongOption(substr($key, 2), $value); 138 } elseif ('-' === $key[0]) { 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 * @param string $shortcut The short option key 150 * @param mixed $value The value for the option 151 * 152 * @throws \InvalidArgumentException When option given doesn't exist 153 */ 154 private function addShortOption($shortcut, $value) 155 { 156 if (!$this->definition->hasShortcut($shortcut)) { 157 throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut)); 158 } 159 160 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); 161 } 162 163 /** 164 * Adds a long option value. 165 * 166 * @param string $name The long option key 167 * @param mixed $value The value for the option 168 * 169 * @throws \InvalidArgumentException When option given doesn't exist 170 * @throws \InvalidArgumentException When a required value is missing 171 */ 172 private function addLongOption($name, $value) 173 { 174 if (!$this->definition->hasOption($name)) { 175 throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name)); 176 } 177 178 $option = $this->definition->getOption($name); 179 180 if (null === $value) { 181 if ($option->isValueRequired()) { 182 throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name)); 183 } 184 185 $value = $option->isValueOptional() ? $option->getDefault() : true; 186 } 187 188 $this->options[$name] = $value; 189 } 190 191 /** 192 * Adds an argument value. 193 * 194 * @param string $name The argument name 195 * @param mixed $value The value for the argument 196 * 197 * @throws \InvalidArgumentException When argument given doesn't exist 198 */ 199 private function addArgument($name, $value) 200 { 201 if (!$this->definition->hasArgument($name)) { 202 throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); 203 } 204 205 $this->arguments[$name] = $value; 206 } 207 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |