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

RowsOfFields::interpretOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
rs 9.4285
ccs 5
cts 5
cp 1
cc 1
eloc 6
nc 1
nop 2
crap 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