Completed
Push — master ( 5422a3...00691e )
by Greg
02:23
created

AbstractStructuredList::defaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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
            $options->get(
37
                FormatterOptions::FIELDS,
38
                [
39
                    FormatterOptions::FIELDS => [$options->get(FormatterOptions::FIELD)]
40
                ] + $defaults
41
            ),
42
            $options->get(
43
                FormatterOptions::FIELD_LABELS, $defaults
44
            ),
45
            $data
46
        );
47
48
        $tableTransformer = new TableTransformation($data, $fieldLabels, $options->get(FormatterOptions::ROW_LABELS, $defaults));
49
        if ($options->get(FormatterOptions::LIST_ORIENTATION, $defaults)) {
50
            $tableTransformer->setLayout(TableTransformation::LIST_LAYOUT);
51
        }
52
53
        return $tableTransformer;
54
    }
55
56
    /**
57
     * A structured list may provide its own set of default options. These
58
     * will be used in place of the command's default options (from the
59
     * annotations) in instances where the user does not provide the options
60
     * explicitly (on the commandline) or implicitly (via a configuration file).
61
     *
62
     * @return array
63
     */
64
    protected function defaultOptions()
65
    {
66
        return [
67
            FormatterOptions::FIELDS => [],
68
            FormatterOptions::FIELD_LABELS => [],
69
            FormatterOptions::ROW_LABELS => [],
70
            FormatterOptions::DEFAULT_FIELDS => [],
71
        ];
72
    }
73
}
74