1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\OutputFormatters\Transformations; |
3
|
|
|
|
4
|
|
|
class TableTransformation extends \ArrayObject |
5
|
|
|
{ |
6
|
|
|
protected $headers; |
7
|
|
|
protected $layout; |
8
|
|
|
|
9
|
|
|
const TABLE_LAYOUT = 'table'; |
10
|
|
|
const LIST_LAYOUT = 'list'; |
11
|
|
|
|
12
|
|
|
public function __construct($data, $fieldLabels) |
13
|
|
|
{ |
14
|
|
|
$this->headers = $fieldLabels; |
15
|
|
|
$rows = static::transformRows($data, $fieldLabels); |
16
|
|
|
$this->layout = self::TABLE_LAYOUT; |
17
|
|
|
parent::__construct($rows); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function setLayout($layout) |
21
|
|
|
{ |
22
|
|
|
$this->layout = $layout; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getLayout() |
26
|
|
|
{ |
27
|
|
|
return $this->layout; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function isList() |
31
|
|
|
{ |
32
|
|
|
return $this->layout == self::LIST_LAYOUT; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected static function transformRows($data, $fieldLabels) |
36
|
|
|
{ |
37
|
|
|
$rows = []; |
38
|
|
|
foreach ($data as $row) { |
39
|
|
|
$rows[] = static::transformRow($row, $fieldLabels); |
40
|
|
|
} |
41
|
|
|
return $rows; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected static function transformRow($row, $fieldLabels) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$result = []; |
47
|
|
|
foreach ($fieldLabels as $key => $label) { |
48
|
|
|
$result[$key] = array_key_exists($key, $row) ? $row[$key] : ''; |
49
|
|
|
} |
50
|
|
|
return $result; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getHeaders() |
54
|
|
|
{ |
55
|
|
|
return $this->headers; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getHeader($key) |
59
|
|
|
{ |
60
|
|
|
if (array_key_exists($key, $this->headers)) { |
61
|
|
|
return $this->headers[$key]; |
62
|
|
|
} |
63
|
|
|
return $key; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getData($includeRowKey = false) |
67
|
|
|
{ |
68
|
|
|
$data = $this->getArrayCopy(); |
69
|
|
|
if ($this->isList()) { |
70
|
|
|
$data = $this->getListData(); |
71
|
|
|
} |
72
|
|
|
if ($includeRowKey) { |
73
|
|
|
$data = $this->getRowDataWithKey($data); |
74
|
|
|
} |
75
|
|
|
return $data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function getListData() |
79
|
|
|
{ |
80
|
|
|
$result = []; |
81
|
|
|
foreach ($this as $row) { |
82
|
|
|
foreach ($row as $key => $value) { |
83
|
|
|
$result[$key][] = $value; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
return $result; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getRowDataWithKey($data) |
90
|
|
|
{ |
91
|
|
|
$result = []; |
92
|
|
|
$i = 0; |
93
|
|
|
foreach ($data as $key => $row) { |
94
|
|
|
array_unshift($row, $this->getHeader($key)); |
95
|
|
|
$i++; |
96
|
|
|
$result[$key] = $row; |
97
|
|
|
} |
98
|
|
|
return $result; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.