[ 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 * Represents a command line option. 16 * 17 * @author Fabien Potencier <fabien@symfony.com> 18 */ 19 class InputOption 20 { 21 const VALUE_NONE = 1; 22 const VALUE_REQUIRED = 2; 23 const VALUE_OPTIONAL = 4; 24 const VALUE_IS_ARRAY = 8; 25 26 private $name; 27 private $shortcut; 28 private $mode; 29 private $default; 30 private $description; 31 32 /** 33 * Constructor. 34 * 35 * @param string $name The option name 36 * @param string|array $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts 37 * @param int $mode The option mode: One of the VALUE_* constants 38 * @param string $description A description text 39 * @param mixed $default The default value (must be null for self::VALUE_NONE) 40 * 41 * @throws \InvalidArgumentException If option mode is invalid or incompatible 42 */ 43 public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null) 44 { 45 if (0 === strpos($name, '--')) { 46 $name = substr($name, 2); 47 } 48 49 if (empty($name)) { 50 throw new \InvalidArgumentException('An option name cannot be empty.'); 51 } 52 53 if (empty($shortcut)) { 54 $shortcut = null; 55 } 56 57 if (null !== $shortcut) { 58 if (is_array($shortcut)) { 59 $shortcut = implode('|', $shortcut); 60 } 61 $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-')); 62 $shortcuts = array_filter($shortcuts); 63 $shortcut = implode('|', $shortcuts); 64 65 if (empty($shortcut)) { 66 throw new \InvalidArgumentException('An option shortcut cannot be empty.'); 67 } 68 } 69 70 if (null === $mode) { 71 $mode = self::VALUE_NONE; 72 } elseif (!is_int($mode) || $mode > 15 || $mode < 1) { 73 throw new \InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode)); 74 } 75 76 $this->name = $name; 77 $this->shortcut = $shortcut; 78 $this->mode = $mode; 79 $this->description = $description; 80 81 if ($this->isArray() && !$this->acceptValue()) { 82 throw new \InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.'); 83 } 84 85 $this->setDefault($default); 86 } 87 88 /** 89 * Returns the option shortcut. 90 * 91 * @return string The shortcut 92 */ 93 public function getShortcut() 94 { 95 return $this->shortcut; 96 } 97 98 /** 99 * Returns the option name. 100 * 101 * @return string The name 102 */ 103 public function getName() 104 { 105 return $this->name; 106 } 107 108 /** 109 * Returns true if the option accepts a value. 110 * 111 * @return bool true if value mode is not self::VALUE_NONE, false otherwise 112 */ 113 public function acceptValue() 114 { 115 return $this->isValueRequired() || $this->isValueOptional(); 116 } 117 118 /** 119 * Returns true if the option requires a value. 120 * 121 * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise 122 */ 123 public function isValueRequired() 124 { 125 return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode); 126 } 127 128 /** 129 * Returns true if the option takes an optional value. 130 * 131 * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise 132 */ 133 public function isValueOptional() 134 { 135 return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode); 136 } 137 138 /** 139 * Returns true if the option can take multiple values. 140 * 141 * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise 142 */ 143 public function isArray() 144 { 145 return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode); 146 } 147 148 /** 149 * Sets the default value. 150 * 151 * @param mixed $default The default value 152 * 153 * @throws \LogicException When incorrect default value is given 154 */ 155 public function setDefault($default = null) 156 { 157 if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) { 158 throw new \LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.'); 159 } 160 161 if ($this->isArray()) { 162 if (null === $default) { 163 $default = array(); 164 } elseif (!is_array($default)) { 165 throw new \LogicException('A default value for an array option must be an array.'); 166 } 167 } 168 169 $this->default = $this->acceptValue() ? $default : false; 170 } 171 172 /** 173 * Returns the default value. 174 * 175 * @return mixed The default value 176 */ 177 public function getDefault() 178 { 179 return $this->default; 180 } 181 182 /** 183 * Returns the description text. 184 * 185 * @return string The description text 186 */ 187 public function getDescription() 188 { 189 return $this->description; 190 } 191 192 /** 193 * Checks whether the given option equals this one. 194 * 195 * @param InputOption $option option to compare 196 * 197 * @return bool 198 */ 199 public function equals(InputOption $option) 200 { 201 return $option->getName() === $this->getName() 202 && $option->getShortcut() === $this->getShortcut() 203 && $option->getDefault() === $this->getDefault() 204 && $option->isArray() === $this->isArray() 205 && $option->isValueRequired() === $this->isValueRequired() 206 && $option->isValueOptional() === $this->isValueOptional() 207 ; 208 } 209 }
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 |