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