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\ConfigureInterface; |
9
|
|
|
use Consolidation\OutputFormatters\ValidationInterface; |
10
|
|
|
use Consolidation\OutputFormatters\Transformations\TableTransformation; |
11
|
|
|
use Consolidation\OutputFormatters\Transformations\PropertyParser; |
12
|
|
|
use Consolidation\OutputFormatters\Transformations\ReorderFields; |
13
|
|
|
use Consolidation\OutputFormatters\Exception\IncompatibleDataException; |
14
|
|
|
|
15
|
|
|
class TableFormatter implements FormatterInterface, ConfigureInterface, ValidationInterface |
16
|
|
|
{ |
17
|
|
|
protected $fieldLabels; |
18
|
|
|
protected $defaultFields; |
19
|
|
|
protected $tableStyle; |
20
|
|
|
|
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->tableStyle = 'default'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
public function configure($configurationData) |
30
|
|
|
{ |
31
|
|
|
if (isset($configurationData['table-style'])) { |
32
|
|
|
$this->tableStyle = $configurationData['table-style']; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function validate($structuredData) |
37
|
|
|
{ |
38
|
|
|
// If the provided data was of class RowsOfFields |
39
|
|
|
// or AssociativeList, it will be converted into |
40
|
|
|
// a TableTransformation object. |
41
|
|
|
if (!$structuredData instanceof TableTransformation) { |
42
|
|
|
throw new IncompatibleDataException( |
43
|
|
|
$this, |
44
|
|
|
$structuredData, |
45
|
|
|
[ |
46
|
|
|
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields'), |
47
|
|
|
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\AssociativeList'), |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
return $structuredData; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
public function write(OutputInterface $output, $tableTransformer, $options = []) |
58
|
|
|
{ |
59
|
|
|
$options += [ |
60
|
|
|
'table-style' => $this->tableStyle, |
61
|
|
|
'include-field-labels' => true, |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
$table = new Table($output); |
65
|
|
|
$table->setStyle($options['table-style']); |
66
|
|
|
$headers = $tableTransformer->getHeaders(); |
67
|
|
|
$isList = $tableTransformer->isList(); |
68
|
|
|
$includeHeaders = $options['include-field-labels']; |
69
|
|
|
if ($includeHeaders && !$isList && !empty($headers)) { |
70
|
|
|
$table->setHeaders($headers); |
71
|
|
|
} |
72
|
|
|
$table->setRows($tableTransformer->getData($includeHeaders && $isList)); |
73
|
|
|
$table->render(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|