MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
InputOption.php
Ir para a documentação deste ficheiro.
1<?php
2
4
5/*
6 * This file is part of the Symfony framework.
7 *
8 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
9 *
10 * This source file is subject to the MIT license that is bundled
11 * with this source code in the file LICENSE.
12 */
13
20{
21 const VALUE_NONE = 1;
22 const VALUE_REQUIRED = 2;
23 const VALUE_OPTIONAL = 4;
24 const VALUE_IS_ARRAY = 8;
25
26 protected $name;
27 protected $shortcut;
28 protected $mode;
29 protected $default;
30 protected $description;
31
43 public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
44 {
45 if ('--' === substr($name, 0, 2)) {
46 $name = substr($name, 2);
47 }
48
49 if (empty($shortcut)) {
50 $shortcut = null;
51 }
52
53 if (null !== $shortcut) {
54 if ('-' === $shortcut[0]) {
55 $shortcut = substr($shortcut, 1);
56 }
57 }
58
59 if (null === $mode) {
61 } else if (!is_int($mode) || $mode > 15) {
62 throw new \InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
63 }
64
65 $this->name = $name;
66 $this->shortcut = $shortcut;
67 $this->mode = $mode;
68 $this->description = $description;
69
70 if ($this->isArray() && !$this->acceptValue()) {
71 throw new \InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
72 }
73
74 $this->setDefault($default);
75 }
76
82 public function getShortcut()
83 {
84 return $this->shortcut;
85 }
86
92 public function getName()
93 {
94 return $this->name;
95 }
96
102 public function acceptValue()
103 {
104 return $this->isValueRequired() || $this->isValueOptional();
105 }
106
112 public function isValueRequired()
113 {
114 return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
115 }
116
122 public function isValueOptional()
123 {
124 return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
125 }
126
132 public function isArray()
133 {
134 return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
135 }
136
142 public function setDefault($default = null)
143 {
144 if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
145 throw new \LogicException('Cannot set a default value when using Option::VALUE_NONE mode.');
146 }
147
148 if ($this->isArray()) {
149 if (null === $default) {
150 $default = array();
151 } elseif (!is_array($default)) {
152 throw new \LogicException('A default value for an array option must be an array.');
153 }
154 }
155
156 $this->default = $this->acceptValue() ? $default : false;
157 }
158
164 public function getDefault()
165 {
166 return $this->default;
167 }
168
174 public function getDescription()
175 {
176 return $this->description;
177 }
178}
__construct($name, $shortcut=null, $mode=null, $description='', $default=null)
Definição InputOption.php:43