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