|
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\Validate\ValidDataTypesInterface; |
|
8
|
|
|
use Consolidation\OutputFormatters\Options\FormatterOptions; |
|
9
|
|
|
use Consolidation\OutputFormatters\Validate\ValidDataTypesTrait; |
|
10
|
|
|
use Consolidation\OutputFormatters\StructuredData\TableDataInterface; |
|
11
|
|
|
use Consolidation\OutputFormatters\Transformations\ReorderFields; |
|
12
|
|
|
use Consolidation\OutputFormatters\Exception\IncompatibleDataException; |
|
13
|
|
|
use Consolidation\OutputFormatters\StructuredData\PropertyList; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Display sections of data. |
|
17
|
|
|
* |
|
18
|
|
|
* This formatter takes data in the RowsOfFields data type. |
|
19
|
|
|
* Each row represents one section; the data in each section |
|
20
|
|
|
* is rendered in two columns, with the key in the first column |
|
21
|
|
|
* and the value in the second column. |
|
22
|
|
|
*/ |
|
23
|
|
|
class SectionsFormatter implements FormatterInterface, ValidDataTypesInterface, RenderDataInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use ValidDataTypesTrait; |
|
26
|
|
|
use RenderTableDataTrait; |
|
27
|
|
|
|
|
28
|
|
|
public function validDataTypes() |
|
29
|
|
|
{ |
|
30
|
|
|
return |
|
31
|
|
|
[ |
|
32
|
|
|
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields') |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritdoc |
|
38
|
|
|
*/ |
|
39
|
|
|
public function validate($structuredData) |
|
40
|
|
|
{ |
|
41
|
|
|
// If the provided data was of class RowsOfFields |
|
42
|
|
|
// or PropertyList, it will be converted into |
|
43
|
|
|
// a TableTransformation object by the restructure call. |
|
44
|
|
|
if (!$structuredData instanceof TableDataInterface) { |
|
45
|
|
|
throw new IncompatibleDataException( |
|
46
|
|
|
$this, |
|
47
|
|
|
$structuredData, |
|
48
|
|
|
$this->validDataTypes() |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
return $structuredData; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public function write(OutputInterface $output, $tableTransformer, FormatterOptions $options) |
|
58
|
|
|
{ |
|
59
|
|
|
$table = new Table($output); |
|
60
|
|
|
$table->setStyle('compact'); |
|
61
|
|
|
foreach ($tableTransformer as $rowid => $row) { |
|
62
|
|
|
$rowLabel = $tableTransformer->getRowLabel($rowid); |
|
63
|
|
|
$output->writeln(''); |
|
64
|
|
|
$output->writeln($rowLabel); |
|
65
|
|
|
$sectionData = new PropertyList($row); |
|
66
|
|
|
$sectionOptions = new FormatterOptions([], $options->getOptions()); |
|
67
|
|
|
$sectionTableTransformer = $sectionData->restructure($sectionOptions); |
|
68
|
|
|
$table->setRows($sectionTableTransformer->getTableData(true)); |
|
69
|
|
|
$table->render(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|