Skip to content

Commit b3b80c8

Browse files
k-przybyszewskifabpot
authored andcommitted
fixed TableHelper when cell value has new line
1 parent f77c13a commit b3b80c8

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Helper/TableHelper.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,29 @@ public function addRows(array $rows)
142142
public function addRow(array $row)
143143
{
144144
$this->rows[] = array_values($row);
145+
146+
$keys = array_keys($this->rows);
147+
$rowKey = array_pop($keys);
148+
149+
foreach ($row as $key => $cellValue) {
150+
if (strstr($cellValue, "\n")) {
151+
152+
$lines = explode("\n", $cellValue);
153+
$this->rows[$rowKey][$key] = $lines[0];
154+
unset($lines[0]);
155+
156+
foreach ($lines as $lineKey => $line) {
157+
158+
$nextRowKey = $rowKey + $lineKey + 1;
159+
160+
if (isset($this->rows[$nextRowKey])) {
161+
$this->rows[$nextRowKey][$key] = $line;
162+
} else {
163+
$this->rows[$nextRowKey] = array($key => $line);
164+
}
165+
}
166+
}
167+
}
145168

146169
return $this;
147170
}

Tests/Helper/TableHelperTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,28 @@ public function testRenderProvider()
152152
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
153153
+---------------+--------------------------+------------------+
154154
155+
TABLE
156+
),
157+
array(
158+
array('ISBN', 'Title', 'Author'),
159+
array(
160+
array("99921-58-10-7", "Divine\nComedy", "Dante Alighieri"),
161+
array("9971-5-0210-2", "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
162+
array("960-425-059-0", "The Lord of the Rings", "J. R. R.\nTolkien"),
163+
),
164+
TableHelper::LAYOUT_DEFAULT,
165+
<<<TABLE
166+
+---------------+----------------------------+-----------------+
167+
| ISBN | Title | Author |
168+
+---------------+----------------------------+-----------------+
169+
| 99921-58-10-7 | Divine | Dante Alighieri |
170+
| | Comedy | |
171+
| 9971-5-0210-2 | Harry Potter | Rowling |
172+
| | and the Chamber of Secrets | Joanne K. |
173+
| 960-425-059-0 | The Lord of the Rings | J. R. R. |
174+
| | | Tolkien |
175+
+---------------+----------------------------+-----------------+
176+
155177
TABLE
156178
),
157179
array(

0 commit comments

Comments
 (0)