|
1
|
|
|
<?php |
|
2
|
|
|
namespace Consolidation\OutputFormatters\Formatters; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
5
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
6
|
|
|
|
|
7
|
|
|
use Consolidation\OutputFormatters\FormatterInterface; |
|
8
|
|
|
use Consolidation\OutputFormatters\ValidationInterface; |
|
9
|
|
|
use Consolidation\OutputFormatters\FormatterOptions; |
|
10
|
|
|
use Consolidation\OutputFormatters\StructuredData\TableDataInterface; |
|
11
|
|
|
use Consolidation\OutputFormatters\Transformations\ReorderFields; |
|
12
|
|
|
use Consolidation\OutputFormatters\Exception\IncompatibleDataException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Display a table of data with the Symfony Table class. |
|
16
|
|
|
* |
|
17
|
|
|
* This formatter takes data of either the RowsOfFields or |
|
18
|
|
|
* AssociativeList data type. Tables can be rendered with the |
|
19
|
|
|
* rows running either vertically (the normal orientation) or |
|
20
|
|
|
* horizontally. By default, associative lists will be displayed |
|
21
|
|
|
* as two columns, with the key in the first column and the |
|
22
|
|
|
* value in the second column. |
|
23
|
|
|
*/ |
|
24
|
|
|
class TableFormatter implements FormatterInterface, ValidationInterface, RenderDataInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use RenderTableDataTrait; |
|
27
|
|
|
|
|
28
|
|
|
protected $fieldLabels; |
|
29
|
|
|
protected $defaultFields; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct() |
|
32
|
9 |
|
{ |
|
33
|
|
|
} |
|
34
|
9 |
|
|
|
35
|
9 |
|
/** |
|
36
|
|
|
* @inheritdoc |
|
37
|
|
|
*/ |
|
38
|
|
|
public function validate($structuredData) |
|
39
|
|
|
{ |
|
40
|
9 |
|
// If the provided data was of class RowsOfFields |
|
41
|
|
|
// or AssociativeList, it will be converted into |
|
42
|
9 |
|
// a TableTransformation object by the restructure call. |
|
43
|
2 |
|
if (!$structuredData instanceof TableDataInterface) { |
|
44
|
2 |
|
throw new IncompatibleDataException( |
|
45
|
9 |
|
$this, |
|
46
|
|
|
$structuredData, |
|
47
|
|
|
[ |
|
48
|
|
|
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields'), |
|
49
|
|
|
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\AssociativeList'), |
|
50
|
9 |
|
] |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
return $structuredData; |
|
54
|
|
|
} |
|
55
|
9 |
|
|
|
56
|
2 |
|
|
|
57
|
2 |
|
/** |
|
58
|
2 |
|
* @inheritdoc |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function write(OutputInterface $output, $tableTransformer, FormatterOptions $options) |
|
61
|
2 |
|
{ |
|
62
|
|
|
$defaults = [ |
|
63
|
2 |
|
'table-style' => 'default', |
|
64
|
|
|
'include-field-labels' => true, |
|
65
|
7 |
|
]; |
|
66
|
|
|
|
|
67
|
|
|
$table = new Table($output); |
|
68
|
|
|
$table->setStyle($options->get('table-style', $defaults)); |
|
69
|
|
|
$headers = $tableTransformer->getHeaders(); |
|
70
|
|
|
$isList = $tableTransformer->isList(); |
|
71
|
|
|
$includeHeaders = $options->get('include-field-labels', $defaults); |
|
72
|
7 |
|
if ($includeHeaders && !$isList && !empty($headers)) { |
|
73
|
|
|
$table->setHeaders($headers); |
|
74
|
|
|
} |
|
75
|
7 |
|
$table->setRows($tableTransformer->getTableData($includeHeaders && $isList)); |
|
76
|
7 |
|
$table->render(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|