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\ConfigurationAwareInterface; |
9
|
|
|
use Consolidation\OutputFormatters\Transformations\TableTransformation; |
10
|
|
|
use Consolidation\OutputFormatters\Transformations\PropertyParser; |
11
|
|
|
use Consolidation\OutputFormatters\Transformations\ReorderFields; |
12
|
|
|
|
13
|
|
|
class TableFormatter implements FormatterInterface, ConfigurationAwareInterface |
14
|
|
|
{ |
15
|
|
|
protected $fieldLabels; |
16
|
|
|
protected $defaultFields; |
17
|
|
|
protected $tableStyle; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @inheritdoc |
21
|
|
|
*/ |
22
|
|
|
public function configure($configurationData) |
23
|
|
|
{ |
24
|
|
|
$configurationData += [ |
25
|
|
|
'field-labels' => [], |
26
|
|
|
'default-fields' => [], |
27
|
|
|
'table-style' => 'default', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
$this->fieldLabels = PropertyParser::parse($configurationData['field-labels']); |
31
|
|
|
$this->defaultFields = PropertyParser::parse($configurationData['default-fields']); |
32
|
|
|
$this->tableStyle = $configurationData['table-style']; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
public function write($data, $options, OutputInterface $output) |
39
|
|
|
{ |
40
|
|
|
$options += [ |
41
|
|
|
'table-style' => $this->tableStyle, |
42
|
|
|
'fields' => $this->defaultFields, |
43
|
|
|
'include-field-labels' => true, |
44
|
|
|
]; |
45
|
|
|
$reorderer = new ReorderFields(); |
46
|
|
|
$fieldLabels = $reorderer->reorder($options['fields'], $this->fieldLabels, $data); |
47
|
|
|
$tableTransformer = new TableTransformation($data, $fieldLabels, $options); |
48
|
|
|
|
49
|
|
|
$table = new Table($output); |
50
|
|
|
$table->setStyle($options['table-style']); |
51
|
|
|
if ($options['include-field-labels']) { |
52
|
|
|
$table->setHeaders($tableTransformer->getHeaders()); |
53
|
|
|
} |
54
|
|
|
$table->setRows($tableTransformer->getData()); |
55
|
|
|
$table->render(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|