1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\OutputFormatters\Transformations; |
3
|
|
|
|
4
|
|
|
use Consolidation\OutputFormatters\StructuredData\TableDataInterface; |
5
|
|
|
use Consolidation\OutputFormatters\StructuredData\OriginalDataInterface; |
6
|
|
|
use Consolidation\OutputFormatters\StructuredData\MetadataHolderInterface; |
7
|
|
|
|
8
|
|
|
class TableTransformation extends \ArrayObject implements TableDataInterface, OriginalDataInterface |
9
|
|
|
{ |
10
|
|
|
protected $headers; |
11
|
|
|
protected $rowLabels; |
12
|
|
|
protected $layout; |
13
|
|
|
/** @var MetadataHolderInterface */ |
14
|
7 |
|
protected $originalData; |
15
|
|
|
|
16
|
7 |
|
const TABLE_LAYOUT = 'table'; |
17
|
7 |
|
const LIST_LAYOUT = 'list'; |
18
|
7 |
|
|
19
|
7 |
|
public function __construct($data, $fieldLabels, $rowLabels = []) |
20
|
7 |
|
{ |
21
|
|
|
$this->headers = $fieldLabels; |
22
|
3 |
|
$this->rowLabels = $rowLabels; |
23
|
|
|
$rows = static::transformRows($data, $fieldLabels); |
24
|
3 |
|
$this->layout = self::TABLE_LAYOUT; |
25
|
3 |
|
parent::__construct($rows); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function setLayout($layout) |
29
|
|
|
{ |
30
|
|
|
$this->layout = $layout; |
31
|
|
|
} |
32
|
7 |
|
|
33
|
|
|
public function getLayout() |
34
|
7 |
|
{ |
35
|
|
|
return $this->layout; |
36
|
|
|
} |
37
|
7 |
|
|
38
|
|
|
public function isList() |
39
|
7 |
|
{ |
40
|
7 |
|
return $this->layout == self::LIST_LAYOUT; |
41
|
7 |
|
} |
42
|
7 |
|
|
43
|
7 |
|
protected static function transformRows($data, $fieldLabels) |
44
|
|
|
{ |
45
|
|
|
$rows = []; |
46
|
7 |
|
foreach ($data as $rowid => $row) { |
47
|
|
|
$rows[$rowid] = static::transformRow($row, $fieldLabels); |
48
|
7 |
|
} |
49
|
7 |
|
return $rows; |
50
|
7 |
|
} |
51
|
7 |
|
|
52
|
7 |
|
protected static function transformRow($row, $fieldLabels) |
53
|
|
|
{ |
54
|
|
|
$result = []; |
55
|
7 |
|
foreach ($fieldLabels as $key => $label) { |
56
|
|
|
$result[$key] = array_key_exists($key, $row) ? $row[$key] : ''; |
57
|
7 |
|
} |
58
|
|
|
return $result; |
59
|
|
|
} |
60
|
3 |
|
|
61
|
|
|
public function getHeaders() |
62
|
3 |
|
{ |
63
|
3 |
|
return $this->headers; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getHeader($key) |
67
|
|
|
{ |
68
|
|
|
if (array_key_exists($key, $this->headers)) { |
69
|
|
|
return $this->headers[$key]; |
70
|
|
|
} |
71
|
|
|
return $key; |
72
|
|
|
} |
73
|
7 |
|
|
74
|
|
|
public function getRowLabels() |
75
|
7 |
|
{ |
76
|
7 |
|
return $this->rowLabels; |
77
|
3 |
|
} |
78
|
3 |
|
|
79
|
7 |
|
public function getRowLabel($rowid) |
80
|
3 |
|
{ |
81
|
3 |
|
if (array_key_exists($rowid, $this->rowLabels)) { |
82
|
7 |
|
return $this->rowLabels[$rowid]; |
83
|
|
|
} |
84
|
|
|
return $rowid; |
85
|
3 |
|
} |
86
|
|
|
|
87
|
3 |
|
public function getOriginalData() |
88
|
3 |
|
{ |
89
|
3 |
|
if (isset($this->originalData)) { |
90
|
3 |
|
return $this->originalData->reconstruct($this->getArrayCopy(), $this->originalData->getMetadata()); |
|
|
|
|
91
|
3 |
|
} |
92
|
3 |
|
return $this->getArrayCopy(); |
93
|
3 |
|
} |
94
|
|
|
|
95
|
|
|
public function setOriginalData(MetadataHolderInterface $data) |
96
|
3 |
|
{ |
97
|
|
|
$this->originalData = $data; |
98
|
3 |
|
} |
99
|
3 |
|
|
100
|
3 |
|
public function getTableData($includeRowKey = false) |
101
|
3 |
|
{ |
102
|
3 |
|
$data = $this->getArrayCopy(); |
103
|
3 |
|
if ($this->isList()) { |
104
|
3 |
|
$data = $this->convertTableToList(); |
105
|
3 |
|
} |
106
|
|
|
if ($includeRowKey) { |
107
|
|
|
$data = $this->getRowDataWithKey($data); |
108
|
|
|
} |
109
|
|
|
return $data; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function convertTableToList() |
113
|
|
|
{ |
114
|
|
|
$result = []; |
115
|
|
|
foreach ($this as $row) { |
116
|
|
|
foreach ($row as $key => $value) { |
117
|
|
|
$result[$key][] = $value; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
return $result; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function getRowDataWithKey($data) |
124
|
|
|
{ |
125
|
|
|
$result = []; |
126
|
|
|
$i = 0; |
127
|
|
|
foreach ($data as $key => $row) { |
128
|
|
|
array_unshift($row, $this->getHeader($key)); |
129
|
|
|
$i++; |
130
|
|
|
$result[$key] = $row; |
131
|
|
|
} |
132
|
|
|
return $result; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.