|
1
|
|
|
<?php |
|
2
|
|
|
namespace Consolidation\OutputFormatters\Formatters; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
5
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
6
|
|
|
use Symfony\Component\Console\Helper\TableStyle; |
|
7
|
|
|
|
|
8
|
|
|
use Consolidation\OutputFormatters\Validate\ValidDataTypesInterface; |
|
9
|
|
|
use Consolidation\OutputFormatters\Options\FormatterOptions; |
|
10
|
|
|
use Consolidation\OutputFormatters\Validate\ValidDataTypesTrait; |
|
11
|
|
|
use Consolidation\OutputFormatters\StructuredData\TableDataInterface; |
|
12
|
|
|
use Consolidation\OutputFormatters\Transformations\ReorderFields; |
|
13
|
|
|
use Consolidation\OutputFormatters\Exception\IncompatibleDataException; |
|
14
|
|
|
use Consolidation\OutputFormatters\Transformations\WordWrapper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Display a table of data with the Symfony Table class. |
|
18
|
|
|
* |
|
19
|
|
|
* This formatter takes data of either the RowsOfFields or |
|
20
|
|
|
* PropertyList data type. Tables can be rendered with the |
|
21
|
|
|
* rows running either vertically (the normal orientation) or |
|
22
|
|
|
* horizontally. By default, associative lists will be displayed |
|
23
|
|
|
* as two columns, with the key in the first column and the |
|
24
|
|
|
* value in the second column. |
|
25
|
|
|
*/ |
|
26
|
|
|
class TableFormatter implements FormatterInterface, ValidDataTypesInterface, RenderDataInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use ValidDataTypesTrait; |
|
29
|
|
|
use RenderTableDataTrait; |
|
30
|
|
|
|
|
31
|
|
|
protected $fieldLabels; |
|
32
|
9 |
|
protected $defaultFields; |
|
33
|
|
|
|
|
34
|
9 |
|
public function __construct() |
|
35
|
9 |
|
{ |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function validDataTypes() |
|
39
|
|
|
{ |
|
40
|
9 |
|
return |
|
41
|
|
|
[ |
|
42
|
9 |
|
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields'), |
|
43
|
2 |
|
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\PropertyList') |
|
44
|
2 |
|
]; |
|
45
|
9 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritdoc |
|
49
|
|
|
*/ |
|
50
|
9 |
|
public function validate($structuredData) |
|
51
|
|
|
{ |
|
52
|
|
|
// If the provided data was of class RowsOfFields |
|
53
|
|
|
// or PropertyList, it will be converted into |
|
54
|
|
|
// a TableTransformation object by the restructure call. |
|
55
|
9 |
|
if (!$structuredData instanceof TableDataInterface) { |
|
56
|
2 |
|
throw new IncompatibleDataException( |
|
57
|
2 |
|
$this, |
|
58
|
2 |
|
$structuredData, |
|
59
|
|
|
$this->validDataTypes() |
|
60
|
2 |
|
); |
|
61
|
2 |
|
} |
|
62
|
|
|
return $structuredData; |
|
63
|
2 |
|
} |
|
64
|
|
|
|
|
65
|
7 |
|
/** |
|
66
|
|
|
* @inheritdoc |
|
67
|
|
|
*/ |
|
68
|
|
|
public function write(OutputInterface $output, $tableTransformer, FormatterOptions $options) |
|
69
|
|
|
{ |
|
70
|
|
|
$defaults = [ |
|
71
|
|
|
FormatterOptions::TABLE_STYLE => 'consolidation', |
|
72
|
7 |
|
FormatterOptions::INCLUDE_FIELD_LABELS => true, |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
7 |
|
$table = new Table($output); |
|
76
|
7 |
|
|
|
77
|
|
|
static::addCustomTableStyles($table); |
|
78
|
|
|
|
|
79
|
7 |
|
$table->setStyle($options->get(FormatterOptions::TABLE_STYLE, $defaults)); |
|
80
|
7 |
|
$headers = $tableTransformer->getHeaders(); |
|
81
|
7 |
|
$isList = $tableTransformer->isList(); |
|
82
|
7 |
|
$includeHeaders = $options->get(FormatterOptions::INCLUDE_FIELD_LABELS, $defaults); |
|
83
|
7 |
|
if ($includeHeaders && !$isList && !empty($headers)) { |
|
84
|
7 |
|
$table->setHeaders($headers); |
|
85
|
4 |
|
} |
|
86
|
4 |
|
$data = $tableTransformer->getTableData($includeHeaders && $isList); |
|
87
|
7 |
|
$data = $this->wrap($data, $options); |
|
88
|
7 |
|
$table->setRows($data); |
|
89
|
7 |
|
$table->render(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Wrap the table data |
|
94
|
|
|
* @param array $data |
|
95
|
|
|
* @param FormatterOptions $options |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function wrap($data, FormatterOptions $options) |
|
99
|
|
|
{ |
|
100
|
|
|
$wrapper = new WordWrapper($options->get(FormatterOptions::TERMINAL_WIDTH)); |
|
101
|
|
|
return $wrapper->wrap($data); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Add our custom table style(s) to the table. |
|
106
|
|
|
*/ |
|
107
|
|
|
protected static function addCustomTableStyles($table) |
|
108
|
|
|
{ |
|
109
|
|
|
// The 'consolidation' style is the same as the 'symfony-style-guide' |
|
110
|
|
|
// style, except it maintains the colored headers used in 'default'. |
|
111
|
|
|
$consolidationStyle = new TableStyle(); |
|
112
|
|
|
$consolidationStyle |
|
113
|
|
|
->setHorizontalBorderChar('-') |
|
114
|
|
|
->setVerticalBorderChar(' ') |
|
115
|
|
|
->setCrossingChar(' ') |
|
116
|
|
|
; |
|
117
|
|
|
$table->setStyleDefinition('consolidation', $consolidationStyle); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|