1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\OutputFormatters; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Manage a collection of formatters; return one on request. |
8
|
|
|
*/ |
9
|
|
|
class FormatterManager |
10
|
|
|
{ |
11
|
|
|
protected $formatters = []; |
12
|
|
|
|
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
$this->formatters = [ |
16
|
|
|
'yaml' => '\Consolidation\OutputFormatters\Formatters\YamlFormatter', |
17
|
|
|
'json' => '\Consolidation\OutputFormatters\Formatters\JsonFormatter', |
18
|
|
|
'print-r' => '\Consolidation\OutputFormatters\Formatters\PrintRFormatter', |
19
|
|
|
'var_export' => '\Consolidation\OutputFormatters\Formatters\VarExportFormatter', |
20
|
|
|
'list' => '\Consolidation\OutputFormatters\Formatters\ListFormatter', |
21
|
|
|
'csv' => '\Consolidation\OutputFormatters\Formatters\CsvFormatter', |
22
|
|
|
'table' => '\Consolidation\OutputFormatters\Formatters\TableFormatter', |
23
|
|
|
]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function write(OutputInterface $output, $format, $structuredOutput, $annotationData = [], $options = []) |
27
|
|
|
{ |
28
|
|
|
$formatter = $this->getFormatter($format, $annotationData); |
29
|
|
|
|
30
|
|
|
// Restructure the output data (e.g. select fields to display, etc.). |
31
|
|
|
$structuredOutput = $this->restructureData($structuredOutput, $annotationData, $options); |
32
|
|
|
|
33
|
|
|
// Make sure that the provided data is in the correct format for the selected formatter. |
34
|
|
|
$structuredOutput = $this->validateData($formatter, $structuredOutput); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$formatter->write($output, $structuredOutput, $options); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getFormatter($format, $configurationData = []) |
40
|
|
|
{ |
41
|
|
|
if (is_string($format) && array_key_exists($format, $this->formatters)) { |
42
|
|
|
$formatter = new $this->formatters[$format]; |
43
|
|
|
if ($formatter instanceof ConfigureInterface) { |
44
|
|
|
$formatter->configure($configurationData); |
45
|
|
|
} |
46
|
|
|
return $formatter; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function validateData(FormatterInterface $formatter, $structuredOutput) |
51
|
|
|
{ |
52
|
|
|
// If the formatter implements ValidationInterface, then let it |
53
|
|
|
// test the data and throw or return an error |
54
|
|
|
if ($formatter instanceof ValidationInterface) { |
55
|
|
|
return $formatter->validate($structuredOutput); |
56
|
|
|
} |
57
|
|
|
// If the formatter does not implement ValidationInterface, then |
58
|
|
|
// it will never be passed an ArrayObject; we will always give |
59
|
|
|
// it a simple array. |
60
|
|
|
if ($structuredOutput instanceof \ArrayObject) { |
61
|
|
|
return $structuredOutput->getArrayCopy(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $structuredOutput; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function restructureData($structuredOutput, $configurationData, $options) |
68
|
|
|
{ |
69
|
|
|
if ($structuredOutput instanceof RestructureInterface) { |
70
|
|
|
return $structuredOutput->restructure($configurationData, $options); |
71
|
|
|
} |
72
|
|
|
return $structuredOutput; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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: