Completed
Push — master ( 00691e...418f89 )
by Greg
02:26
created

createTableTransformation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 2
1
<?php
2
namespace Consolidation\OutputFormatters\StructuredData;
3
4
use Consolidation\OutputFormatters\StructuredData\RestructureInterface;
5
use Consolidation\OutputFormatters\Options\FormatterOptions;
6
use Consolidation\OutputFormatters\StructuredData\ListDataInterface;
7
use Consolidation\OutputFormatters\Transformations\ReorderFields;
8
use Consolidation\OutputFormatters\Transformations\TableTransformation;
9
10
/**
11
 * Holds an array where each element of the array is one row,
12
 * and each row contains an associative array where the keys
13
 * are the field names, and the values are the field data.
14
 *
15
 * It is presumed that every row contains the same keys.
16
 */
17
abstract class AbstractStructuredList extends \ArrayObject implements RestructureInterface, ListDataInterface
18
{
19
    protected $data;
20
21
    public function __construct($data)
22
    {
23
        parent::__construct($data);
24
    }
25
26
    abstract public function restructure(FormatterOptions $options);
27
28
    abstract public function getListData();
29
30
    protected function createTableTransformation($data, $options)
31
    {
32
        $defaults = $this->defaultOptions();
33
34
        $reorderer = new ReorderFields();
35
        $fieldLabels = $reorderer->reorder(
36
            $this->getFields($options, $defaults),
37
            $options->get(FormatterOptions::FIELD_LABELS, $defaults),
38
            $data
39
        );
40
41
        $tableTransformer = new TableTransformation($data, $fieldLabels, $options->get(FormatterOptions::ROW_LABELS, $defaults));
42
        if ($options->get(FormatterOptions::LIST_ORIENTATION, $defaults)) {
43
            $tableTransformer->setLayout(TableTransformation::LIST_LAYOUT);
44
        }
45
46
        return $tableTransformer;
47
    }
48
49
    protected function getFields($options, $defaults)
50
    {
51
        $fieldShortcut = $options->get(FormatterOptions::FIELD);
52
        if (!empty($fieldShortcut)) {
53
            return [$fieldShortcut];
54
        }
55
        return $options->get(FormatterOptions::FIELDS, $defaults);
56
    }
57
58
    /**
59
     * A structured list may provide its own set of default options. These
60
     * will be used in place of the command's default options (from the
61
     * annotations) in instances where the user does not provide the options
62
     * explicitly (on the commandline) or implicitly (via a configuration file).
63
     *
64
     * @return array
65
     */
66
    protected function defaultOptions()
67
    {
68
        return [
69
            FormatterOptions::FIELDS => [],
70
            FormatterOptions::FIELD_LABELS => [],
71
            FormatterOptions::ROW_LABELS => [],
72
            FormatterOptions::DEFAULT_FIELDS => [],
73
        ];
74
    }
75
}
76