Completed
Pull Request — master (#23)
by Greg
03:09
created

FormatterOptions::convert()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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