Completed
Push — master ( 42fedb...ca07cd )
by Greg
10s
created

TableTransformation   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 91.53%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 2
cbo 0
dl 0
loc 117
ccs 54
cts 59
cp 0.9153
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setLayout() 0 4 1
A getLayout() 0 4 1
A isList() 0 4 1
A transformRows() 0 8 2
A transformRow() 0 8 3
A getHeaders() 0 4 1
A getHeader() 0 7 2
A getRowLabels() 0 4 1
A getRowLabel() 0 7 2
A getOriginalData() 0 4 1
A getTableData() 0 11 3
A convertTableToList() 0 10 3
A getRowDataWithKey() 0 11 2
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
13
    const TABLE_LAYOUT = 'table';
14 7
    const LIST_LAYOUT = 'list';
15
16 7
    public function __construct($data, $fieldLabels, $rowLabels = [])
17 7
    {
18 7
        $this->headers = $fieldLabels;
19 7
        $this->rowLabels = $rowLabels;
20 7
        $rows = static::transformRows($data, $fieldLabels);
21
        $this->layout = self::TABLE_LAYOUT;
22 3
        parent::__construct($rows);
23
    }
24 3
25 3
    public function setLayout($layout)
26
    {
27
        $this->layout = $layout;
28
    }
29
30
    public function getLayout()
31
    {
32 7
        return $this->layout;
33
    }
34 7
35
    public function isList()
36
    {
37 7
        return $this->layout == self::LIST_LAYOUT;
38
    }
39 7
40 7
    protected static function transformRows($data, $fieldLabels)
41 7
    {
42 7
        $rows = [];
43 7
        foreach ($data as $rowid => $row) {
44
            $rows[$rowid] = static::transformRow($row, $fieldLabels);
45
        }
46 7
        return $rows;
47
    }
48 7
49 7
    protected static function transformRow($row, $fieldLabels)
50 7
    {
51 7
        $result = [];
52 7
        foreach ($fieldLabels as $key => $label) {
53
            $result[$key] = array_key_exists($key, $row) ? $row[$key] : '';
54
        }
55 7
        return $result;
56
    }
57 7
58
    public function getHeaders()
59
    {
60 3
        return $this->headers;
61
    }
62 3
63 3
    public function getHeader($key)
64
    {
65
        if (array_key_exists($key, $this->headers)) {
66
            return $this->headers[$key];
67
        }
68
        return $key;
69
    }
70
71
    public function getRowLabels()
72
    {
73 7
        return $this->rowLabels;
74
    }
75 7
76 7
    public function getRowLabel($rowid)
77 3
    {
78 3
        if (array_key_exists($rowid, $this->rowLabels)) {
79 7
            return $this->rowLabels[$rowid];
80 3
        }
81 3
        return $rowid;
82 7
    }
83
84
    public function getOriginalData()
85 3
    {
86
        return $this->getArrayCopy();
87 3
    }
88 3
89 3
    public function getTableData($includeRowKey = false)
90 3
    {
91 3
        $data = $this->getArrayCopy();
92 3
        if ($this->isList()) {
93 3
            $data = $this->convertTableToList();
94
        }
95
        if ($includeRowKey) {
96 3
            $data = $this->getRowDataWithKey($data);
97
        }
98 3
        return $data;
99 3
    }
100 3
101 3
    protected function convertTableToList()
102 3
    {
103 3
        $result = [];
104 3
        foreach ($this as $row) {
105 3
            foreach ($row as $key => $value) {
106
                $result[$key][] = $value;
107
            }
108
        }
109
        return $result;
110
    }
111
112
    protected function getRowDataWithKey($data)
113
    {
114
        $result = [];
115
        $i = 0;
116
        foreach ($data as $key => $row) {
117
            array_unshift($row, $this->getHeader($key));
118
            $i++;
119
            $result[$key] = $row;
120
        }
121
        return $result;
122
    }
123
}
124