Completed
Push — master ( cb21a4...e61a24 )
by Greg
02:44
created

FormatterOptions::setConfigurationDefault()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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
    const FORMAT = 'format';
36
    const DEFAULT_FORMAT = 'default-format';
37
    const TABLE_STYLE = 'table-style';
38
    const LIST_ORIENTATION = 'list-orientation';
39
    const FIELDS = 'fields';
40
    const INCLUDE_FIELD_LABELS = 'include-field-labels';
41
    const ROW_LABELS = 'row-labels';
42
    const FIELD_LABELS = 'field-labels';
43
    const DEFAULT_FIELDS = 'default-fields';
44
45
    public function __construct($configurationData = [], $options = [])
46
    {
47
        $this->configurationData = $configurationData;
48
        $this->options = $options;
49
    }
50
51
    public function override($configurationData)
52
    {
53
        $override = new self();
54
        $override
55
            ->setConfigurationData($configurationData + $this->getConfigurationData())
56
            ->setOptions($this->getOptions());
57
        return $override;
58
    }
59
60
    public function get($key, $defaults = [], $default = false)
61
    {
62
        $value = $this->fetch($key, $defaults, $default);
63
        return $this->parse($key, $value);
64
    }
65
66
    public function getFormat($defaults = [])
67
    {
68
        return $this->get(self::FORMAT, $defaults, $this->get(self::DEFAULT_FORMAT, $defaults, ''));
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
    }
70
71
    protected function fetch($key, $defaults = [], $default = false)
72
    {
73
        $values = array_merge(
74
            $defaults,
75
            $this->getConfigurationData(),
76
            $this->getOptions(),
77
            $this->getInputOptions($defaults)
78
        );
79
        if (array_key_exists($key, $values)) {
80
            return $values[$key];
81
        }
82
        return $default;
83
    }
84
85
    protected function parse($key, $value)
86
    {
87
        $optionFormat = $this->getOptionFormat($key);
88
        if ($optionFormat) {
89
            return $this->$optionFormat($value);
90
        }
91
        return $value;
92
    }
93
94
    public function parsePropertyList($value)
95
    {
96
        return PropertyParser::parse($value);
97
    }
98
99
    protected function getOptionFormat($key)
100
    {
101
        $propertyFormats = [
102
            self::ROW_LABELS => 'PropertyList',
103
            self::FIELD_LABELS => 'PropertyList',
104
            self::DEFAULT_FIELDS => 'PropertyList',
105
        ];
106
        if (array_key_exists($key, $propertyFormats)) {
107
            return "parse{$propertyFormats[$key]}";
108
        }
109
        return false;
110
    }
111
112
    public function setConfigurationData($configurationData)
113
    {
114
        $this->configurationData = $configurationData;
115
        return $this;
116
    }
117
118
    protected function setConfigurationValue($key, $value)
119
    {
120
        $this->configurationData[$key] = $value;
121
        return $this;
122
    }
123
124
    public function setConfigurationDefault($key, $value)
125
    {
126
        if (!array_key_exists($key, $this->configurationData)) {
127
            return $this->setConfigurationValue($key, $value);
128
        }
129
        return $this;
130
    }
131
132
    public function getConfigurationData()
133
    {
134
        return $this->configurationData;
135
    }
136
137
    public function setOptions($options)
138
    {
139
        $this->options = $options;
140
        return $this;
141
    }
142
143
    public function setOption($key, $value)
144
    {
145
        $this->options[$key] = $value;
146
        return $this;
147
    }
148
149
    public function getOptions()
150
    {
151
        return $this->options;
152
    }
153
154
    public function setInput(InputInterface $input)
155
    {
156
        $this->input = $input;
157
    }
158
159
    public function getInputOptions($defaults)
160
    {
161
        if (!isset($this->input)) {
162
            return [];
163
        }
164
        $options = [];
165
        foreach ($defaults as $key => $value) {
166
            if ($this->input->hasOption($key)) {
167
                $result = $this->input->getOption($key);
168
                if (isset($result)) {
169
                    $options[$key] = $this->input->getOption($key);
170
                }
171
            }
172
        }
173
        return $options;
174
    }
175
}
176