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