Completed
Pull Request — master (#69)
by Greg
05:13
created

AbstractStructuredList::getFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
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\TableTransformation;
8
9
/**
10
 * Holds an array where each element of the array is one row,
11
 * and each row contains an associative array where the keys
12
 * are the field names, and the values are the field data.
13
 *
14
 * It is presumed that every row contains the same keys.
15
 */
16
abstract class AbstractStructuredList extends AbstractListData implements RestructureInterface, RenderCellCollectionInterface
17
{
18
    use RenderCellCollectionTrait;
19
20
    public function __construct($data)
21
    {
22
        parent::__construct($data);
23
    }
24
25
    abstract public function restructure(FormatterOptions $options);
26
27
    protected function createTableTransformation($data, $options)
28
    {
29
        $defaults = $this->defaultOptions();
30
        $fieldLabels = $this->getReorderedFieldLabels($data, $options, $defaults);
31
32
        $tableTransformer = $this->instantiateTableTransformation($data, $fieldLabels, $options->get(FormatterOptions::ROW_LABELS, $defaults));
33
        if ($options->get(FormatterOptions::LIST_ORIENTATION, $defaults)) {
34
            $tableTransformer->setLayout(TableTransformation::LIST_LAYOUT);
35
        }
36
37
        return $tableTransformer;
38
    }
39
40
    protected function instantiateTableTransformation($data, $fieldLabels, $rowLabels)
41
    {
42
        return new TableTransformation($data, $fieldLabels, $rowLabels);
43
    }
44
45
    protected function defaultOptions()
46
    {
47
        return [
48
            FormatterOptions::ROW_LABELS => [],
49
            FormatterOptions::DEFAULT_FIELDS => [],
50
        ] + parent::defaultOptions();
51
    }
52
}
53