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:
Fabien Potencier 2013-06-16 14:10:21 +02:00
commit 0da84dac18
2 changed files with 34 additions and 1 deletions

View File

@ -359,12 +359,18 @@ class TableHelper extends Helper
private function renderCell(array $row, $column, $cellFormat)
{
$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(
$cellFormat,
str_pad(
$this->paddingChar.$cell.$this->paddingChar,
$this->getColumnWidth($column),
$width,
$this->paddingChar,
$this->padType
)

View File

@ -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()
{
return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);