TYPO3  7.6
InputOption.php
Go to the documentation of this file.
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 
22 {
23  const VALUE_NONE = 1;
24  const VALUE_REQUIRED = 2;
25  const VALUE_OPTIONAL = 4;
26  const VALUE_IS_ARRAY = 8;
27 
28  private $name;
29  private $shortcut;
30  private $mode;
31  private $default;
32  private $description;
33 
47  public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
48  {
49  if (0 === strpos($name, '--')) {
50  $name = substr($name, 2);
51  }
52 
53  if (empty($name)) {
54  throw new \InvalidArgumentException('An option name cannot be empty.');
55  }
56 
57  if (empty($shortcut)) {
58  $shortcut = null;
59  }
60 
61  if (null !== $shortcut) {
62  if (is_array($shortcut)) {
63  $shortcut = implode('|', $shortcut);
64  }
65  $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
66  $shortcuts = array_filter($shortcuts);
67  $shortcut = implode('|', $shortcuts);
68 
69  if (empty($shortcut)) {
70  throw new \InvalidArgumentException('An option shortcut cannot be empty.');
71  }
72  }
73 
74  if (null === $mode) {
75  $mode = self::VALUE_NONE;
76  } elseif (!is_int($mode) || $mode > 15 || $mode < 1) {
77  throw new \InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
78  }
79 
80  $this->name = $name;
81  $this->shortcut = $shortcut;
82  $this->mode = $mode;
83  $this->description = $description;
84 
85  if ($this->isArray() && !$this->acceptValue()) {
86  throw new \InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
87  }
88 
89  $this->setDefault($default);
90  }
91 
97  public function getShortcut()
98  {
99  return $this->shortcut;
100  }
101 
107  public function getName()
108  {
109  return $this->name;
110  }
111 
117  public function acceptValue()
118  {
119  return $this->isValueRequired() || $this->isValueOptional();
120  }
121 
127  public function isValueRequired()
128  {
129  return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
130  }
131 
137  public function isValueOptional()
138  {
139  return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
140  }
141 
147  public function isArray()
148  {
149  return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
150  }
151 
159  public function setDefault($default = null)
160  {
161  if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
162  throw new \LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
163  }
164 
165  if ($this->isArray()) {
166  if (null === $default) {
167  $default = array();
168  } elseif (!is_array($default)) {
169  throw new \LogicException('A default value for an array option must be an array.');
170  }
171  }
172 
173  $this->default = $this->acceptValue() ? $default : false;
174  }
175 
181  public function getDefault()
182  {
183  return $this->default;
184  }
185 
191  public function getDescription()
192  {
193  return $this->description;
194  }
195 
203  public function equals(InputOption $option)
204  {
205  return $option->getName() === $this->getName()
206  && $option->getShortcut() === $this->getShortcut()
207  && $option->getDefault() === $this->getDefault()
208  && $option->isArray() === $this->isArray()
209  && $option->isValueRequired() === $this->isValueRequired()
210  && $option->isValueOptional() === $this->isValueOptional()
211  ;
212  }
213 }