Completed
Push — master ( af6901...cf3b49 )
by Greg
10s
created

FormatterOptions::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Consolidation\OutputFormatters;
3
4
use Symfony\Component\Console\Input\InputInterface;
5
use Consolidation\OutputFormatters\Transformations\PropertyParser;
6
7
/**
8
 * FormetterOptions holds information that affects the way a formatter
9
 * renders its output.
10
 *
11
 * There are three places where a formatter might get options from:
12
 *
13
 * 1. Configuration associated with the command that produced the output.
14
 *    This is passed in to FormatterManager::write() along with the data
15
 *    to format.  It might originally come from annotations on the command,
16
 *    or it might come from another source.  Examples include the field labels
17
 *    for a table, or the default list of fields to display.
18
 *
19
 * 2. Options specified by the user, e.g. by commandline options.
20
 *
21
 * 3. Default values associated with the formatter itself.
22
 *
23
 * This class caches configuration from sources (1) and (2), and expects
24
 * to be provided the defaults, (3), whenever a value is requested.
25
 */
26
class FormatterOptions
27
{
28
    /** var array */
29
    protected $configurationData = [];
30
    /** var array */
31
    protected $options = [];
32
    /** var InputInterface */
33
    protected $input;
34
35
    public function __construct($configurationData = [], $options = [])
36
    {
37
        $this->configurationData = $configurationData;
38
        $this->options = $options;
39
    }
40
41
    public function override($configurationData)
42
    {
43
        $override = new self();
44
        $override
45
            ->setConfigurationData($configurationData + $this->getConfigurationData())
46
            ->setOptions($this->getOptions());
47
        return $override;
48
    }
49
50
    public function get($key, $defaults = [], $default = false)
51
    {
52
        $value = $this->fetch($key, $defaults, $default);
53
        return $this->parse($key, $value);
54
    }
55
56
    protected function fetch($key, $defaults = [], $default = false)
57
    {
58
        $values = array_merge(
59
            $defaults,
60
            $this->getConfigurationData(),
61
            $this->getOptions(),
62
            $this->getInputOptions($defaults)
63
        );
64
        if (array_key_exists($key, $values)) {
65
            return $values[$key];
66
        }
67
        return $default;
68
    }
69
70
    protected function parse($key, $value)
71
    {
72
        $optionFormat = $this->getOptionFormat($key);
73
        if ($optionFormat) {
74
            return $this->$optionFormat($value);
75
        }
76
        return $value;
77
    }
78
79
    public function parsePropertyList($value)
80
    {
81
        return PropertyParser::parse($value);
82
    }
83
84
    protected function getOptionFormat($key)
85
    {
86
        $propertyFormats = [
87
            'row-labels' => 'PropertyList',
88
            'field-labels' => 'PropertyList',
89
            'default-fields' => 'PropertyList',
90
        ];
91
        if (array_key_exists($key, $propertyFormats)) {
92
            return "parse{$propertyFormats[$key]}";
93
        }
94
        return false;
95
    }
96
97
    public function setConfigurationData($configurationData)
98
    {
99
        $this->configurationData = $configurationData;
100
        return $this;
101
    }
102
103
    public function setConfigurationValue($key, $value)
104
    {
105
        $this->configurationData[$key] = $value;
106
        return $this;
107
    }
108
109
    public function setConfigurationDefault($key, $value)
110
    {
111
        if (!array_key_exists($key, $this->configurationData)) {
112
            return $this->setConfigurationValue($key, $value);
113
        }
114
        return $this;
115
    }
116
117
    public function getConfigurationData()
118
    {
119
        return $this->configurationData;
120
    }
121
122
    public function setOptions($options)
123
    {
124
        $this->options = $options;
125
        return $this;
126
    }
127
128
    public function setOption($key, $value)
129
    {
130
        $this->options[$key] = $value;
131
        return $this;
132
    }
133
134
    public function getOptions()
135
    {
136
        return $this->options;
137
    }
138
139
    public function setInput(InputInterface $input)
140
    {
141
        $this->input = $input;
142
    }
143
144
    public function getInputOptions($defaults)
145
    {
146
        if (!isset($this->input)) {
147
            return [];
148
        }
149
        $options = [];
150
        foreach ($defaults as $key => $value) {
151
            if ($this->input->hasOption($key)) {
152
                $options[$key] = $this->input->getOption($key);
153
            }
154
        }
155
        return $options;
156
    }
157
}
158