Completed
Pull Request — master (#10)
by Greg
02:12
created

RowsOfFields::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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