1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Parameter.php |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace netfocusinc\argh; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Parameters define the arguments your CLI application can recieve. |
11
|
|
|
* |
12
|
|
|
* Parameters are pre-configured arguments that your CLI application can retrieve |
13
|
|
|
* from command line arguments. |
14
|
|
|
* |
15
|
|
|
* @api |
16
|
|
|
* |
17
|
|
|
* @author Benjamin Hough |
18
|
|
|
* |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
abstract class Parameter |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
// |
25
|
|
|
// CONSTANTS |
26
|
|
|
// |
27
|
|
|
|
28
|
|
|
// Parameter Names |
29
|
|
|
const ARGH_NAME_VARIABLE = '_ARGH_VARIABLE_'; |
30
|
|
|
|
31
|
|
|
// Parameter Data Types |
32
|
|
|
const ARGH_TYPE_BOOLEAN = 1; |
33
|
|
|
const ARGH_TYPE_INT = 2; |
34
|
|
|
const ARGH_TYPE_STRING = 3; |
35
|
|
|
const ARGH_TYPE_LIST = 4; |
36
|
|
|
const ARGH_TYPE_COMMAND = 5; |
37
|
|
|
const ARGH_TYPE_VARIABLE = 6; |
38
|
|
|
|
39
|
|
|
// |
40
|
|
|
// PUBLIC PROPERTIES |
41
|
|
|
// |
42
|
|
|
|
43
|
|
|
/** @var string The name of a Parameter. Can be used on the command line; e.g. -file */ |
44
|
|
|
private $name; |
45
|
|
|
|
46
|
|
|
/** @var string A single character flag used to refer to this Parameter. */ |
47
|
|
|
private $flag; |
48
|
|
|
|
49
|
|
|
/** @var bool Is the Parameter required? */ |
50
|
|
|
private $required; |
51
|
|
|
|
52
|
|
|
/** @var mixed The default value of the Parameter */ |
53
|
|
|
private $default; |
54
|
|
|
|
55
|
|
|
/** @var string Descriptive text to print with 'usage' */ |
56
|
|
|
private $description; |
57
|
|
|
|
58
|
|
|
/** @var array A list limiting the options for this Parameters value */ |
59
|
|
|
private $options; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var mixed The value of this parameter. |
63
|
|
|
* |
64
|
|
|
* Null indicates no Argument was supplied on the command line. |
65
|
|
|
* |
66
|
|
|
* Each Parameter may have its own type of value (e.g. int, string, array) |
67
|
|
|
*/ |
68
|
|
|
protected $value; |
69
|
|
|
|
70
|
|
|
// |
71
|
|
|
// STATIC METHODS |
72
|
|
|
// |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates a new Parameter (sub-type) using the provided attributes |
76
|
|
|
* |
77
|
|
|
* This function is called statically on the subtypes of Parameter (e.g. BooleanParameter) |
78
|
|
|
* It uses the supplied attributes to construct a new Parameter. |
79
|
|
|
* |
80
|
|
|
* @api |
81
|
|
|
* |
82
|
|
|
* @since 1.0.0 |
83
|
|
|
* |
84
|
|
|
* @param array $attributes |
85
|
|
|
* |
86
|
|
|
* @return Parameter |
87
|
|
|
* @throws ArghException |
88
|
|
|
*/ |
89
|
|
|
public static function createWithAttributes(array $attributes): Parameter |
90
|
|
|
{ |
91
|
|
|
// Init default attributes for a new Parameter |
92
|
|
|
$name = null; |
93
|
|
|
$flag = null; |
94
|
|
|
$required = FALSE; |
95
|
|
|
$default = null; |
96
|
|
|
$description = null; |
97
|
|
|
$options = array(); |
98
|
|
|
|
99
|
|
|
// Extract parameter attributes from array |
100
|
|
|
if( array_key_exists('name', $attributes) ) $name = $attributes['name']; |
101
|
|
|
if( array_key_exists('flag', $attributes) ) $flag = $attributes['flag']; |
102
|
|
|
if( array_key_exists('required', $attributes) ) $required = $attributes['required']; |
103
|
|
|
if( array_key_exists('default', $attributes) ) $default = $attributes['default']; |
104
|
|
|
if( array_key_exists('description', $attributes) ) $description = $attributes['description']; |
105
|
|
|
if( array_key_exists('options', $attributes) ) $options = $attributes['options']; |
106
|
|
|
|
107
|
|
|
// Construct a new Parameter instance |
108
|
|
|
// Late static binding results in new instance of (calling) subclass |
109
|
|
|
|
110
|
|
|
//! TODO: What if this is called on abstract Parameter |
111
|
|
|
return new static($name, $flag, $required, $default, $description, $options); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// |
115
|
|
|
// PUBLIC METHODS |
116
|
|
|
// |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Parameter contructor. |
120
|
|
|
* |
121
|
|
|
* This function defines a constructor that is leveraged by Parameter sub-types. |
122
|
|
|
* Normally, Parameter (sub-types, e.g. BooleanParameter) are creating using the static Parameter:createWithAttributes() function. |
123
|
|
|
* Parameter is an abstract class, and as such cannot be instantiated directly. |
124
|
|
|
* |
125
|
|
|
* @since 1.0.0 |
126
|
|
|
* |
127
|
|
|
* @param string $name |
128
|
|
|
* @param string $flag |
129
|
|
|
* @param bool $required |
130
|
|
|
* @param mixed $default |
131
|
|
|
* @param string $description |
132
|
|
|
* @param array $options |
133
|
|
|
* |
134
|
|
|
* @return Parameter |
135
|
|
|
* @throws ArghException |
136
|
|
|
*/ |
137
|
|
|
public function __construct(string $name, string $flag=null, bool $required=FALSE, $default=null, string $description=null, array $options=array()) |
138
|
|
|
{ |
139
|
|
|
// Required a non-empty 'name' |
140
|
|
|
if(empty($name)) |
141
|
|
|
{ |
142
|
|
|
throw(new ArghException('Parameter must have a name')); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Set properties on this object |
146
|
|
|
$this->name = $name; |
147
|
|
|
$this->flag = $flag; |
148
|
|
|
$this->required = $required; |
149
|
|
|
$this->default = $default; |
150
|
|
|
$this->description = $description; |
151
|
|
|
$this->options = $options; |
152
|
|
|
|
153
|
|
|
// New Parameters ALWAYS have null value |
154
|
|
|
$this->value = null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// |
158
|
|
|
// GETTERS |
159
|
|
|
// |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the text 'name' of this Parameter |
163
|
|
|
* |
164
|
|
|
* @since 1.0.0 |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function getName(): string { return $this->name; } |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns the character 'flag' of this Parameter |
172
|
|
|
* |
173
|
|
|
* @since 1.0.0 |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getFlag() { return $this->flag; } |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Returns the a boolean value indicating if this Parameter is required |
181
|
|
|
* |
182
|
|
|
* @since 1.0.0 |
183
|
|
|
* |
184
|
|
|
* @return boolean |
185
|
|
|
*/ |
186
|
|
|
public function isRequired(): bool { return $this->required; } |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns the 'default' value of this Parmater |
190
|
|
|
* |
191
|
|
|
* @since 1.0.0 |
192
|
|
|
* |
193
|
|
|
* @return mixed |
194
|
|
|
*/ |
195
|
|
|
public function getDefault() { return $this->default; } |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns the text 'description' of this Parameter |
199
|
|
|
* |
200
|
|
|
* @since 1.0.0 |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
public function getDescription() { return $this->description; } |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Returns an array of 'options' that are legal for this Parameters 'value' |
208
|
|
|
* |
209
|
|
|
* @since 1.0.0 |
210
|
|
|
* |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
|
|
public function getOptions(): array { return $this->options; } |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns a boolean indicating if this Parameter has any defined 'options' |
217
|
|
|
* |
218
|
|
|
* @since 1.0.0 |
219
|
|
|
* |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
public function hasOptions(): bool |
223
|
|
|
{ |
224
|
|
|
if(count($this->options) > 0) |
225
|
|
|
{ |
226
|
|
|
return TRUE; |
227
|
|
|
} |
228
|
|
|
else |
229
|
|
|
{ |
230
|
|
|
return FALSE; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns a boolean indicating if the specified $value is a permissible 'option' of this Parameter |
236
|
|
|
* |
237
|
|
|
* @since 1.0.0 |
238
|
|
|
* |
239
|
|
|
* @param mixed $value |
240
|
|
|
* |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
|
|
public function isOption($value): bool |
244
|
|
|
{ |
245
|
|
|
if( in_array($value, $this->options) ) |
246
|
|
|
{ |
247
|
|
|
return TRUE; |
248
|
|
|
} |
249
|
|
|
else |
250
|
|
|
{ |
251
|
|
|
return FALSE; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Returns the 'value' of this Parameter |
257
|
|
|
* |
258
|
|
|
* @since 1.0.0 |
259
|
|
|
* |
260
|
|
|
* @return mixed |
261
|
|
|
*/ |
262
|
|
|
public function getValue() |
263
|
|
|
{ |
264
|
|
|
return $this->value; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// |
268
|
|
|
// ABSTRACT METHODS |
269
|
|
|
// |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Returns an int corresponding to this Parameters type |
273
|
|
|
* |
274
|
|
|
* @since 1.0.0 |
275
|
|
|
* |
276
|
|
|
* @return int |
277
|
|
|
*/ |
278
|
|
|
abstract public function getParameterType(): int; |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Sets the 'value' of this Paramter |
282
|
|
|
* |
283
|
|
|
* @since 1.0.0 |
284
|
|
|
* |
285
|
|
|
* @param mixed $value |
286
|
|
|
*/ |
287
|
|
|
abstract public function setValue($value); |
288
|
|
|
|
289
|
|
|
} |
290
|
|
|
|