Completed
Pull Request — master (#66)
by Greg
04:41
created

TableTransformation::setOriginalData()   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 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Consolidation\OutputFormatters\Transformations;
3
4
use Consolidation\OutputFormatters\StructuredData\TableDataInterface;
5
use Consolidation\OutputFormatters\StructuredData\OriginalDataInterface;
6
7
class TableTransformation extends \ArrayObject implements TableDataInterface, OriginalDataInterface
8
{
9
    protected $headers;
10
    protected $rowLabels;
11
    protected $layout;
12
    protected $originalData;
13
14 7
    const TABLE_LAYOUT = 'table';
15
    const LIST_LAYOUT = 'list';
16 7
17 7
    public function __construct($data, $fieldLabels, $rowLabels = [])
18 7
    {
19 7
        $this->headers = $fieldLabels;
20 7
        $this->rowLabels = $rowLabels;
21
        $rows = static::transformRows($data, $fieldLabels);
22 3
        $this->layout = self::TABLE_LAYOUT;
23
        parent::__construct($rows);
24 3
    }
25 3
26
    public function setLayout($layout)
27
    {
28
        $this->layout = $layout;
29
    }
30
31
    public function getLayout()
32 7
    {
33
        return $this->layout;
34 7
    }
35
36
    public function isList()
37 7
    {
38
        return $this->layout == self::LIST_LAYOUT;
39 7
    }
40 7
41 7
    protected static function transformRows($data, $fieldLabels)
42 7
    {
43 7
        $rows = [];
44
        foreach ($data as $rowid => $row) {
45
            $rows[$rowid] = static::transformRow($row, $fieldLabels);
46 7
        }
47
        return $rows;
48 7
    }
49 7
50 7
    protected static function transformRow($row, $fieldLabels)
51 7
    {
52 7
        $result = [];
53
        foreach ($fieldLabels as $key => $label) {
54
            $result[$key] = array_key_exists($key, $row) ? $row[$key] : '';
55 7
        }
56
        return $result;
57 7
    }
58
59
    public function getHeaders()
60 3
    {
61
        return $this->headers;
62 3
    }
63 3
64
    public function getHeader($key)
65
    {
66
        if (array_key_exists($key, $this->headers)) {
67
            return $this->headers[$key];
68
        }
69
        return $key;
70
    }
71
72
    public function getRowLabels()
73 7
    {
74
        return $this->rowLabels;
75 7
    }
76 7
77 3
    public function getRowLabel($rowid)
78 3
    {
79 7
        if (array_key_exists($rowid, $this->rowLabels)) {
80 3
            return $this->rowLabels[$rowid];
81 3
        }
82 7
        return $rowid;
83
    }
84
85 3
    public function getOriginalData()
86
    {
87 3
        if (isset($this->originalData)) {
88 3
            return $this->originalData;
89 3
        }
90 3
        return $this->getArrayCopy();
91 3
    }
92 3
93 3
    public function setOriginalData($data)
94
    {
95
        $this->originalData = $data;
96 3
    }
97
98 3
    public function getTableData($includeRowKey = false)
99 3
    {
100 3
        $data = $this->getArrayCopy();
101 3
        if ($this->isList()) {
102 3
            $data = $this->convertTableToList();
103 3
        }
104 3
        if ($includeRowKey) {
105 3
            $data = $this->getRowDataWithKey($data);
106
        }
107
        return $data;
108
    }
109
110
    protected function convertTableToList()
111
    {
112
        $result = [];
113
        foreach ($this as $row) {
114
            foreach ($row as $key => $value) {
115
                $result[$key][] = $value;
116
            }
117
        }
118
        return $result;
119
    }
120
121
    protected function getRowDataWithKey($data)
122
    {
123
        $result = [];
124
        $i = 0;
125
        foreach ($data as $key => $row) {
126
            array_unshift($row, $this->getHeader($key));
127
            $i++;
128
            $result[$key] = $row;
129
        }
130
        return $result;
131
    }
132
}
133