1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\AnnotatedCommand\Parser; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* An associative array that maps from key to default value; |
6
|
|
|
* each entry can also have a description. |
7
|
|
|
*/ |
8
|
|
|
class DefaultsWithDescriptions |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array Associative array of key : default mappings |
12
|
|
|
*/ |
13
|
|
|
protected $values; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array Associative array used like a set to indicate default value |
17
|
|
|
* exists for the key. |
18
|
|
|
*/ |
19
|
|
|
protected $hasDefault; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array Associative array of key : description mappings |
23
|
|
|
*/ |
24
|
|
|
protected $descriptions; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var mixed Default value that the default value of items in |
28
|
|
|
* the collection should take when not specified in the 'add' method. |
29
|
|
|
*/ |
30
|
|
|
protected $defaultDefault; |
31
|
|
|
|
32
|
|
|
public function __construct($values = [], $defaultDefault = null) |
33
|
|
|
{ |
34
|
|
|
$this->values = $values; |
35
|
|
|
$this->hasDefault = []; |
36
|
|
|
$this->descriptions = []; |
37
|
|
|
$this->defaultDefault = $defaultDefault; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Return just the key : default values mapping |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function getValues() |
46
|
|
|
{ |
47
|
|
|
return $this->values; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Check to see whether the speicifed key exists in the collection. |
52
|
|
|
* |
53
|
|
|
* @param string $key |
54
|
|
|
* @return boolean |
55
|
|
|
*/ |
56
|
|
|
public function exists($key) |
57
|
|
|
{ |
58
|
|
|
return array_key_exists($key, $this->values); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the value of one entry. |
63
|
|
|
* |
64
|
|
|
* @param string $key The key of the item. |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function get($key) |
68
|
|
|
{ |
69
|
|
|
if (array_key_exists($key, $this->values)) { |
70
|
|
|
return $this->values[$key]; |
71
|
|
|
} |
72
|
|
|
return $this->defaultDefault; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the description of one entry. |
77
|
|
|
* |
78
|
|
|
* @param string $key The key of the item. |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getDescription($key) |
82
|
|
|
{ |
83
|
|
|
if (array_key_exists($key, $this->descriptions)) { |
84
|
|
|
return $this->descriptions[$key]; |
85
|
|
|
} |
86
|
|
|
return ''; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add another argument to this command. |
91
|
|
|
* |
92
|
|
|
* @param string $key Name of the argument. |
93
|
|
|
* @param string $description Help text for the argument. |
94
|
|
|
* @param mixed $defaultValue The default value for the argument. |
95
|
|
|
*/ |
96
|
|
|
public function add($key, $description = '', $defaultValue = null) |
97
|
|
|
{ |
98
|
|
|
if (!$this->exists($key) || isset($defaultValue)) { |
99
|
|
|
$this->values[$key] = isset($defaultValue) ? $defaultValue : $this->defaultDefault; |
100
|
|
|
} |
101
|
|
|
unset($this->descriptions[$key]); |
102
|
|
|
if (!empty($description)) { |
103
|
|
|
$this->descriptions[$key] = $description; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Change the default value of an entry. |
109
|
|
|
* |
110
|
|
|
* @param string $key |
111
|
|
|
* @param mixed $defaultValue |
112
|
|
|
*/ |
113
|
|
|
public function setDefaultValue($key, $defaultValue) |
114
|
|
|
{ |
115
|
|
|
$this->values[$key] = $defaultValue; |
116
|
|
|
$this->hasDefault[$key] = true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Check to see if the named argument definitively has a default value. |
121
|
|
|
* |
122
|
|
|
* @param string $key |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function hasDefault($key) |
126
|
|
|
{ |
127
|
|
|
return array_key_exists($key, $this->hasDefault); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Remove an entry |
132
|
|
|
* |
133
|
|
|
* @param string $key The entry to remove |
134
|
|
|
*/ |
135
|
|
|
public function clear($key) |
136
|
|
|
{ |
137
|
|
|
unset($this->values[$key]); |
138
|
|
|
unset($this->descriptions[$key]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Rename an existing option to something else. |
143
|
|
|
*/ |
144
|
|
|
public function rename($oldName, $newName) |
145
|
|
|
{ |
146
|
|
|
$this->add($newName, $this->getDescription($oldName), $this->get($oldName)); |
147
|
|
|
$this->clear($oldName); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|