merged branch jakzal/bugfix/table-helper-multi-byte (PR #8285)
This PR was merged into the 2.3 branch.
Discussion
----------
[Console] Fixed the table rendering with multi-byte strings.
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #8284
| License | MIT
| Doc PR | -
Commits
-------
ab1439e
[Console] Fixed the table rendering with multi-byte strings.
This commit is contained in:
commit
0da84dac18
@ -359,12 +359,18 @@ class TableHelper extends Helper
|
|||||||
private function renderCell(array $row, $column, $cellFormat)
|
private function renderCell(array $row, $column, $cellFormat)
|
||||||
{
|
{
|
||||||
$cell = isset($row[$column]) ? $row[$column] : '';
|
$cell = isset($row[$column]) ? $row[$column] : '';
|
||||||
|
$width = $this->getColumnWidth($column);
|
||||||
|
|
||||||
|
// str_pad won't work properly with multi-byte strings, we need to fix the padding
|
||||||
|
if (function_exists('mb_strlen') && false !== $encoding = mb_detect_encoding($cell)) {
|
||||||
|
$width += strlen($cell) - mb_strlen($cell, $encoding);
|
||||||
|
}
|
||||||
|
|
||||||
$this->output->write(sprintf(
|
$this->output->write(sprintf(
|
||||||
$cellFormat,
|
$cellFormat,
|
||||||
str_pad(
|
str_pad(
|
||||||
$this->paddingChar.$cell.$this->paddingChar,
|
$this->paddingChar.$cell.$this->paddingChar,
|
||||||
$this->getColumnWidth($column),
|
$width,
|
||||||
$this->paddingChar,
|
$this->paddingChar,
|
||||||
$this->padType
|
$this->padType
|
||||||
)
|
)
|
||||||
|
@ -174,6 +174,33 @@ TABLE
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRenderMultiByte()
|
||||||
|
{
|
||||||
|
if (!function_exists('mb_strlen')) {
|
||||||
|
$this->markTestSkipped('The "mbstring" extension is not available');
|
||||||
|
}
|
||||||
|
|
||||||
|
$table = new TableHelper();
|
||||||
|
$table
|
||||||
|
->setHeaders(array('■■'))
|
||||||
|
->setRows(array(array(1234)))
|
||||||
|
->setLayout(TableHelper::LAYOUT_DEFAULT)
|
||||||
|
;
|
||||||
|
$table->render($output = $this->getOutputStream());
|
||||||
|
|
||||||
|
$expected =
|
||||||
|
<<<TABLE
|
||||||
|
+------+
|
||||||
|
| ■■ |
|
||||||
|
+------+
|
||||||
|
| 1234 |
|
||||||
|
+------+
|
||||||
|
|
||||||
|
TABLE;
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||||
|
}
|
||||||
|
|
||||||
protected function getOutputStream()
|
protected function getOutputStream()
|
||||||
{
|
{
|
||||||
return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
|
return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
|
||||||
|
Reference in New Issue
Block a user