Completed
Pull Request — master (#18)
by Greg
02:21
created

RowsOfFields   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 6
c 6
b 0
f 3
lcom 1
cbo 2
dl 0
loc 46
ccs 21
cts 21
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getListData() 0 4 1
A restructure() 0 5 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
class RowsOfFields extends \ArrayObject implements RestructureInterface, ListDataInterface
18
{
19
    protected $data;
20
21 9
    public function __construct($data)
22
    {
23 9
        parent::__construct($data);
24 9
    }
25
26 4
    public function restructure(FormatterOptions $options)
27
    {
28 4
        $data = $this->getArrayCopy();
29 4
        return $this->createTableTransformation($data, $options);
30
    }
31
32 2
    public function getListData()
33
    {
34 2
        return array_keys($this->getArrayCopy());
35
    }
36
37 7
    protected function createTableTransformation($data, $options)
38
    {
39 7
        $defaults = $this->defaultOptions();
40
41 7
        $reorderer = new ReorderFields();
42 7
        $fieldLabels = $reorderer->reorder($options->get('fields', $defaults), $options->get('field-labels', $defaults), $data);
43
44 7
        $tableTransformer = new TableTransformation($data, $fieldLabels, $options->get('row-labels', $defaults));
45
        if ($options->get('list-orientation', $defaults)) {
46 7
            $tableTransformer->setLayout(TableTransformation::LIST_LAYOUT);
47
        }
48
49 7
        return $tableTransformer;
50
    }
51 7
52
    protected function defaultOptions()
53 7
    {
54 7
        return [
55
            'list-orientation' => false,
56 7
            'fields' => [],
57
            'field-labels' => [],
58
            'row-labels' => [],
59 7
            'default-fields' => [],
60
        ];
61
    }
62
}
63