Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
InputOption.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\Console\Input;
013
014 use Symfony\Component\Console\Exception\InvalidArgumentException;
015 use Symfony\Component\Console\Exception\LogicException;
016
017 /**
018 * Represents a command line option.
019 *
020 * @author Fabien Potencier <fabien@symfony.com>
021 */
022 class InputOption
023 {
024 const VALUE_NONE = 1;
025 const VALUE_REQUIRED = 2;
026 const VALUE_OPTIONAL = 4;
027 const VALUE_IS_ARRAY = 8;
028
029 private $name;
030 private $shortcut;
031 private $mode;
032 private $default;
033 private $description;
034
035 /**
036 * Constructor.
037 *
038 * @param string $name The option name
039 * @param string|array $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
040 * @param int $mode The option mode: One of the VALUE_* constants
041 * @param string $description A description text
042 * @param mixed $default The default value (must be null for self::VALUE_NONE)
043 *
044 * @throws InvalidArgumentException If option mode is invalid or incompatible
045 */
046 public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
047 {
048 if (0 === strpos($name, '--')) {
049 $name = substr($name, 2);
050 }
051
052 if (empty($name)) {
053 throw new InvalidArgumentException('An option name cannot be empty.');
054 }
055
056 if (empty($shortcut)) {
057 $shortcut = null;
058 }
059
060 if (null !== $shortcut) {
061 if (is_array($shortcut)) {
062 $shortcut = implode('|', $shortcut);
063 }
064 $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
065 $shortcuts = array_filter($shortcuts);
066 $shortcut = implode('|', $shortcuts);
067
068 if (empty($shortcut)) {
069 throw new InvalidArgumentException('An option shortcut cannot be empty.');
070 }
071 }
072
073 if (null === $mode) {
074 $mode = self::VALUE_NONE;
075 } elseif (!is_int($mode) || $mode > 15 || $mode < 1) {
076 throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
077 }
078
079 $this->name = $name;
080 $this->shortcut = $shortcut;
081 $this->mode = $mode;
082 $this->description = $description;
083
084 if ($this->isArray() && !$this->acceptValue()) {
085 throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
086 }
087
088 $this->setDefault($default);
089 }
090
091 /**
092 * Returns the option shortcut.
093 *
094 * @return string The shortcut
095 */
096 public function getShortcut()
097 {
098 return $this->shortcut;
099 }
100
101 /**
102 * Returns the option name.
103 *
104 * @return string The name
105 */
106 public function getName()
107 {
108 return $this->name;
109 }
110
111 /**
112 * Returns true if the option accepts a value.
113 *
114 * @return bool true if value mode is not self::VALUE_NONE, false otherwise
115 */
116 public function acceptValue()
117 {
118 return $this->isValueRequired() || $this->isValueOptional();
119 }
120
121 /**
122 * Returns true if the option requires a value.
123 *
124 * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise
125 */
126 public function isValueRequired()
127 {
128 return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
129 }
130
131 /**
132 * Returns true if the option takes an optional value.
133 *
134 * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise
135 */
136 public function isValueOptional()
137 {
138 return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
139 }
140
141 /**
142 * Returns true if the option can take multiple values.
143 *
144 * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise
145 */
146 public function isArray()
147 {
148 return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
149 }
150
151 /**
152 * Sets the default value.
153 *
154 * @param mixed $default The default value
155 *
156 * @throws LogicException When incorrect default value is given
157 */
158 public function setDefault($default = null)
159 {
160 if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
161 throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
162 }
163
164 if ($this->isArray()) {
165 if (null === $default) {
166 $default = array();
167 } elseif (!is_array($default)) {
168 throw new LogicException('A default value for an array option must be an array.');
169 }
170 }
171
172 $this->default = $this->acceptValue() ? $default : false;
173 }
174
175 /**
176 * Returns the default value.
177 *
178 * @return mixed The default value
179 */
180 public function getDefault()
181 {
182 return $this->default;
183 }
184
185 /**
186 * Returns the description text.
187 *
188 * @return string The description text
189 */
190 public function getDescription()
191 {
192 return $this->description;
193 }
194
195 /**
196 * Checks whether the given option equals this one.
197 *
198 * @param InputOption $option option to compare
199 *
200 * @return bool
201 */
202 public function equals(InputOption $option)
203 {
204 return $option->getName() === $this->getName()
205 && $option->getShortcut() === $this->getShortcut()
206 && $option->getDefault() === $this->getDefault()
207 && $option->isArray() === $this->isArray()
208 && $option->isValueRequired() === $this->isValueRequired()
209 && $option->isValueOptional() === $this->isValueOptional()
210 ;
211 }
212 }
213