Passed
Branch master (03c1d9)
by Greg
02:52
created

RowsOfFields::getListData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Consolidation\OutputFormatters\StructuredData;
3
4
use Consolidation\OutputFormatters\RestructureInterface;
5
use Consolidation\OutputFormatters\StructuredData\ListDataInterface;
6
use Consolidation\OutputFormatters\Transformations\PropertyParser;
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 7
    public function __construct($data)
22
    {
23 7
        parent::__construct($data);
24 7
    }
25
26 3
    public function restructure($configurationData, $options)
27
    {
28 3
        $data = $this->getArrayCopy();
29 3
        return $this->createTableTransformation($data, $configurationData, $options);
30
    }
31
32 1
    public function getListData()
33
    {
34 1
        return array_keys($this->getArrayCopy());
35
    }
36
37 5
    protected function createTableTransformation($data, $configurationData, $options)
38
    {
39 5
        $options = $this->interpretOptions($configurationData, $options);
40
41 5
        $reorderer = new ReorderFields();
42 5
        $fieldLabels = $reorderer->reorder($options['fields'], $options['field-labels'], $data);
43
44 5
        $tableTransformer = new TableTransformation($data, $fieldLabels);
45
46 5
        return $tableTransformer;
47
    }
48
49 5
    protected function interpretOptions($configurationData, $options)
50
    {
51 5
        $configurationData += $this->defaultOptions();
52
53 5
        $configurationData['field-labels'] = PropertyParser::parse($configurationData['field-labels']);
54 5
        $configurationData['default-fields'] = PropertyParser::parse($configurationData['default-fields']);
55
56 5
        return $options + $configurationData;
57
    }
58
59 5
    protected function defaultOptions()
60
    {
61
        return [
62 5
            'fields' => [],
63 5
            'field-labels' => [],
64 5
            'default-fields' => [],
65 5
        ];
66
    }
67
}
68