2 namespace Consolidation\OutputFormatters\Formatters;
4 use Symfony\Component\Console\Output\OutputInterface;
5 use Symfony\Component\Console\Helper\Table;
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;
16 * Display sections of data.
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.
23 class SectionsFormatter implements FormatterInterface, ValidDataTypesInterface, RenderDataInterface
25 use ValidDataTypesTrait;
26 use RenderTableDataTrait;
28 public function validDataTypes()
32 new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields')
39 public function validate($structuredData)
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(
48 $this->validDataTypes()
51 return $structuredData;
57 public function write(OutputInterface $output, $tableTransformer, FormatterOptions $options)
59 $table = new Table($output);
60 $table->setStyle('compact');
61 foreach ($tableTransformer as $rowid => $row) {
62 $rowLabel = $tableTransformer->getRowLabel($rowid);
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));