1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Helper; |
4
|
|
|
|
5
|
|
|
class TextGrid |
6
|
|
|
{ |
7
|
|
|
private bool $isCli; |
8
|
|
|
|
9
|
|
|
protected array $matrix; |
10
|
|
|
|
11
|
|
|
protected array $rows; |
12
|
|
|
|
13
|
|
|
protected array $columns; |
14
|
|
|
|
15
|
|
|
private string $gridDisplay; |
16
|
|
|
|
17
|
36 |
|
public function __construct(array $matrix, bool $isCli = true) |
18
|
|
|
{ |
19
|
36 |
|
$this->rows = array_keys($matrix); |
20
|
36 |
|
$this->columns = array_keys($matrix[$this->rows[0]]); |
21
|
|
|
|
22
|
36 |
|
$matrix = array_values($matrix); |
23
|
36 |
|
array_walk( |
24
|
36 |
|
$matrix, |
25
|
36 |
|
function (&$row): void { |
26
|
36 |
|
$row = array_values($row); |
27
|
36 |
|
} |
28
|
36 |
|
); |
29
|
|
|
|
30
|
36 |
|
$this->matrix = $matrix; |
31
|
36 |
|
$this->isCli = $isCli; |
32
|
|
|
} |
33
|
|
|
|
34
|
36 |
|
public function render(): string |
35
|
|
|
{ |
36
|
36 |
|
$this->gridDisplay = $this->isCli ? '' : '<pre>'; |
37
|
|
|
|
38
|
36 |
|
if (!empty($this->rows)) { |
39
|
36 |
|
$maxRow = max($this->rows); |
40
|
36 |
|
$maxRowLength = mb_strlen((string) $maxRow) + 1; |
41
|
36 |
|
$columnWidths = $this->getColumnWidths(); |
42
|
|
|
|
43
|
36 |
|
$this->renderColumnHeader($maxRowLength, $columnWidths); |
44
|
36 |
|
$this->renderRows($maxRowLength, $columnWidths); |
45
|
36 |
|
$this->renderFooter($maxRowLength, $columnWidths); |
46
|
|
|
} |
47
|
|
|
|
48
|
36 |
|
$this->gridDisplay .= $this->isCli ? '' : '</pre>'; |
49
|
|
|
|
50
|
36 |
|
return $this->gridDisplay; |
51
|
|
|
} |
52
|
|
|
|
53
|
36 |
|
private function renderRows(int $maxRowLength, array $columnWidths): void |
54
|
|
|
{ |
55
|
36 |
|
foreach ($this->matrix as $row => $rowData) { |
56
|
36 |
|
$this->gridDisplay .= '|' . str_pad((string) $this->rows[$row], $maxRowLength, ' ', STR_PAD_LEFT) . ' '; |
57
|
36 |
|
$this->renderCells($rowData, $columnWidths); |
58
|
36 |
|
$this->gridDisplay .= '|' . PHP_EOL; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
36 |
|
private function renderCells(array $rowData, array $columnWidths): void |
63
|
|
|
{ |
64
|
36 |
|
foreach ($rowData as $column => $cell) { |
65
|
36 |
|
$displayCell = ($this->isCli) ? (string) $cell : htmlentities((string) $cell); |
66
|
36 |
|
$this->gridDisplay .= '| '; |
67
|
36 |
|
$this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - mb_strlen($cell ?? '') + 1); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
36 |
|
private function renderColumnHeader(int $maxRowLength, array $columnWidths): void |
72
|
|
|
{ |
73
|
36 |
|
$this->gridDisplay .= str_repeat(' ', $maxRowLength + 2); |
74
|
36 |
|
foreach ($this->columns as $column => $reference) { |
75
|
36 |
|
$this->gridDisplay .= '+-' . str_repeat('-', $columnWidths[$column] + 1); |
76
|
|
|
} |
77
|
36 |
|
$this->gridDisplay .= '+' . PHP_EOL; |
78
|
|
|
|
79
|
36 |
|
$this->gridDisplay .= str_repeat(' ', $maxRowLength + 2); |
80
|
36 |
|
foreach ($this->columns as $column => $reference) { |
81
|
36 |
|
$this->gridDisplay .= '| ' . str_pad((string) $reference, $columnWidths[$column] + 1, ' '); |
82
|
|
|
} |
83
|
36 |
|
$this->gridDisplay .= '|' . PHP_EOL; |
84
|
|
|
|
85
|
36 |
|
$this->renderFooter($maxRowLength, $columnWidths); |
86
|
|
|
} |
87
|
|
|
|
88
|
36 |
|
private function renderFooter(int $maxRowLength, array $columnWidths): void |
89
|
|
|
{ |
90
|
36 |
|
$this->gridDisplay .= '+' . str_repeat('-', $maxRowLength + 1); |
91
|
36 |
|
foreach ($this->columns as $column => $reference) { |
92
|
36 |
|
$this->gridDisplay .= '+-'; |
93
|
36 |
|
$this->gridDisplay .= str_pad((string) '', $columnWidths[$column] + 1, '-'); |
94
|
|
|
} |
95
|
36 |
|
$this->gridDisplay .= '+' . PHP_EOL; |
96
|
|
|
} |
97
|
|
|
|
98
|
36 |
|
private function getColumnWidths(): array |
99
|
|
|
{ |
100
|
36 |
|
$columnCount = count($this->matrix, COUNT_RECURSIVE) / count($this->matrix); |
101
|
36 |
|
$columnWidths = []; |
102
|
36 |
|
for ($column = 0; $column < $columnCount; ++$column) { |
103
|
36 |
|
$columnWidths[] = $this->getColumnWidth(array_column($this->matrix, $column)); |
104
|
|
|
} |
105
|
|
|
|
106
|
36 |
|
return $columnWidths; |
107
|
|
|
} |
108
|
|
|
|
109
|
36 |
|
private function getColumnWidth(array $columnData): int |
110
|
|
|
{ |
111
|
36 |
|
$columnWidth = 0; |
112
|
36 |
|
$columnData = array_values($columnData); |
113
|
|
|
|
114
|
36 |
|
foreach ($columnData as $columnValue) { |
115
|
36 |
|
if (is_string($columnValue)) { |
116
|
36 |
|
$columnWidth = max($columnWidth, mb_strlen($columnValue)); |
117
|
22 |
|
} elseif (is_bool($columnValue)) { |
118
|
|
|
$columnWidth = max($columnWidth, mb_strlen($columnValue ? 'TRUE' : 'FALSE')); |
119
|
|
|
} |
120
|
|
|
|
121
|
36 |
|
$columnWidth = max($columnWidth, mb_strlen((string) $columnWidth)); |
122
|
|
|
} |
123
|
|
|
|
124
|
36 |
|
return $columnWidth; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|