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