Completed
Push — master ( 22f03b...8473a4 )
by Greg
02:36
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 Method

Rating   Name   Duplication   Size   Complexity  
A FormatterOptions::getFormat() 0 4 1
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 DEFAULT_STRING_FIELD = 'default-string-field';
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
        return $this->parse($key, $value);
68
    }
69
70
    public function getXmlSchema()
71
    {
72
        return new XmlSchema();
73
    }
74
75
    public function getFormat($defaults = [])
76
    {
77
        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...
78
    }
79
80
    protected function fetch($key, $defaults = [], $default = false)
81
    {
82
        $values = array_merge(
83
            $defaults,
84
            $this->getConfigurationData(),
85
            $this->getOptions(),
86
            $this->getInputOptions($defaults)
87
        );
88
        if (array_key_exists($key, $values)) {
89
            return $values[$key];
90
        }
91
        return $default;
92
    }
93
94
    protected function parse($key, $value)
95
    {
96
        $optionFormat = $this->getOptionFormat($key);
97
        if ($optionFormat) {
98
            return $this->$optionFormat($value);
99
        }
100
        return $value;
101
    }
102
103
    public function parsePropertyList($value)
104
    {
105
        return PropertyParser::parse($value);
106
    }
107
108
    protected function getOptionFormat($key)
109
    {
110
        $propertyFormats = [
111
            self::ROW_LABELS => 'PropertyList',
112
            self::FIELD_LABELS => 'PropertyList',
113
            self::DEFAULT_FIELDS => 'PropertyList',
114
        ];
115
        if (array_key_exists($key, $propertyFormats)) {
116
            return "parse{$propertyFormats[$key]}";
117
        }
118
        return false;
119
    }
120
121
    public function setConfigurationData($configurationData)
122
    {
123
        $this->configurationData = $configurationData;
124
        return $this;
125
    }
126
127
    protected function setConfigurationValue($key, $value)
128
    {
129
        $this->configurationData[$key] = $value;
130
        return $this;
131
    }
132
133
    public function setConfigurationDefault($key, $value)
134
    {
135
        if (!array_key_exists($key, $this->configurationData)) {
136
            return $this->setConfigurationValue($key, $value);
137
        }
138
        return $this;
139
    }
140
141
    public function getConfigurationData()
142
    {
143
        return $this->configurationData;
144
    }
145
146
    public function setOptions($options)
147
    {
148
        $this->options = $options;
149
        return $this;
150
    }
151
152
    public function setOption($key, $value)
153
    {
154
        $this->options[$key] = $value;
155
        return $this;
156
    }
157
158
    public function getOptions()
159
    {
160
        return $this->options;
161
    }
162
163
    public function setInput(InputInterface $input)
164
    {
165
        $this->input = $input;
166
    }
167
168
    public function getInputOptions($defaults)
169
    {
170
        if (!isset($this->input)) {
171
            return [];
172
        }
173
        $options = [];
174
        foreach ($defaults as $key => $value) {
175
            if ($this->input->hasOption($key)) {
176
                $result = $this->input->getOption($key);
177
                if (isset($result)) {
178
                    $options[$key] = $this->input->getOption($key);
179
                }
180
            }
181
        }
182
        return $options;
183
    }
184
}
185