Completed
Pull Request — master (#11)
by Greg
02:06
created

RowsOfFields::restructure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
        $data = $this->getArrayCopy();
28
        return $this->createTableTransformation($data, $configurationData, $options);
29
    }
30
31
    protected function createTableTransformation($data, $configurationData, $options)
32
    {
33
        $options = $this->interpretOptions($configurationData, $options);
34
35
        $reorderer = new ReorderFields();
36
        $fieldLabels = $reorderer->reorder($options['fields'], $options['field-labels'], $data);
37
38
        $tableTransformer = new TableTransformation($data, $fieldLabels);
39
40
        return $tableTransformer;
41
    }
42
43
    protected function interpretOptions($configurationData, $options)
44
    {
45
        $configurationData += $this->defaultOptions();
46
47
        $configurationData['field-labels'] = PropertyParser::parse($configurationData['field-labels']);
48
        $configurationData['default-fields'] = PropertyParser::parse($configurationData['default-fields']);
49
50
        return $options + $configurationData;
51
    }
52
53
    protected function defaultOptions()
54
    {
55
        return [
56
            'fields' => [],
57
            'field-labels' => [],
58
            'default-fields' => [],
59
        ];
60
    }
61
}
62