Skip to content

add footers support in table helper #10

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

Closed
wants to merge 1 commit into from
Closed
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
37 changes: 37 additions & 0 deletions Helper/TableHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class TableHelper extends Helper
*/
private $headers = array();

/**
* Table footers.
*
* @var array
*/
private $footers = array();

/**
* Table rows.
*
Expand All @@ -45,6 +52,7 @@ class TableHelper extends Helper
private $verticalBorderChar;
private $crossingChar;
private $cellHeaderFormat;
private $cellFooterFormat;
private $cellRowFormat;
private $cellRowContentFormat;
private $borderFormat;
Expand Down Expand Up @@ -91,6 +99,7 @@ public function setLayout($layout)
->setVerticalBorderChar(' ')
->setCrossingChar(' ')
->setCellHeaderFormat('<info>%s</info>')
->setCellFooterFormat('<info>%s</info>')
->setCellRowFormat('%s')
->setCellRowContentFormat(' %s ')
->setBorderFormat('%s')
Expand All @@ -105,6 +114,7 @@ public function setLayout($layout)
->setVerticalBorderChar(' ')
->setCrossingChar('')
->setCellHeaderFormat('<info>%s</info>')
->setCellFooterFormat('<info>%s</info>')
->setCellRowFormat('%s')
->setCellRowContentFormat('%s')
->setBorderFormat('%s')
Expand All @@ -119,6 +129,7 @@ public function setLayout($layout)
->setVerticalBorderChar('|')
->setCrossingChar('+')
->setCellHeaderFormat('<info>%s</info>')
->setCellFooterFormat('<info>%s</info>')
->setCellRowFormat('%s')
->setCellRowContentFormat(' %s ')
->setBorderFormat('%s')
Expand All @@ -141,6 +152,13 @@ public function setHeaders(array $headers)
return $this;
}

public function setFooters(array $footers)
{
$this->footers = array_values($footers);

return $this;
}

public function setRows(array $rows)
{
$this->rows = array();
Expand Down Expand Up @@ -268,6 +286,20 @@ public function setCellHeaderFormat($cellHeaderFormat)
return $this;
}

/**
* Sets footer cell format.
*
* @param string $cellFooterFormat
*
* @return TableHelper
*/
public function setCellFooterFormat($cellFooterFormat)
{
$this->cellFooterFormat = $cellFooterFormat;

return $this;
}

/**
* Sets row cell format.
*
Expand Down Expand Up @@ -353,6 +385,10 @@ public function render(OutputInterface $output)
if (!empty($this->rows)) {
$this->renderRowSeparator();
}
$this->renderRow($this->footers, $this->cellHeaderFormat);
if (!empty($this->footers)) {
$this->renderRowSeparator();
}

$this->cleanup();
}
Expand Down Expand Up @@ -472,6 +508,7 @@ private function getColumnWidth($column)
foreach ($this->rows as $row) {
$lengths[] = $this->getCellWidth($row, $column);
}
$lengths[] = $this->getCellWidth($this->footers, $column);

return $this->columnWidths[$column] = max($lengths) + strlen($this->cellRowContentFormat) - 2;
}
Expand Down