From 88f84112294295e7f3425fafe1f4d4d8fda70589 Mon Sep 17 00:00:00 2001 From: Marcel Voigt Date: Mon, 25 Jan 2016 20:51:36 +0100 Subject: [PATCH 1/2] Implement MarkdownTable --- .../Console/Helper/MarkdownTable.php | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 src/PHPSemVerChecker/Console/Helper/MarkdownTable.php diff --git a/src/PHPSemVerChecker/Console/Helper/MarkdownTable.php b/src/PHPSemVerChecker/Console/Helper/MarkdownTable.php new file mode 100644 index 0000000..5f5820d --- /dev/null +++ b/src/PHPSemVerChecker/Console/Helper/MarkdownTable.php @@ -0,0 +1,185 @@ +output = $output; + } + + /** + * Set the column headers. + * + * @param array $headers + * @return $this + */ + public function setHeaders(array $headers) + { + // Ensure zero-indexed array + $this->headers = array_values($headers); + return $this; + } + + /** + * Sets all rows, replacing any existing. + * + * @param array $rows + * @return $this + */ + public function setRows(array $rows) + { + $this->rows = []; + return $this->addRows($rows); + } + + /** + * @param array $rows + * @return $this + */ + public function addRows(array $rows) + { + foreach ($rows as $row) { + $this->addRow($row); + } + return $this; + } + + /** + * @param \Symfony\Component\Console\Helper\TableSeparator|array $row + * @return $this + */ + public function addRow($row) + { + if ($row instanceof TableSeparator) { + $this->rows[] = $row; + + return $this; + } + if (!is_array($row)) { + throw new \InvalidArgumentException('A row must be an array or a TableSeparator instance.'); + } + $this->rows[] = array_values($row); + return $this; + } + + /** + * @param int $index + * @param array $row + * @return $this + */ + public function setRow($index, array $row) + { + $this->rows[$index] = $row; + return $this; + } + + /** + * Renders table to output. + */ + public function render() + { + $this->prepare(); + $this->output->writeln(''); + $this->renderRow($this->headers); + $this->renderRowSeparator(); + foreach ($this->rows as $row) { + $this->renderRow($row); + } + } + + /** + * Renders a single row. + * + * @param \Symfony\Component\Console\Helper\TableSeparator|array $row + */ + private function renderRow($row) + { + if ($row instanceof TableSeparator) { + $this->renderRowSeparator(); + return; + } + $this->output->write('| '); + $cells = []; + foreach ($row as $index => $content) { + $cell = $content; + $padding = $this->columnWidths[$index] - Helper::strlenWithoutDecoration($this->output->getFormatter(), $content); + $cell .= str_repeat(' ', $padding); + $cells[] = $cell; + } + $this->output->writeln(implode(' | ', $cells) . ' |'); + } + + /** + * Renders the row separator. In this case it should only be used as a header separator. + */ + private function renderRowSeparator() + { + $this->output->write('|'); + foreach ($this->columnWidths as $columnWidth) { + $this->output->write(str_repeat('-', $columnWidth + 1)); + $this->output->write('-|'); + } + $this->output->writeln(''); + } + + /** + * Prepare for rendering. + */ + private function prepare() + { + $this->columnWidths = []; + $this->prepareColumnWidths($this->headers); + foreach ($this->rows as $row) { + $this->prepareColumnWidths($row); + } + } + + /** + * Extracts maximum column widths from a row. + * + * @param \Symfony\Component\Console\Helper\TableSeparator|array $row + */ + private function prepareColumnWidths($row) + { + if ($row instanceof TableSeparator) { + return; + } + foreach ($row as $index => $content) { + $currentMaximum = 0; + if (isset($this->columnWidths[$index])) { + $currentMaximum = $this->columnWidths[$index]; + } + $width = Helper::strlenWithoutDecoration($this->output->getFormatter(), $content); + $this->columnWidths[$index] = max($currentMaximum, $width); + } + } +} From c2cd0c283d5cb489c317de490051bbb3bb372292 Mon Sep 17 00:00:00 2001 From: Marcel Voigt Date: Mon, 25 Jan 2016 20:51:52 +0100 Subject: [PATCH 2/2] Use MarkdownTable in Reporter --- src/PHPSemVerChecker/Reporter/Reporter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PHPSemVerChecker/Reporter/Reporter.php b/src/PHPSemVerChecker/Reporter/Reporter.php index 0c216b2..2e5dd2a 100644 --- a/src/PHPSemVerChecker/Reporter/Reporter.php +++ b/src/PHPSemVerChecker/Reporter/Reporter.php @@ -2,10 +2,10 @@ namespace PHPSemVerChecker\Reporter; +use PHPSemVerChecker\Console\Helper\MarkdownTable; use PHPSemVerChecker\Operation\Operation; use PHPSemVerChecker\Report\Report; use PHPSemVerChecker\SemanticVersioning\Level; -use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Output\OutputInterface; class Reporter { @@ -72,7 +72,7 @@ protected function outputReport(OutputInterface $output, Report $report, $contex */ protected function outputTable(OutputInterface $output, Report $report, $context) { - $table = new Table($output); + $table = new MarkdownTable($output); $table->setHeaders(['Level', 'Location', 'Target', 'Reason', 'Code']); foreach (Level::asList('desc') as $level) { $reportForLevel = $report[$context][$level];