|
1
|
|
|
<?php |
|
2
|
|
|
namespace Consolidation\OutputFormatters\Formatters; |
|
3
|
|
|
|
|
4
|
|
|
use Consolidation\OutputFormatters\FormatterInterface; |
|
5
|
|
|
use Consolidation\OutputFormatters\ValidationInterface; |
|
6
|
|
|
use Consolidation\OutputFormatters\OverrideOptionsInterface; |
|
7
|
|
|
use Consolidation\OutputFormatters\FormatterOptions; |
|
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
|
1 |
|
{ |
|
21
|
|
|
/** |
|
22
|
1 |
|
* @inheritdoc |
|
23
|
1 |
|
*/ |
|
24
|
1 |
|
public function write(OutputInterface $output, $data, FormatterOptions $options) |
|
25
|
1 |
|
{ |
|
26
|
|
|
if (is_string($data)) { |
|
27
|
|
|
return $output->writeln($data); |
|
28
|
|
|
} |
|
29
|
|
|
return $this->reduceToSigleFieldAndWrite($output, $data, $options); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @inheritdoc |
|
34
|
|
|
*/ |
|
35
|
|
|
public function overrideOptions($structuredOutput, FormatterOptions $options) |
|
36
|
|
|
{ |
|
37
|
|
|
$defaultField = $options->get(FormatterOptions::SINGLE_FIELD_DEFAULT, [], []); |
|
|
|
|
|
|
38
|
|
|
return $options->override( |
|
39
|
|
|
[ |
|
40
|
|
|
FormatterOptions::FIELDS => $defaultField, |
|
41
|
|
|
FormatterOptions::INCLUDE_FIELD_LABELS => false, |
|
42
|
|
|
] |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* If the data provided to a 'string' formatter is a table, then try |
|
48
|
|
|
* to emit it as a TSV value. |
|
49
|
|
|
* |
|
50
|
|
|
* @param OutputInterface $output |
|
51
|
|
|
* @param mixed $data |
|
52
|
|
|
* @param FormatterOptions $options |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function reduceToSigleFieldAndWrite(OutputInterface $output, $data, FormatterOptions $options) |
|
55
|
|
|
{ |
|
56
|
|
|
$alternateFormatter = new TsvFormatter(); |
|
57
|
|
|
try { |
|
58
|
|
|
$data = $alternateFormatter->validate($data); |
|
59
|
|
|
$alternateFormatter->write($output, $data, $options); |
|
60
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Do not return any valid data types -- this formatter will never show up |
|
66
|
|
|
* in a list of valid formats. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function validDataTypes() |
|
69
|
|
|
{ |
|
70
|
|
|
return []; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Always validate any data, though. This format will never |
|
75
|
|
|
* cause an error if it is selected for an incompatible data type; at |
|
76
|
|
|
* worse, it simply does not print any data. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function validate($structuredData) |
|
79
|
|
|
{ |
|
80
|
|
|
return $structuredData; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: