More tidying.
[yaffs-website] / vendor / consolidation / annotated-command / src / Parser / DefaultsWithDescriptions.php
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 = array_filter($this->values, function ($value) {
36             return isset($value);
37         });
38         $this->descriptions = [];
39         $this->defaultDefault = $defaultDefault;
40     }
41
42     /**
43      * Return just the key : default values mapping
44      *
45      * @return array
46      */
47     public function getValues()
48     {
49         return $this->values;
50     }
51
52     /**
53      * Return true if this set of options is empty
54      *
55      * @return
56      */
57     public function isEmpty()
58     {
59         return empty($this->values);
60     }
61
62     /**
63      * Check to see whether the speicifed key exists in the collection.
64      *
65      * @param string $key
66      * @return boolean
67      */
68     public function exists($key)
69     {
70         return array_key_exists($key, $this->values);
71     }
72
73     /**
74      * Get the value of one entry.
75      *
76      * @param string $key The key of the item.
77      * @return string
78      */
79     public function get($key)
80     {
81         if (array_key_exists($key, $this->values)) {
82             return $this->values[$key];
83         }
84         return $this->defaultDefault;
85     }
86
87     /**
88      * Get the description of one entry.
89      *
90      * @param string $key The key of the item.
91      * @return string
92      */
93     public function getDescription($key)
94     {
95         if (array_key_exists($key, $this->descriptions)) {
96             return $this->descriptions[$key];
97         }
98         return '';
99     }
100
101     /**
102      * Add another argument to this command.
103      *
104      * @param string $key Name of the argument.
105      * @param string $description Help text for the argument.
106      * @param mixed $defaultValue The default value for the argument.
107      */
108     public function add($key, $description = '', $defaultValue = null)
109     {
110         if (!$this->exists($key) || isset($defaultValue)) {
111             $this->values[$key] = isset($defaultValue) ? $defaultValue : $this->defaultDefault;
112         }
113         unset($this->descriptions[$key]);
114         if (!empty($description)) {
115             $this->descriptions[$key] = $description;
116         }
117     }
118
119     /**
120      * Change the default value of an entry.
121      *
122      * @param string $key
123      * @param mixed $defaultValue
124      */
125     public function setDefaultValue($key, $defaultValue)
126     {
127         $this->values[$key] = $defaultValue;
128         $this->hasDefault[$key] = true;
129         return $this;
130     }
131
132     /**
133      * Check to see if the named argument definitively has a default value.
134      *
135      * @param string $key
136      * @return bool
137      */
138     public function hasDefault($key)
139     {
140         return array_key_exists($key, $this->hasDefault);
141     }
142
143     /**
144      * Remove an entry
145      *
146      * @param string $key The entry to remove
147      */
148     public function clear($key)
149     {
150         unset($this->values[$key]);
151         unset($this->descriptions[$key]);
152     }
153
154     /**
155      * Rename an existing option to something else.
156      */
157     public function rename($oldName, $newName)
158     {
159         $this->add($newName, $this->getDescription($oldName), $this->get($oldName));
160         $this->clear($oldName);
161     }
162 }