Completed
Pull Request — master (#17)
by Greg
02:45
created

SectionsFormatter::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 9.4285
cc 2
eloc 13
nc 2
nop 3
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\StructuredData\TableDataInterface;
11
use Consolidation\OutputFormatters\Transformations\ReorderFields;
12
use Consolidation\OutputFormatters\Exception\IncompatibleDataException;
13
use Consolidation\OutputFormatters\StructuredData\AssociativeList;
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, ValidationInterface, RenderDataInterface
24
{
25
    use RenderTableDataTrait;
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function validate($structuredData)
31
    {
32
        // If the provided data was of class RowsOfFields
33
        // or AssociativeList, it will be converted into
34
        // a TableTransformation object by the restructure call.
35
        if (!$structuredData instanceof TableDataInterface) {
36
            throw new IncompatibleDataException(
37
                $this,
38
                $structuredData,
39
                new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields')
40
            );
41
        }
42
        return $structuredData;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function write(OutputInterface $output, $tableTransformer, $options = [])
49
    {
50
        $options += [
51
            'table-style' => $this->tableStyle,
0 ignored issues
show
Bug introduced by
The property tableStyle does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
            'include-field-labels' => true,
53
        ];
54
55
        $table = new Table($output);
56
        $table->setStyle('compact');
57
        foreach ($tableTransformer as $rowid => $row) {
58
            $output->writeln('');
59
            $output->writeln($rowid); // TODO: convert to a label
60
            $sectionData = new AssociativeList($row);
61
            $sectionTableTransformer = $sectionData->restructure([], $options);
62
            $table->setRows($sectionTableTransformer->getTableData(true));
63
            $table->render();
64
        }
65
    }
66
}
67