|
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
|
|
|
use Consolidation\OutputFormatters\Options\FormatterOptions; |
|
8
|
|
|
use Consolidation\OutputFormatters\Formatters\TsvFormatter; |
|
9
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
10
|
|
|
|
|
11
|
|
|
class TableTransformation extends \ArrayObject implements TableDataInterface, StringTransformationInterface, OriginalDataInterface |
|
12
|
|
|
{ |
|
13
|
|
|
protected $headers; |
|
14
|
7 |
|
protected $rowLabels; |
|
15
|
|
|
protected $layout; |
|
16
|
7 |
|
/** @var MetadataHolderInterface */ |
|
17
|
7 |
|
protected $originalData; |
|
18
|
7 |
|
|
|
19
|
7 |
|
const TABLE_LAYOUT = 'table'; |
|
20
|
7 |
|
const LIST_LAYOUT = 'list'; |
|
21
|
|
|
|
|
22
|
3 |
|
public function __construct($data, $fieldLabels, $rowLabels = []) |
|
23
|
|
|
{ |
|
24
|
3 |
|
$this->headers = $fieldLabels; |
|
25
|
3 |
|
$this->rowLabels = $rowLabels; |
|
26
|
|
|
$rows = static::transformRows($data, $fieldLabels); |
|
27
|
|
|
$this->layout = self::TABLE_LAYOUT; |
|
28
|
|
|
parent::__construct($rows); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function setLayout($layout) |
|
32
|
7 |
|
{ |
|
33
|
|
|
$this->layout = $layout; |
|
34
|
7 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getLayout() |
|
37
|
7 |
|
{ |
|
38
|
|
|
return $this->layout; |
|
39
|
7 |
|
} |
|
40
|
7 |
|
|
|
41
|
7 |
|
public function isList() |
|
42
|
7 |
|
{ |
|
43
|
7 |
|
return $this->layout == self::LIST_LAYOUT; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
7 |
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
7 |
|
*/ |
|
49
|
7 |
|
public function simplifyToString(FormatterOptions $options) |
|
50
|
7 |
|
{ |
|
51
|
7 |
|
$alternateFormatter = new TsvFormatter(); |
|
52
|
7 |
|
$output = new BufferedOutput(); |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
7 |
|
$data = $alternateFormatter->validate($this->getArrayCopy()); |
|
|
|
|
|
|
56
|
|
|
$alternateFormatter->write($output, $this->getArrayCopy(), $options); |
|
57
|
7 |
|
} catch (\Exception $e) { |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
return $output->fetch(); |
|
60
|
3 |
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
protected static function transformRows($data, $fieldLabels) |
|
63
|
3 |
|
{ |
|
64
|
|
|
$rows = []; |
|
65
|
|
|
foreach ($data as $rowid => $row) { |
|
66
|
|
|
$rows[$rowid] = static::transformRow($row, $fieldLabels); |
|
67
|
|
|
} |
|
68
|
|
|
return $rows; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected static function transformRow($row, $fieldLabels) |
|
72
|
|
|
{ |
|
73
|
7 |
|
$result = []; |
|
74
|
|
|
foreach ($fieldLabels as $key => $label) { |
|
75
|
7 |
|
$result[$key] = array_key_exists($key, $row) ? $row[$key] : ''; |
|
76
|
7 |
|
} |
|
77
|
3 |
|
return $result; |
|
78
|
3 |
|
} |
|
79
|
7 |
|
|
|
80
|
3 |
|
public function getHeaders() |
|
81
|
3 |
|
{ |
|
82
|
7 |
|
return $this->headers; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
public function getHeader($key) |
|
86
|
|
|
{ |
|
87
|
3 |
|
if (array_key_exists($key, $this->headers)) { |
|
88
|
3 |
|
return $this->headers[$key]; |
|
89
|
3 |
|
} |
|
90
|
3 |
|
return $key; |
|
91
|
3 |
|
} |
|
92
|
3 |
|
|
|
93
|
3 |
|
public function getRowLabels() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->rowLabels; |
|
96
|
3 |
|
} |
|
97
|
|
|
|
|
98
|
3 |
|
public function getRowLabel($rowid) |
|
99
|
3 |
|
{ |
|
100
|
3 |
|
if (array_key_exists($rowid, $this->rowLabels)) { |
|
101
|
3 |
|
return $this->rowLabels[$rowid]; |
|
102
|
3 |
|
} |
|
103
|
3 |
|
return $rowid; |
|
104
|
3 |
|
} |
|
105
|
3 |
|
|
|
106
|
|
|
public function getOriginalData() |
|
107
|
|
|
{ |
|
108
|
|
|
if (isset($this->originalData)) { |
|
109
|
|
|
return $this->originalData->reconstruct($this->getArrayCopy(), $this->originalData->getMetadata()); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
return $this->getArrayCopy(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function setOriginalData(MetadataHolderInterface $data) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->originalData = $data; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getTableData($includeRowKey = false) |
|
120
|
|
|
{ |
|
121
|
|
|
$data = $this->getArrayCopy(); |
|
122
|
|
|
if ($this->isList()) { |
|
123
|
|
|
$data = $this->convertTableToList(); |
|
124
|
|
|
} |
|
125
|
|
|
if ($includeRowKey) { |
|
126
|
|
|
$data = $this->getRowDataWithKey($data); |
|
127
|
|
|
} |
|
128
|
|
|
return $data; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
protected function convertTableToList() |
|
132
|
|
|
{ |
|
133
|
|
|
$result = []; |
|
134
|
|
|
foreach ($this as $row) { |
|
135
|
|
|
foreach ($row as $key => $value) { |
|
136
|
|
|
$result[$key][] = $value; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
return $result; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
protected function getRowDataWithKey($data) |
|
143
|
|
|
{ |
|
144
|
|
|
$result = []; |
|
145
|
|
|
$i = 0; |
|
146
|
|
|
foreach ($data as $key => $row) { |
|
147
|
|
|
array_unshift($row, $this->getHeader($key)); |
|
148
|
|
|
$i++; |
|
149
|
|
|
$result[$key] = $row; |
|
150
|
|
|
} |
|
151
|
|
|
return $result; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.