From 40e58cdc9a9ce99a25f3c8c01e5e2df01f5d6551 Mon Sep 17 00:00:00 2001 From: Krzysztof Przybyszewski Date: Sun, 15 Dec 2013 18:59:25 +0100 Subject: [PATCH] fixed TableHelper when cell value has new line --- .../Component/Console/Helper/TableHelper.php | 23 +++++++++++++++++++ .../Console/Tests/Helper/TableHelperTest.php | 22 ++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/Symfony/Component/Console/Helper/TableHelper.php b/src/Symfony/Component/Console/Helper/TableHelper.php index c18f29598a..d08f9416f4 100644 --- a/src/Symfony/Component/Console/Helper/TableHelper.php +++ b/src/Symfony/Component/Console/Helper/TableHelper.php @@ -142,6 +142,29 @@ class TableHelper extends Helper public function addRow(array $row) { $this->rows[] = array_values($row); + + $keys = array_keys($this->rows); + $rowKey = array_pop($keys); + + foreach ($row as $key => $cellValue) { + if (strstr($cellValue, "\n")) { + + $lines = explode("\n", $cellValue); + $this->rows[$rowKey][$key] = $lines[0]; + unset($lines[0]); + + foreach ($lines as $lineKey => $line) { + + $nextRowKey = $rowKey + $lineKey + 1; + + if (isset($this->rows[$nextRowKey])) { + $this->rows[$nextRowKey][$key] = $line; + } else { + $this->rows[$nextRowKey] = array($key => $line); + } + } + } + } return $this; } diff --git a/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php index 5873a7fda6..983accf8c2 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php @@ -152,6 +152,28 @@ TABLE | 80-902734-1-6 | And Then There Were None | Agatha Christie | +---------------+--------------------------+------------------+ +TABLE + ), + array( + array('ISBN', 'Title', 'Author'), + array( + array("99921-58-10-7", "Divine\nComedy", "Dante Alighieri"), + array("9971-5-0210-2", "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."), + array("960-425-059-0", "The Lord of the Rings", "J. R. R.\nTolkien"), + ), + TableHelper::LAYOUT_DEFAULT, +<<