PluginProbe
Depicter — Popup & Slider Builder / 1.3.2
Depicter — Popup & Slider Builder v1.3.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / symfony / console / Input / InputOption.php

InputOption.php in Depicter — Popup & Slider Builder 1.3.2, at vendor/symfony/console/Input/InputOption.php

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