Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / consolidation / output-formatters / src / Options / FormatterOptions.php
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     const LIST_DELIMITER = 'list-delimiter';
50     const TERMINAL_WIDTH = 'width';
51     const METADATA_TEMPLATE = 'metadata-template';
52
53     /**
54      * Create a new FormatterOptions with the configuration data and the
55      * user-specified options for this request.
56      *
57      * @see FormatterOptions::setInput()
58      * @param array $configurationData
59      * @param array $options
60      */
61     public function __construct($configurationData = [], $options = [])
62     {
63         $this->configurationData = $configurationData;
64         $this->options = $options;
65     }
66
67     /**
68      * Create a new FormatterOptions object with new configuration data (provided),
69      * and the same options data as this instance.
70      *
71      * @param array $configurationData
72      * @return FormatterOptions
73      */
74     public function override($configurationData)
75     {
76         $override = new self();
77         $override
78             ->setConfigurationData($configurationData + $this->getConfigurationData())
79             ->setOptions($this->getOptions());
80         return $override;
81     }
82
83     public function setTableStyle($style)
84     {
85         return $this->setConfigurationValue(self::TABLE_STYLE, $style);
86     }
87
88     public function setDelimiter($delimiter)
89     {
90         return $this->setConfigurationValue(self::DELIMITER, $delimiter);
91     }
92
93     public function setListDelimiter($listDelimiter)
94     {
95         return $this->setConfigurationValue(self::LIST_DELIMITER, $listDelimiter);
96     }
97
98
99
100     public function setIncludeFieldLables($includFieldLables)
101     {
102         return $this->setConfigurationValue(self::INCLUDE_FIELD_LABELS, $includFieldLables);
103     }
104
105     public function setListOrientation($listOrientation)
106     {
107         return $this->setConfigurationValue(self::LIST_ORIENTATION, $listOrientation);
108     }
109
110     public function setRowLabels($rowLabels)
111     {
112         return $this->setConfigurationValue(self::ROW_LABELS, $rowLabels);
113     }
114
115     public function setDefaultFields($fields)
116     {
117         return $this->setConfigurationValue(self::DEFAULT_FIELDS, $fields);
118     }
119
120     public function setFieldLabels($fieldLabels)
121     {
122         return $this->setConfigurationValue(self::FIELD_LABELS, $fieldLabels);
123     }
124
125     public function setDefaultStringField($defaultStringField)
126     {
127         return $this->setConfigurationValue(self::DEFAULT_STRING_FIELD, $defaultStringField);
128     }
129
130     public function setWidth($width)
131     {
132         return $this->setConfigurationValue(self::TERMINAL_WIDTH, $width);
133     }
134
135     /**
136      * Get a formatter option
137      *
138      * @param string $key
139      * @param array $defaults
140      * @param mixed $default
141      * @return mixed
142      */
143     public function get($key, $defaults = [], $default = false)
144     {
145         $value = $this->fetch($key, $defaults, $default);
146         return $this->parse($key, $value);
147     }
148
149     /**
150      * Return the XmlSchema to use with --format=xml for data types that support
151      * that.  This is used when an array needs to be converted into xml.
152      *
153      * @return XmlSchema
154      */
155     public function getXmlSchema()
156     {
157         return new XmlSchema();
158     }
159
160     /**
161      * Determine the format that was requested by the caller.
162      *
163      * @param array $defaults
164      * @return string
165      */
166     public function getFormat($defaults = [])
167     {
168         return $this->get(self::FORMAT, [], $this->get(self::DEFAULT_FORMAT, $defaults, ''));
169     }
170
171     /**
172      * Look up a key, and return its raw value.
173      *
174      * @param string $key
175      * @param array $defaults
176      * @param mixed $default
177      * @return mixed
178      */
179     protected function fetch($key, $defaults = [], $default = false)
180     {
181         $defaults = $this->defaultsForKey($key, $defaults, $default);
182         $values = $this->fetchRawValues($defaults);
183         return $values[$key];
184     }
185
186     /**
187      * Reduce provided defaults to the single item identified by '$key',
188      * if it exists, or an empty array otherwise.
189      *
190      * @param string $key
191      * @param array $defaults
192      * @return array
193      */
194     protected function defaultsForKey($key, $defaults, $default = false)
195     {
196         if (array_key_exists($key, $defaults)) {
197             return [$key => $defaults[$key]];
198         }
199         return [$key => $default];
200     }
201
202     /**
203      * Look up all of the items associated with the provided defaults.
204      *
205      * @param array $defaults
206      * @return array
207      */
208     protected function fetchRawValues($defaults = [])
209     {
210         return array_merge(
211             $defaults,
212             $this->getConfigurationData(),
213             $this->getOptions(),
214             $this->getInputOptions($defaults)
215         );
216     }
217
218     /**
219      * Given the raw value for a specific key, do any type conversion
220      * (e.g. from a textual list to an array) needed for the data.
221      *
222      * @param string $key
223      * @param mixed $value
224      * @return mixed
225      */
226     protected function parse($key, $value)
227     {
228         $optionFormat = $this->getOptionFormat($key);
229         if (!empty($optionFormat) && is_string($value)) {
230             return $this->$optionFormat($value);
231         }
232         return $value;
233     }
234
235     /**
236      * Convert from a textual list to an array
237      *
238      * @param string $value
239      * @return array
240      */
241     public function parsePropertyList($value)
242     {
243         return PropertyParser::parse($value);
244     }
245
246     /**
247      * Given a specific key, return the class method name of the
248      * parsing method for data stored under this key.
249      *
250      * @param string $key
251      * @return string
252      */
253     protected function getOptionFormat($key)
254     {
255         $propertyFormats = [
256             self::ROW_LABELS => 'PropertyList',
257             self::FIELD_LABELS => 'PropertyList',
258         ];
259         if (array_key_exists($key, $propertyFormats)) {
260             return "parse{$propertyFormats[$key]}";
261         }
262         return '';
263     }
264
265     /**
266      * Change the configuration data for this formatter options object.
267      *
268      * @param array $configurationData
269      * @return FormatterOptions
270      */
271     public function setConfigurationData($configurationData)
272     {
273         $this->configurationData = $configurationData;
274         return $this;
275     }
276
277     /**
278      * Change one configuration value for this formatter option.
279      *
280      * @param string $key
281      * @param mixed $value
282      * @return FormetterOptions
283      */
284     protected function setConfigurationValue($key, $value)
285     {
286         $this->configurationData[$key] = $value;
287         return $this;
288     }
289
290     /**
291      * Change one configuration value for this formatter option, but only
292      * if it does not already have a value set.
293      *
294      * @param string $key
295      * @param mixed $value
296      * @return FormetterOptions
297      */
298     public function setConfigurationDefault($key, $value)
299     {
300         if (!array_key_exists($key, $this->configurationData)) {
301             return $this->setConfigurationValue($key, $value);
302         }
303         return $this;
304     }
305
306     /**
307      * Return a reference to the configuration data for this object.
308      *
309      * @return array
310      */
311     public function getConfigurationData()
312     {
313         return $this->configurationData;
314     }
315
316     /**
317      * Set all of the options that were specified by the user for this request.
318      *
319      * @param array $options
320      * @return FormatterOptions
321      */
322     public function setOptions($options)
323     {
324         $this->options = $options;
325         return $this;
326     }
327
328     /**
329      * Change one option value specified by the user for this request.
330      *
331      * @param string $key
332      * @param mixed $value
333      * @return FormatterOptions
334      */
335     public function setOption($key, $value)
336     {
337         $this->options[$key] = $value;
338         return $this;
339     }
340
341     /**
342      * Return a reference to the user-specified options for this request.
343      *
344      * @return array
345      */
346     public function getOptions()
347     {
348         return $this->options;
349     }
350
351     /**
352      * Provide a Symfony Console InputInterface containing the user-specified
353      * options for this request.
354      *
355      * @param InputInterface $input
356      * @return type
357      */
358     public function setInput(InputInterface $input)
359     {
360         $this->input = $input;
361     }
362
363     /**
364      * Return all of the options from the provided $defaults array that
365      * exist in our InputInterface object.
366      *
367      * @param array $defaults
368      * @return array
369      */
370     public function getInputOptions($defaults)
371     {
372         if (!isset($this->input)) {
373             return [];
374         }
375         $options = [];
376         foreach ($defaults as $key => $value) {
377             if ($this->input->hasOption($key)) {
378                 $result = $this->input->getOption($key);
379                 if (isset($result)) {
380                     $options[$key] = $this->input->getOption($key);
381                 }
382             }
383         }
384         return $options;
385     }
386 }