| 1 |
<?php |
| 2 |
|
| 3 |
namespace MrClay\Cli; |
| 4 |
|
| 5 |
use BadMethodCallException; |
| 6 |
|
| 7 |
/** |
| 8 |
* An argument for a CLI app. This specifies the argument, what values it expects and |
| 9 |
* how it's treated during validation. |
| 10 |
* |
| 11 |
* By default, the argument will be assumed to be an optional letter flag with no value following. |
| 12 |
* |
| 13 |
* If the argument may receive a value, call mayHaveValue(). If there's whitespace after the |
| 14 |
* flag, the value will be returned as true instead of the string. |
| 15 |
* |
| 16 |
* If the argument MUST be accompanied by a value, call mustHaveValue(). In this case, whitespace |
| 17 |
* is permitted between the flag and its value. |
| 18 |
* |
| 19 |
* Use assertFile() or assertDir() to indicate that the argument must return a string value |
| 20 |
* specifying a file or directory. During validation, the value will be resolved to a |
| 21 |
* full file/dir path (not necessarily existing!) and the original value will be accessible |
| 22 |
* via a "*.raw" key. E.g. $cli->values['f.raw'] |
| 23 |
* |
| 24 |
* Use assertReadable()/assertWritable() to cause the validator to test the file/dir for |
| 25 |
* read/write permissions respectively. |
| 26 |
* |
| 27 |
* @method \MrClay\Cli\Arg mayHaveValue() Assert that the argument, if present, may receive a string value |
| 28 |
* @method \MrClay\Cli\Arg mustHaveValue() Assert that the argument, if present, must receive a string value |
| 29 |
* @method \MrClay\Cli\Arg assertFile() Assert that the argument's value must specify a file |
| 30 |
* @method \MrClay\Cli\Arg assertDir() Assert that the argument's value must specify a directory |
| 31 |
* @method \MrClay\Cli\Arg assertReadable() Assert that the specified file/dir must be readable |
| 32 |
* @method \MrClay\Cli\Arg assertWritable() Assert that the specified file/dir must be writable |
| 33 |
* |
| 34 |
* @property-read bool mayHaveValue |
| 35 |
* @property-read bool mustHaveValue |
| 36 |
* @property-read bool assertFile |
| 37 |
* @property-read bool assertDir |
| 38 |
* @property-read bool assertReadable |
| 39 |
* @property-read bool assertWritable |
| 40 |
* @property-read bool useAsInfile |
| 41 |
* @property-read bool useAsOutfile |
| 42 |
* |
| 43 |
* @author Steve Clay <[email protected]> |
| 44 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
| 45 |
*/ |
| 46 |
class Arg |
| 47 |
{ |
| 48 |
/** |
| 49 |
* @return array |
| 50 |
*/ |
| 51 |
public function getDefaultSpec() |
| 52 |
{ |
| 53 |
return array( |
| 54 |
'mayHaveValue' => false, |
| 55 |
'mustHaveValue' => false, |
| 56 |
'assertFile' => false, |
| 57 |
'assertDir' => false, |
| 58 |
'assertReadable' => false, |
| 59 |
'assertWritable' => false, |
| 60 |
'useAsInfile' => false, |
| 61 |
'useAsOutfile' => false, |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @var array |
| 67 |
*/ |
| 68 |
protected $spec = array(); |
| 69 |
|
| 70 |
/** |
| 71 |
* @var bool |
| 72 |
*/ |
| 73 |
protected $required = false; |
| 74 |
|
| 75 |
/** |
| 76 |
* @var string |
| 77 |
*/ |
| 78 |
protected $description = ''; |
| 79 |
|
| 80 |
/** |
| 81 |
* @param bool $isRequired |
| 82 |
*/ |
| 83 |
public function __construct($isRequired = false) |
| 84 |
{ |
| 85 |
$this->spec = $this->getDefaultSpec(); |
| 86 |
$this->required = (bool) $isRequired; |
| 87 |
if ($isRequired) { |
| 88 |
$this->spec['mustHaveValue'] = true; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Assert that the argument's value points to a writable file. When |
| 94 |
* Cli::openOutput() is called, a write pointer to this file will |
| 95 |
* be provided. |
| 96 |
* @return Arg |
| 97 |
*/ |
| 98 |
public function useAsOutfile() |
| 99 |
{ |
| 100 |
$this->spec['useAsOutfile'] = true; |
| 101 |
|
| 102 |
return $this->assertFile()->assertWritable(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Assert that the argument's value points to a readable file. When |
| 107 |
* Cli::openInput() is called, a read pointer to this file will |
| 108 |
* be provided. |
| 109 |
* @return Arg |
| 110 |
*/ |
| 111 |
public function useAsInfile() |
| 112 |
{ |
| 113 |
$this->spec['useAsInfile'] = true; |
| 114 |
|
| 115 |
return $this->assertFile()->assertReadable(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
public function getSpec() |
| 122 |
{ |
| 123 |
return $this->spec; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @param string $desc |
| 128 |
* @return Arg |
| 129 |
*/ |
| 130 |
public function setDescription($desc) |
| 131 |
{ |
| 132 |
$this->description = $desc; |
| 133 |
|
| 134 |
return $this; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
public function getDescription() |
| 141 |
{ |
| 142 |
return $this->description; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @return bool |
| 147 |
*/ |
| 148 |
public function isRequired() |
| 149 |
{ |
| 150 |
return $this->required; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Note: magic methods declared in class PHPDOC |
| 155 |
* |
| 156 |
* @param string $name |
| 157 |
* @param array $args |
| 158 |
* @return Arg |
| 159 |
* @throws BadMethodCallException |
| 160 |
*/ |
| 161 |
public function __call($name, array $args = array()) |
| 162 |
{ |
| 163 |
if (array_key_exists($name, $this->spec)) { |
| 164 |
$this->spec[$name] = true; |
| 165 |
if ($name === 'assertFile' || $name === 'assertDir') { |
| 166 |
$this->spec['mustHaveValue'] = true; |
| 167 |
} |
| 168 |
} else { |
| 169 |
throw new BadMethodCallException('Method does not exist'); |
| 170 |
} |
| 171 |
|
| 172 |
return $this; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Note: magic properties declared in class PHPDOC |
| 177 |
* |
| 178 |
* @param string $name |
| 179 |
* @return bool|null |
| 180 |
*/ |
| 181 |
public function __get($name) |
| 182 |
{ |
| 183 |
if (array_key_exists($name, $this->spec)) { |
| 184 |
return $this->spec[$name]; |
| 185 |
} |
| 186 |
|
| 187 |
return null; |
| 188 |
} |
| 189 |
} |
| 190 |
|