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.
This commit is contained in:
oleibman
2025-03-24 21:06:43 -07:00
parent ca71ae77ac
commit eb31984340
2 changed files with 21 additions and 6 deletions
+6 -1
View File
@@ -1200,7 +1200,12 @@ You can then echo `$result` to a terminal, or write it to a file with `file_put_
| A | B | C | D |
+---+-----+------------------+---+----------+
| 1 | 6 | 1900-01-06 00:00 | | 0.572917 |
| 2 | 6 | 1900-01-06 00:00 | | 1<>2 |
| 2 | 6 | TRUE | | 1<>2 |
| 3 | xyz | xyz | | |
+---+-----+------------------+---+----------+
```
Please note that this may produce sub-optimal results for situations such as:
- 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)
- Fullwidth characters
- right-to-left characters (better display in a browser than a terminal on a non-RTL system)
- multi-line strings
+15 -5
View File
@@ -48,7 +48,7 @@ class TextGrid
if (!empty($this->rows)) {
$maxRow = max($this->rows);
$maxRowLength = mb_strlen((string) $maxRow) + 1;
$maxRowLength = strlen((string) $maxRow) + 1;
$columnWidths = $this->getColumnWidths();
$this->renderColumnHeader($maxRowLength, $columnWidths);
@@ -80,10 +80,10 @@ class TextGrid
private function renderCells(array $rowData, array $columnWidths): void
{
foreach ($rowData as $column => $cell) {
$valueForLength = StringHelper::convertToString($cell, convertBool: true);
$valueForLength = $this->getString($cell);
$displayCell = $this->isCli ? $valueForLength : htmlentities($valueForLength);
$this->gridDisplay .= '| ';
$this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - mb_strlen($valueForLength) + 1);
$this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - $this->strlen($valueForLength) + 1);
}
}
@@ -95,7 +95,7 @@ class TextGrid
return;
}
foreach ($this->columns as $column => $reference) {
$columnWidths[$column] = max($columnWidths[$column], mb_strlen($reference));
$columnWidths[$column] = max($columnWidths[$column], $this->strlen($reference));
}
if ($this->rowHeaders) {
$this->gridDisplay .= str_repeat(' ', $maxRowLength + 2);
@@ -145,9 +145,19 @@ class TextGrid
$columnData = array_values($columnData);
foreach ($columnData as $columnValue) {
$columnWidth = max($columnWidth, mb_strlen(StringHelper::convertToString($columnValue, convertBool: true)));
$columnWidth = max($columnWidth, $this->strlen($this->getString($columnValue)));
}
return $columnWidth;
}
protected function getString(mixed $value): string
{
return StringHelper::convertToString($value, convertBool: true);
}
protected function strlen(string $value): int
{
return mb_strlen($value);
}
}