Completed
Push — master ( 3c952e...cb21a4 )
by Greg
11s
created

AbstractStructuredList   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
c 2
b 0
f 1
lcom 0
cbo 2
dl 0
loc 39
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
restructure() 0 1 ?
getListData() 0 1 ?
A createTableTransformation() 0 14 2
A defaultOptions() 0 10 1
1
<?php
2
namespace Consolidation\OutputFormatters\StructuredData;
3
4
use Consolidation\OutputFormatters\RestructureInterface;
5
use Consolidation\OutputFormatters\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($options->get('fields', $defaults), $options->get('field-labels', $defaults), $data);
36
37
        $tableTransformer = new TableTransformation($data, $fieldLabels, $options->get('row-labels', $defaults));
38
        if ($options->get('list-orientation', $defaults)) {
39
            $tableTransformer->setLayout(TableTransformation::LIST_LAYOUT);
40
        }
41
42
        return $tableTransformer;
43
    }
44
45
    protected function defaultOptions()
46
    {
47
        return [
48
            'list-orientation' => false,
49
            'fields' => [],
50
            'field-labels' => [],
51
            'row-labels' => [],
52
            'default-fields' => [],
53
        ];
54
    }
55
}
56