Completed
Push — master ( 1c5efd...75f82a )
by Greg
12s
created

DefaultsWithDescriptions   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 123
rs 10

9 Methods

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