2 namespace Consolidation\OutputFormatters\Options;
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;
10 * FormetterOptions holds information that affects the way a formatter
13 * There are three places where a formatter might get options from:
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.
21 * 2. Options specified by the user, e.g. by commandline options.
23 * 3. Default values associated with the formatter itself.
25 * This class caches configuration from sources (1) and (2), and expects
26 * to be provided the defaults, (3), whenever a value is requested.
28 class FormatterOptions
31 protected $configurationData = [];
33 protected $options = [];
34 /** var InputInterface */
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 const LIST_DELIMITER = 'list-delimiter';
50 const TERMINAL_WIDTH = 'width';
53 * Create a new FormatterOptions with the configuration data and the
54 * user-specified options for this request.
56 * @see FormatterOptions::setInput()
57 * @param array $configurationData
58 * @param array $options
60 public function __construct($configurationData = [], $options = [])
62 $this->configurationData = $configurationData;
63 $this->options = $options;
67 * Create a new FormatterOptions object with new configuration data (provided),
68 * and the same options data as this instance.
70 * @param array $configurationData
71 * @return FormatterOptions
73 public function override($configurationData)
75 $override = new self();
77 ->setConfigurationData($configurationData + $this->getConfigurationData())
78 ->setOptions($this->getOptions());
82 public function setTableStyle($style)
84 return $this->setConfigurationValue(self::TABLE_STYLE, $style);
87 public function setDelimiter($delimiter)
89 return $this->setConfigurationValue(self::DELIMITER, $delimiter);
92 public function setListDelimiter($listDelimiter)
94 return $this->setConfigurationValue(self::LIST_DELIMITER, $listDelimiter);
99 public function setIncludeFieldLables($includFieldLables)
101 return $this->setConfigurationValue(self::INCLUDE_FIELD_LABELS, $includFieldLables);
104 public function setListOrientation($listOrientation)
106 return $this->setConfigurationValue(self::LIST_ORIENTATION, $listOrientation);
109 public function setRowLabels($rowLabels)
111 return $this->setConfigurationValue(self::ROW_LABELS, $rowLabels);
114 public function setDefaultFields($fields)
116 return $this->setConfigurationValue(self::DEFAULT_FIELDS, $fields);
119 public function setFieldLabels($fieldLabels)
121 return $this->setConfigurationValue(self::FIELD_LABELS, $fieldLabels);
124 public function setDefaultStringField($defaultStringField)
126 return $this->setConfigurationValue(self::DEFAULT_STRING_FIELD, $defaultStringField);
129 public function setWidth($width)
131 return $this->setConfigurationValue(self::TERMINAL_WIDTH, $width);
135 * Get a formatter option
138 * @param array $defaults
139 * @param mixed $default
142 public function get($key, $defaults = [], $default = false)
144 $value = $this->fetch($key, $defaults, $default);
145 return $this->parse($key, $value);
149 * Return the XmlSchema to use with --format=xml for data types that support
150 * that. This is used when an array needs to be converted into xml.
154 public function getXmlSchema()
156 return new XmlSchema();
160 * Determine the format that was requested by the caller.
162 * @param array $defaults
165 public function getFormat($defaults = [])
167 return $this->get(self::FORMAT, [], $this->get(self::DEFAULT_FORMAT, $defaults, ''));
171 * Look up a key, and return its raw value.
174 * @param array $defaults
175 * @param mixed $default
178 protected function fetch($key, $defaults = [], $default = false)
180 $defaults = $this->defaultsForKey($key, $defaults, $default);
181 $values = $this->fetchRawValues($defaults);
182 return $values[$key];
186 * Reduce provided defaults to the single item identified by '$key',
187 * if it exists, or an empty array otherwise.
190 * @param array $defaults
193 protected function defaultsForKey($key, $defaults, $default = false)
195 if (array_key_exists($key, $defaults)) {
196 return [$key => $defaults[$key]];
198 return [$key => $default];
202 * Look up all of the items associated with the provided defaults.
204 * @param array $defaults
207 protected function fetchRawValues($defaults = [])
211 $this->getConfigurationData(),
213 $this->getInputOptions($defaults)
218 * Given the raw value for a specific key, do any type conversion
219 * (e.g. from a textual list to an array) needed for the data.
222 * @param mixed $value
225 protected function parse($key, $value)
227 $optionFormat = $this->getOptionFormat($key);
228 if (!empty($optionFormat) && is_string($value)) {
229 return $this->$optionFormat($value);
235 * Convert from a textual list to an array
237 * @param string $value
240 public function parsePropertyList($value)
242 return PropertyParser::parse($value);
246 * Given a specific key, return the class method name of the
247 * parsing method for data stored under this key.
252 protected function getOptionFormat($key)
255 self::ROW_LABELS => 'PropertyList',
256 self::FIELD_LABELS => 'PropertyList',
258 if (array_key_exists($key, $propertyFormats)) {
259 return "parse{$propertyFormats[$key]}";
265 * Change the configuration data for this formatter options object.
267 * @param array $configurationData
268 * @return FormatterOptions
270 public function setConfigurationData($configurationData)
272 $this->configurationData = $configurationData;
277 * Change one configuration value for this formatter option.
280 * @param mixed $value
281 * @return FormetterOptions
283 protected function setConfigurationValue($key, $value)
285 $this->configurationData[$key] = $value;
290 * Change one configuration value for this formatter option, but only
291 * if it does not already have a value set.
294 * @param mixed $value
295 * @return FormetterOptions
297 public function setConfigurationDefault($key, $value)
299 if (!array_key_exists($key, $this->configurationData)) {
300 return $this->setConfigurationValue($key, $value);
306 * Return a reference to the configuration data for this object.
310 public function getConfigurationData()
312 return $this->configurationData;
316 * Set all of the options that were specified by the user for this request.
318 * @param array $options
319 * @return FormatterOptions
321 public function setOptions($options)
323 $this->options = $options;
328 * Change one option value specified by the user for this request.
331 * @param mixed $value
332 * @return FormatterOptions
334 public function setOption($key, $value)
336 $this->options[$key] = $value;
341 * Return a reference to the user-specified options for this request.
345 public function getOptions()
347 return $this->options;
351 * Provide a Symfony Console InputInterface containing the user-specified
352 * options for this request.
354 * @param InputInterface $input
357 public function setInput(InputInterface $input)
359 $this->input = $input;
363 * Return all of the options from the provided $defaults array that
364 * exist in our InputInterface object.
366 * @param array $defaults
369 public function getInputOptions($defaults)
371 if (!isset($this->input)) {
375 foreach ($defaults as $key => $value) {
376 if ($this->input->hasOption($key)) {
377 $result = $this->input->getOption($key);
378 if (isset($result)) {
379 $options[$key] = $this->input->getOption($key);