1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace clearice; |
4
|
|
|
|
5
|
|
|
class Options //implements \ArrayAccess, \Iterator |
6
|
|
|
{ |
7
|
|
|
private $options = []; |
8
|
|
|
private $index = 0; |
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A map of all the options the parser recognises. |
12
|
|
|
* The map is actually an array which associates short or long options with |
13
|
|
|
* their appropriate parameters. Options which have both long and short |
14
|
|
|
* versions would be repeated. This structure is used to quickly find the |
15
|
|
|
* paramters of an option whether in the short form or long form. This |
16
|
|
|
* parameter is automatically populated by the library as options are added. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $map; |
21
|
|
|
|
22
|
27 |
|
public function add($options) |
23
|
|
|
{ |
24
|
27 |
|
foreach ($options as $option) { |
25
|
27 |
|
if (is_string($option)) { |
26
|
4 |
|
$option = $this->stringOptionToArray($option); |
27
|
4 |
|
} |
28
|
27 |
|
$option = $this->fillOption($option); |
29
|
27 |
|
$this->options[] = $option; |
30
|
27 |
|
$command = isset($option['command']) ? $option['command'] : '__default__'; |
31
|
27 |
|
if (isset($option['short'])) { |
32
|
26 |
|
$this->map[$command][$option['short']] = $option; |
33
|
26 |
|
} |
34
|
27 |
|
if (isset($option['long'])) { |
35
|
27 |
|
$this->map[$command][$option['long']] = $option; |
36
|
27 |
|
} |
37
|
27 |
|
} |
38
|
27 |
|
$this->options += $options; |
39
|
27 |
|
} |
40
|
|
|
|
41
|
27 |
|
private function fillOption($option) |
42
|
|
|
{ |
43
|
27 |
|
$option['has_value'] = isset($option['has_value']) ? $option['has_value'] : false; |
44
|
27 |
|
$option['command'] = isset($option['command']) ? $option['command'] : null; |
45
|
27 |
|
$option['multi'] = isset($option['multi']) ? $option['multi'] : null; |
46
|
27 |
|
$option['group'] = isset($option['group']) ? $option['group'] : null; |
47
|
27 |
|
return $option; |
48
|
|
|
} |
49
|
|
|
|
50
|
24 |
|
public function getMap() |
51
|
|
|
{ |
52
|
24 |
|
return $this->map; |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
private function stringOptionToArray($option) |
56
|
|
|
{ |
57
|
4 |
|
$newOption = []; |
58
|
4 |
|
if (strlen($option) == 1) { |
59
|
1 |
|
$newOption['short'] = $option; |
60
|
1 |
|
} else { |
61
|
4 |
|
$newOption['long'] = $option; |
62
|
|
|
} |
63
|
4 |
|
return $newOption; |
64
|
|
|
} |
65
|
|
|
|
66
|
8 |
|
public function getArray() |
67
|
|
|
{ |
68
|
8 |
|
return $this->options; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
|
|
|
|
72
|
|
|
|
73
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.