Completed
Pull Request — master (#21)
by Greg
03:15
created

FormatterOptions::getXmlSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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