Version 1
[yaffs-website] / vendor / consolidation / output-formatters / src / Formatters / StringFormatter.php
1 <?php
2 namespace Consolidation\OutputFormatters\Formatters;
3
4 use Consolidation\OutputFormatters\Validate\ValidationInterface;
5 use Consolidation\OutputFormatters\Options\OverrideOptionsInterface;
6 use Consolidation\OutputFormatters\Options\FormatterOptions;
7 use Consolidation\OutputFormatters\Validate\ValidDataTypesTrait;
8 use Symfony\Component\Console\Output\OutputInterface;
9 use Consolidation\OutputFormatters\StructuredData\RestructureInterface;
10
11 /**
12  * String formatter
13  *
14  * This formatter is used as the default action when no
15  * particular formatter is requested.  It will print the
16  * provided data only if it is a string; if any other
17  * type is given, then nothing is printed.
18  */
19 class StringFormatter implements FormatterInterface, ValidationInterface, OverrideOptionsInterface
20 {
21     /**
22      * All data types are acceptable.
23      */
24     public function isValidDataType(\ReflectionClass $dataType)
25     {
26         return true;
27     }
28
29     /**
30      * @inheritdoc
31      */
32     public function write(OutputInterface $output, $data, FormatterOptions $options)
33     {
34         if (is_string($data)) {
35             return $output->writeln($data);
36         }
37         return $this->reduceToSigleFieldAndWrite($output, $data, $options);
38     }
39
40     /**
41      * @inheritdoc
42      */
43     public function overrideOptions($structuredOutput, FormatterOptions $options)
44     {
45         $defaultField = $options->get(FormatterOptions::DEFAULT_STRING_FIELD, [], '');
46         $userFields = $options->get(FormatterOptions::FIELDS, [FormatterOptions::FIELDS => $options->get(FormatterOptions::FIELD)]);
47         $optionsOverride = $options->override([]);
48         if (empty($userFields) && !empty($defaultField)) {
49             $optionsOverride->setOption(FormatterOptions::FIELDS, $defaultField);
50         }
51         return $optionsOverride;
52     }
53
54     /**
55      * If the data provided to a 'string' formatter is a table, then try
56      * to emit it as a TSV value.
57      *
58      * @param OutputInterface $output
59      * @param mixed $data
60      * @param FormatterOptions $options
61      */
62     protected function reduceToSigleFieldAndWrite(OutputInterface $output, $data, FormatterOptions $options)
63     {
64         $alternateFormatter = new TsvFormatter();
65         try {
66             $data = $alternateFormatter->validate($data);
67             $alternateFormatter->write($output, $data, $options);
68         } catch (\Exception $e) {
69         }
70     }
71
72     /**
73      * Always validate any data, though. This format will never
74      * cause an error if it is selected for an incompatible data type; at
75      * worse, it simply does not print any data.
76      */
77     public function validate($structuredData)
78     {
79         return $structuredData;
80     }
81 }