Completed
Push — master ( 81dd2f...193118 )
by Greg
8s
created

TableFormatter::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
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