| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfCommandArgument |
|---|
| 20 |
{ |
|---|
| 21 |
const REQUIRED = 1; |
|---|
| 22 |
const OPTIONAL = 2; |
|---|
| 23 |
|
|---|
| 24 |
const IS_ARRAY = 4; |
|---|
| 25 |
|
|---|
| 26 |
protected |
|---|
| 27 |
$name = null, |
|---|
| 28 |
$mode = null, |
|---|
| 29 |
$default = null, |
|---|
| 30 |
$help = ''; |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
* Constructor. |
|---|
| 34 |
* |
|---|
| 35 |
* @param string $name The argument name |
|---|
| 36 |
* @param integer $mode The argument mode: self::REQUIRED or self::OPTIONAL |
|---|
| 37 |
* @param string $help A help text |
|---|
| 38 |
* @param mixed $default The default value (for self::OPTIONAL mode only) |
|---|
| 39 |
*/ |
|---|
| 40 |
public function __construct($name, $mode = null, $help = '', $default = null) |
|---|
| 41 |
{ |
|---|
| 42 |
if (null === $mode) |
|---|
| 43 |
{ |
|---|
| 44 |
$mode = self::OPTIONAL; |
|---|
| 45 |
} |
|---|
| 46 |
else if (is_string($mode) || $mode > 7) |
|---|
| 47 |
{ |
|---|
| 48 |
throw new sfCommandException(sprintf('Argument mode "%s" is not valid.', $mode)); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
$this->name = $name; |
|---|
| 52 |
$this->mode = $mode; |
|---|
| 53 |
$this->help = $help; |
|---|
| 54 |
|
|---|
| 55 |
$this->setDefault($default); |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
* Returns the argument name. |
|---|
| 60 |
* |
|---|
| 61 |
* @return string The argument name |
|---|
| 62 |
*/ |
|---|
| 63 |
public function getName() |
|---|
| 64 |
{ |
|---|
| 65 |
return $this->name; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
* Returns true if the argument is required. |
|---|
| 70 |
* |
|---|
| 71 |
* @return Boolean true if parameter mode is self::REQUIRED, false otherwise |
|---|
| 72 |
*/ |
|---|
| 73 |
public function isRequired() |
|---|
| 74 |
{ |
|---|
| 75 |
return self::REQUIRED === (self::REQUIRED & $this->mode); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
* Returns true if the argument can take multiple values. |
|---|
| 80 |
* |
|---|
| 81 |
* @return Boolean true if mode is self::IS_ARRAY, false otherwise |
|---|
| 82 |
*/ |
|---|
| 83 |
public function isArray() |
|---|
| 84 |
{ |
|---|
| 85 |
return self::IS_ARRAY === (self::IS_ARRAY & $this->mode); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
* Sets the default value. |
|---|
| 90 |
* |
|---|
| 91 |
* @param mixed $default The default value |
|---|
| 92 |
*/ |
|---|
| 93 |
public function setDefault($default = null) |
|---|
| 94 |
{ |
|---|
| 95 |
if (self::REQUIRED === $this->mode && null !== $default) |
|---|
| 96 |
{ |
|---|
| 97 |
throw new sfCommandException('Cannot set a default value except for sfCommandParameter::OPTIONAL mode.'); |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
if ($this->isArray()) |
|---|
| 101 |
{ |
|---|
| 102 |
if (null === $default) |
|---|
| 103 |
{ |
|---|
| 104 |
$default = array(); |
|---|
| 105 |
} |
|---|
| 106 |
else if (!is_array($default)) |
|---|
| 107 |
{ |
|---|
| 108 |
throw new sfCommandException('A default value for an array argument must be an array.'); |
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
$this->default = $default; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
* Returns the default value. |
|---|
| 117 |
* |
|---|
| 118 |
* @return mixed The default value |
|---|
| 119 |
*/ |
|---|
| 120 |
public function getDefault() |
|---|
| 121 |
{ |
|---|
| 122 |
return $this->default; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
* Returns the help text. |
|---|
| 127 |
* |
|---|
| 128 |
* @return string The help text |
|---|
| 129 |
*/ |
|---|
| 130 |
public function getHelp() |
|---|
| 131 |
{ |
|---|
| 132 |
return $this->help; |
|---|
| 133 |
} |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|