1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This is just a simple helper class which displays the result set as a nice console table. |
5
|
|
|
* |
6
|
|
|
* Class ConsoleTable |
7
|
|
|
*/ |
8
|
|
|
class ConsoleTable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array<string,int> |
12
|
|
|
*/ |
13
|
|
|
protected array $columns = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param array<int|string, string|int|array<string|int, array<mixed>|int|string>> $data |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
protected function detectColumnSize(array $data): void |
21
|
|
|
{ |
22
|
|
|
// Find the longest string in each column |
23
|
|
|
$columns = []; |
24
|
|
|
foreach ($data as $row) { |
25
|
|
|
$row = (array)$row; |
26
|
|
|
foreach ($row as $cellKey => $cell) { |
27
|
|
|
if (is_array($cell)) { |
28
|
|
|
$cell = implode(', ', $cell); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$valueLength = strlen(strval($cell)); |
32
|
|
|
$nameLength = strlen(strval($cellKey)); |
33
|
|
|
|
34
|
|
|
$length = max($nameLength, $valueLength); |
35
|
|
|
|
36
|
|
|
if (empty($columns[$cellKey]) || $columns[$cellKey] < $length) { |
37
|
|
|
$columns[strval($cellKey)] = $length; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->columns = $columns; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function buildRow(): string |
46
|
|
|
{ |
47
|
|
|
$table = '+'; |
48
|
|
|
foreach ($this->columns as $length) { |
49
|
|
|
$table .= str_repeat('-', $length + 1) . '-+'; |
50
|
|
|
} |
51
|
|
|
$table .= PHP_EOL; |
52
|
|
|
|
53
|
|
|
return $table; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array<int|string, string|int|array<string|int, array<mixed>|int|string>> $data |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
protected function buildHeader(array $data): string |
62
|
|
|
{ |
63
|
|
|
$table = $this->buildRow(); |
64
|
|
|
|
65
|
|
|
// Build the header |
66
|
|
|
foreach ($data as $row) { |
67
|
|
|
$table .= '| '; |
68
|
|
|
$row = (array)$row; |
69
|
|
|
foreach ($row as $cellKey => $cell) { |
70
|
|
|
$cellKey = strval($cellKey); |
71
|
|
|
$table .= str_pad($cellKey, $this->columns[$cellKey]) . ' | '; |
72
|
|
|
} |
73
|
|
|
$table .= PHP_EOL; |
74
|
|
|
|
75
|
|
|
break; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$table .= $this->buildRow(); |
79
|
|
|
|
80
|
|
|
return $table; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array<int|string, string|int|array<string|int, array<mixed>|int|string>> $data |
85
|
|
|
*/ |
86
|
|
|
public function __construct(array $data) |
87
|
|
|
{ |
88
|
|
|
$this->detectColumnSize($data); |
89
|
|
|
|
90
|
|
|
$table = $this->buildHeader($data); |
91
|
|
|
|
92
|
|
|
// Output table, padding columns |
93
|
|
|
foreach ($data as $row) { |
94
|
|
|
$table .= '| '; |
95
|
|
|
|
96
|
|
|
foreach ($this->columns as $column => $length) { |
97
|
|
|
$value = $row[$column] ?? ''; |
98
|
|
|
if (is_array($value)) { |
99
|
|
|
$value = implode(', ', $value); |
100
|
|
|
} |
101
|
|
|
$table .= str_pad((string)($value), $length) . ' | '; |
102
|
|
|
} |
103
|
|
|
$table .= PHP_EOL; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$table .= $this->buildRow(); |
107
|
|
|
|
108
|
|
|
echo $table; |
109
|
|
|
} |
110
|
|
|
} |