Skip to content

Commit eb31984

Browse files
committed
Make Class Extendable
Document some cases where solution might not work well. Making class extendable and offering some over-rideable methods may allow for solutions to some of these problems.
1 parent ca71ae7 commit eb31984

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

docs/topics/reading-and-writing-to-file.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,12 @@ You can then echo `$result` to a terminal, or write it to a file with `file_put_
12001200
| A | B | C | D |
12011201
+---+-----+------------------+---+----------+
12021202
| 1 | 6 | 1900-01-06 00:00 | | 0.572917 |
1203-
| 2 | 6 | 1900-01-06 00:00 | | 1<>2 |
1203+
| 2 | 6 | TRUE | | 1<>2 |
12041204
| 3 | xyz | xyz | | |
12051205
+---+-----+------------------+---+----------+
12061206
```
1207+
Please note that this may produce sub-optimal results for situations such as:
1208+
- use of accents as combining characters rather than using pre-composed characters (may be handled by extending the class to override the `getString` or `strlen` methods)
1209+
- Fullwidth characters
1210+
- right-to-left characters (better display in a browser than a terminal on a non-RTL system)
1211+
- multi-line strings

src/PhpSpreadsheet/Helper/TextGrid.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function render(): string
4848

4949
if (!empty($this->rows)) {
5050
$maxRow = max($this->rows);
51-
$maxRowLength = mb_strlen((string) $maxRow) + 1;
51+
$maxRowLength = strlen((string) $maxRow) + 1;
5252
$columnWidths = $this->getColumnWidths();
5353

5454
$this->renderColumnHeader($maxRowLength, $columnWidths);
@@ -80,10 +80,10 @@ private function renderRows(int $maxRowLength, array $columnWidths): void
8080
private function renderCells(array $rowData, array $columnWidths): void
8181
{
8282
foreach ($rowData as $column => $cell) {
83-
$valueForLength = StringHelper::convertToString($cell, convertBool: true);
83+
$valueForLength = $this->getString($cell);
8484
$displayCell = $this->isCli ? $valueForLength : htmlentities($valueForLength);
8585
$this->gridDisplay .= '| ';
86-
$this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - mb_strlen($valueForLength) + 1);
86+
$this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - $this->strlen($valueForLength) + 1);
8787
}
8888
}
8989

@@ -95,7 +95,7 @@ private function renderColumnHeader(int $maxRowLength, array &$columnWidths): vo
9595
return;
9696
}
9797
foreach ($this->columns as $column => $reference) {
98-
$columnWidths[$column] = max($columnWidths[$column], mb_strlen($reference));
98+
$columnWidths[$column] = max($columnWidths[$column], $this->strlen($reference));
9999
}
100100
if ($this->rowHeaders) {
101101
$this->gridDisplay .= str_repeat(' ', $maxRowLength + 2);
@@ -145,9 +145,19 @@ private function getColumnWidth(array $columnData): int
145145
$columnData = array_values($columnData);
146146

147147
foreach ($columnData as $columnValue) {
148-
$columnWidth = max($columnWidth, mb_strlen(StringHelper::convertToString($columnValue, convertBool: true)));
148+
$columnWidth = max($columnWidth, $this->strlen($this->getString($columnValue)));
149149
}
150150

151151
return $columnWidth;
152152
}
153+
154+
protected function getString(mixed $value): string
155+
{
156+
return StringHelper::convertToString($value, convertBool: true);
157+
}
158+
159+
protected function strlen(string $value): int
160+
{
161+
return mb_strlen($value);
162+
}
153163
}

0 commit comments

Comments
 (0)