Skip to content

[Console][Table] fixed render row that contains multiple cells. #17002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ private function buildTableRows($rows)

// Remove any new line breaks and replace it with a new line
foreach ($rows[$rowKey] as $column => $cell) {
$rows[$rowKey] = $this->fillCells($rows[$rowKey], $column);
if (!strstr($cell, "\n")) {
continue;
}
Expand All @@ -363,7 +362,7 @@ private function buildTableRows($rows)

$tableRows = array();
foreach ($rows as $rowKey => $row) {
$tableRows[] = $row;
$tableRows[] = $this->fillCells($row);
if (isset($unmergedRows[$rowKey])) {
$tableRows = array_merge($tableRows, $unmergedRows[$rowKey]);
}
Expand Down Expand Up @@ -429,21 +428,23 @@ private function fillNextRows($rows, $line)
* fill cells for a row that contains colspan > 1.
*
* @param array $row
* @param int $column
*
* @return array
*/
private function fillCells($row, $column)
private function fillCells($row)
{
$cell = $row[$column];
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
foreach (range($column + 1, $column + $cell->getColspan() - 1) as $position) {
// insert empty value into rows at column position
array_splice($row, $position, 0, '');
$newRow = array();
foreach ($row as $column => $cell) {
$newRow[] = $cell;
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
foreach (range($column + 1, $column + $cell->getColspan() - 1) as $position) {
// insert empty value at column position
$newRow[] = '';
}
}
}

return $row;
return $newRow ?: $row;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,24 @@ public function testRenderProvider()
| ISBN | Title | Author |
+------+-------+--------+

TABLE
),
'Row with multiple cells' => array(
array(),
array(
array(
new TableCell('1', array('colspan' => 3)),
new TableCell('2', array('colspan' => 2)),
new TableCell('3', array('colspan' => 2)),
new TableCell('4', array('colspan' => 2)),
),
),
'default',
<<<TABLE
+--+--+--+--+--+--+--+--+--+
| 1 | 2 | 3 | 4 |
+--+--+--+--+--+--+--+--+--+

TABLE
),
);
Expand Down