| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfCommandOption |
|---|
| 20 |
{ |
|---|
| 21 |
const PARAMETER_NONE = 1; |
|---|
| 22 |
const PARAMETER_REQUIRED = 2; |
|---|
| 23 |
const PARAMETER_OPTIONAL = 4; |
|---|
| 24 |
|
|---|
| 25 |
const IS_ARRAY = 8; |
|---|
| 26 |
|
|---|
| 27 |
protected |
|---|
| 28 |
$name = null, |
|---|
| 29 |
$shortcut = null, |
|---|
| 30 |
$mode = null, |
|---|
| 31 |
$default = null, |
|---|
| 32 |
$help = ''; |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
* Constructor. |
|---|
| 36 |
* |
|---|
| 37 |
* @param string $name The option name |
|---|
| 38 |
* @param string $shortcut The shortcut (can be null) |
|---|
| 39 |
* @param integer $mode The option mode: self::PARAMETER_REQUIRED, self::PARAMETER_NONE or self::PARAMETER_OPTIONAL |
|---|
| 40 |
* @param string $help A help text |
|---|
| 41 |
* @param mixed $default The default value (must be null for self::PARAMETER_REQUIRED or self::PARAMETER_NONE) |
|---|
| 42 |
*/ |
|---|
| 43 |
public function __construct($name, $shortcut = null, $mode = null, $help = '', $default = null) |
|---|
| 44 |
{ |
|---|
| 45 |
if ('--' == substr($name, 0, 2)) |
|---|
| 46 |
{ |
|---|
| 47 |
$name = substr($name, 2); |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
if (empty($shortcut)) |
|---|
| 51 |
{ |
|---|
| 52 |
$shortcut = null; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
if (!is_null($shortcut)) |
|---|
| 56 |
{ |
|---|
| 57 |
if ('-' == $shortcut[0]) |
|---|
| 58 |
{ |
|---|
| 59 |
$shortcut = substr($shortcut, 1); |
|---|
| 60 |
} |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
if (is_null($mode)) |
|---|
| 64 |
{ |
|---|
| 65 |
$mode = self::PARAMETER_NONE; |
|---|
| 66 |
} |
|---|
| 67 |
else if (is_string($mode) || $mode > 15) |
|---|
| 68 |
{ |
|---|
| 69 |
throw new sfCommandException(sprintf('Option mode "%s" is not valid.', $mode)); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
$this->name = $name; |
|---|
| 73 |
$this->shortcut = $shortcut; |
|---|
| 74 |
$this->mode = $mode; |
|---|
| 75 |
$this->help = $help; |
|---|
| 76 |
|
|---|
| 77 |
$this->setDefault($default); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
* Returns the shortcut. |
|---|
| 82 |
* |
|---|
| 83 |
* @return string The shortcut |
|---|
| 84 |
*/ |
|---|
| 85 |
public function getShortcut() |
|---|
| 86 |
{ |
|---|
| 87 |
return $this->shortcut; |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
* Returns the name. |
|---|
| 92 |
* |
|---|
| 93 |
* @return string The name |
|---|
| 94 |
*/ |
|---|
| 95 |
public function getName() |
|---|
| 96 |
{ |
|---|
| 97 |
return $this->name; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
* Returns true if the option accept a parameter. |
|---|
| 102 |
* |
|---|
| 103 |
* @return Boolean true if parameter mode is not self::PARAMETER_NONE, false otherwise |
|---|
| 104 |
*/ |
|---|
| 105 |
public function acceptParameter() |
|---|
| 106 |
{ |
|---|
| 107 |
return $this->isParameterRequired() || $this->isParameterOptional(); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
* Returns true if the option requires a parameter. |
|---|
| 112 |
* |
|---|
| 113 |
* @return Boolean true if parameter mode is self::PARAMETER_REQUIRED, false otherwise |
|---|
| 114 |
*/ |
|---|
| 115 |
public function isParameterRequired() |
|---|
| 116 |
{ |
|---|
| 117 |
return self::PARAMETER_REQUIRED === (self::PARAMETER_REQUIRED & $this->mode); |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
* Returns true if the option takes an optional parameter. |
|---|
| 122 |
* |
|---|
| 123 |
* @return Boolean true if parameter mode is self::PARAMETER_OPTIONAL, false otherwise |
|---|
| 124 |
*/ |
|---|
| 125 |
public function isParameterOptional() |
|---|
| 126 |
{ |
|---|
| 127 |
return self::PARAMETER_OPTIONAL === (self::PARAMETER_OPTIONAL & $this->mode); |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
* Returns true if the option can take multiple values. |
|---|
| 132 |
* |
|---|
| 133 |
* @return Boolean true if mode is self::IS_ARRAY, false otherwise |
|---|
| 134 |
*/ |
|---|
| 135 |
public function isArray() |
|---|
| 136 |
{ |
|---|
| 137 |
return self::IS_ARRAY === (self::IS_ARRAY & $this->mode); |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
* Sets the default value. |
|---|
| 142 |
* |
|---|
| 143 |
* @param mixed $default The default value |
|---|
| 144 |
*/ |
|---|
| 145 |
public function setDefault($default = null) |
|---|
| 146 |
{ |
|---|
| 147 |
if (self::PARAMETER_NONE === (self::PARAMETER_NONE & $this->mode) && !is_null($default)) |
|---|
| 148 |
{ |
|---|
| 149 |
throw new sfCommandException('Cannot set a default value when using sfCommandOption::PARAMETER_NONE mode.'); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
if ($this->isArray()) |
|---|
| 153 |
{ |
|---|
| 154 |
if (is_null($default)) |
|---|
| 155 |
{ |
|---|
| 156 |
$default = array(); |
|---|
| 157 |
} |
|---|
| 158 |
else if (!is_array($default)) |
|---|
| 159 |
{ |
|---|
| 160 |
throw new sfCommandException('A default value for an array option must be an array.'); |
|---|
| 161 |
} |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
$this->default = $this->acceptParameter() ? $default : false; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
* Returns the default value. |
|---|
| 169 |
* |
|---|
| 170 |
* @return mixed The default value |
|---|
| 171 |
*/ |
|---|
| 172 |
public function getDefault() |
|---|
| 173 |
{ |
|---|
| 174 |
return $this->default; |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
* Returns the help text. |
|---|
| 179 |
* |
|---|
| 180 |
* @return string The help text |
|---|
| 181 |
*/ |
|---|
| 182 |
public function getHelp() |
|---|
| 183 |
{ |
|---|
| 184 |
return $this->help; |
|---|
| 185 |
} |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|