1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\OutputFormatters\Options; |
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 FIELD = 'field'; |
43
|
|
|
const INCLUDE_FIELD_LABELS = 'include-field-labels'; |
44
|
|
|
const ROW_LABELS = 'row-labels'; |
45
|
|
|
const FIELD_LABELS = 'field-labels'; |
46
|
|
|
const DEFAULT_FIELDS = 'default-fields'; |
47
|
|
|
const DEFAULT_STRING_FIELD = 'default-string-field'; |
48
|
|
|
const DELIMITER = 'delimiter'; |
49
|
|
|
|
50
|
|
|
public function __construct($configurationData = [], $options = []) |
51
|
|
|
{ |
52
|
|
|
$this->configurationData = $configurationData; |
53
|
|
|
$this->options = $options; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function override($configurationData) |
57
|
|
|
{ |
58
|
|
|
$override = new self(); |
59
|
|
|
$override |
60
|
|
|
->setConfigurationData($configurationData + $this->getConfigurationData()) |
61
|
|
|
->setOptions($this->getOptions()); |
62
|
|
|
return $override; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get a formatter option |
67
|
|
|
* @param string $key |
68
|
|
|
* @param array $defaults |
69
|
|
|
* @param mixed $default |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function get($key, $defaults = [], $default = false) |
73
|
|
|
{ |
74
|
|
|
$value = $this->fetch($key, $defaults, $default); |
75
|
|
|
return $this->parse($key, $value); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getXmlSchema() |
79
|
|
|
{ |
80
|
|
|
return new XmlSchema(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getFormat($defaults = []) |
84
|
|
|
{ |
85
|
|
|
return $this->get(self::FORMAT, [], $this->get(self::DEFAULT_FORMAT, $defaults, '')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function fetch($key, $defaults = [], $default = false) |
89
|
|
|
{ |
90
|
|
|
$values = array_merge( |
91
|
|
|
$defaults, |
92
|
|
|
$this->getConfigurationData(), |
93
|
|
|
$this->getOptions(), |
94
|
|
|
$this->getInputOptions($defaults) |
95
|
|
|
); |
96
|
|
|
if (array_key_exists($key, $values)) { |
97
|
|
|
return $values[$key]; |
98
|
|
|
} |
99
|
|
|
return $default; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function parse($key, $value) |
103
|
|
|
{ |
104
|
|
|
$optionFormat = $this->getOptionFormat($key); |
105
|
|
|
if ($optionFormat) { |
106
|
|
|
return $this->$optionFormat($value); |
107
|
|
|
} |
108
|
|
|
return $value; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function parsePropertyList($value) |
112
|
|
|
{ |
113
|
|
|
return PropertyParser::parse($value); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function getOptionFormat($key) |
117
|
|
|
{ |
118
|
|
|
$propertyFormats = [ |
119
|
|
|
self::ROW_LABELS => 'PropertyList', |
120
|
|
|
self::FIELD_LABELS => 'PropertyList', |
121
|
|
|
]; |
122
|
|
|
if (array_key_exists($key, $propertyFormats)) { |
123
|
|
|
return "parse{$propertyFormats[$key]}"; |
124
|
|
|
} |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function setConfigurationData($configurationData) |
129
|
|
|
{ |
130
|
|
|
$this->configurationData = $configurationData; |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function setConfigurationValue($key, $value) |
135
|
|
|
{ |
136
|
|
|
$this->configurationData[$key] = $value; |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function setConfigurationDefault($key, $value) |
141
|
|
|
{ |
142
|
|
|
if (!array_key_exists($key, $this->configurationData)) { |
143
|
|
|
return $this->setConfigurationValue($key, $value); |
144
|
|
|
} |
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getConfigurationData() |
149
|
|
|
{ |
150
|
|
|
return $this->configurationData; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function setOptions($options) |
154
|
|
|
{ |
155
|
|
|
$this->options = $options; |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function setOption($key, $value) |
160
|
|
|
{ |
161
|
|
|
$this->options[$key] = $value; |
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getOptions() |
166
|
|
|
{ |
167
|
|
|
return $this->options; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function setInput(InputInterface $input) |
171
|
|
|
{ |
172
|
|
|
$this->input = $input; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getInputOptions($defaults) |
176
|
|
|
{ |
177
|
|
|
if (!isset($this->input)) { |
178
|
|
|
return []; |
179
|
|
|
} |
180
|
|
|
$options = []; |
181
|
|
|
foreach ($defaults as $key => $value) { |
182
|
|
|
if ($this->input->hasOption($key)) { |
183
|
|
|
$result = $this->input->getOption($key); |
184
|
|
|
if (isset($result)) { |
185
|
|
|
$options[$key] = $this->input->getOption($key); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
return $options; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|