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