Augmentation vers version 3.3.0

This commit is contained in:
Gauvain Boiché
2020-03-31 15:31:03 +02:00
parent d926806907
commit a1864c0414
2618 changed files with 406015 additions and 31377 deletions

View File

@@ -27,17 +27,17 @@ class Table
/**
* Table headers.
*/
private $headers = array();
private $headers = [];
/**
* Table rows.
*/
private $rows = array();
private $rows = [];
/**
* Column widths cache.
*/
private $columnWidths = array();
private $effectiveColumnWidths = [];
/**
* Number of columns cache.
@@ -59,7 +59,14 @@ class Table
/**
* @var array
*/
private $columnStyles = array();
private $columnStyles = [];
/**
* User set column widths.
*
* @var array
*/
private $columnWidths = [];
private static $styles;
@@ -168,11 +175,41 @@ class Table
return $this->getStyle();
}
/**
* Sets the minimum width of a column.
*
* @param int $columnIndex Column index
* @param int $width Minimum column width in characters
*
* @return $this
*/
public function setColumnWidth($columnIndex, $width)
{
$this->columnWidths[(int) $columnIndex] = (int) $width;
return $this;
}
/**
* Sets the minimum width of all columns.
*
* @return $this
*/
public function setColumnWidths(array $widths)
{
$this->columnWidths = [];
foreach ($widths as $index => $width) {
$this->setColumnWidth($index, $width);
}
return $this;
}
public function setHeaders(array $headers)
{
$headers = array_values($headers);
if (!empty($headers) && !\is_array($headers[0])) {
$headers = array($headers);
$headers = [$headers];
}
$this->headers = $headers;
@@ -182,7 +219,7 @@ class Table
public function setRows(array $rows)
{
$this->rows = array();
$this->rows = [];
return $this->addRows($rows);
}
@@ -281,7 +318,7 @@ class Table
$markup = $this->style->getCrossingChar();
for ($column = 0; $column < $count; ++$column) {
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->columnWidths[$column]).$this->style->getCrossingChar();
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]).$this->style->getCrossingChar();
}
$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
@@ -302,7 +339,6 @@ class Table
*
* | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
*
* @param array $row
* @param string $cellFormat
*/
private function renderRow(array $row, $cellFormat)
@@ -322,18 +358,17 @@ class Table
/**
* Renders table cell with padding.
*
* @param array $row
* @param int $column
* @param string $cellFormat
*/
private function renderCell(array $row, $column, $cellFormat)
{
$cell = isset($row[$column]) ? $row[$column] : '';
$width = $this->columnWidths[$column];
$width = $this->effectiveColumnWidths[$column];
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
// add the width of the following columns(numbers of colspan).
foreach (range($column + 1, $column + $cell->getColspan() - 1) as $nextColumn) {
$width += $this->getColumnSeparatorWidth() + $this->columnWidths[$nextColumn];
$width += $this->getColumnSeparatorWidth() + $this->effectiveColumnWidths[$nextColumn];
}
}
@@ -363,7 +398,7 @@ class Table
return;
}
$columns = array(0);
$columns = [0];
foreach (array_merge($this->headers, $this->rows) as $row) {
if ($row instanceof TableSeparator) {
continue;
@@ -377,7 +412,7 @@ class Table
private function buildTableRows($rows)
{
$unmergedRows = array();
$unmergedRows = [];
for ($rowKey = 0; $rowKey < \count($rows); ++$rowKey) {
$rows = $this->fillNextRows($rows, $rowKey);
@@ -389,7 +424,7 @@ class Table
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
foreach ($lines as $lineKey => $line) {
if ($cell instanceof TableCell) {
$line = new TableCell($line, array('colspan' => $cell->getColspan()));
$line = new TableCell($line, ['colspan' => $cell->getColspan()]);
}
if (0 === $lineKey) {
$rows[$rowKey][$column] = $line;
@@ -400,7 +435,7 @@ class Table
}
}
$tableRows = array();
$tableRows = [];
foreach ($rows as $rowKey => $row) {
$tableRows[] = $this->fillCells($row);
if (isset($unmergedRows[$rowKey])) {
@@ -414,31 +449,35 @@ class Table
/**
* fill rows that contains rowspan > 1.
*
* @param array $rows
* @param int $line
* @param int $line
*
* @return array
*
* @throws InvalidArgumentException
*/
private function fillNextRows(array $rows, $line)
{
$unmergedRows = array();
$unmergedRows = [];
foreach ($rows[$line] as $column => $cell) {
if (null !== $cell && !$cell instanceof TableCell && !is_scalar($cell) && !(\is_object($cell) && method_exists($cell, '__toString'))) {
throw new InvalidArgumentException(sprintf('A cell must be a TableCell, a scalar or an object implementing __toString, %s given.', \gettype($cell)));
}
if ($cell instanceof TableCell && $cell->getRowspan() > 1) {
$nbLines = $cell->getRowspan() - 1;
$lines = array($cell);
$lines = [$cell];
if (strstr($cell, "\n")) {
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
$nbLines = \count($lines) > $nbLines ? substr_count($cell, "\n") : $nbLines;
$rows[$line][$column] = new TableCell($lines[0], array('colspan' => $cell->getColspan()));
$rows[$line][$column] = new TableCell($lines[0], ['colspan' => $cell->getColspan()]);
unset($lines[0]);
}
// create a two dimensional array (rowspan x colspan)
$unmergedRows = array_replace_recursive(array_fill($line + 1, $nbLines, array()), $unmergedRows);
$unmergedRows = array_replace_recursive(array_fill($line + 1, $nbLines, []), $unmergedRows);
foreach ($unmergedRows as $unmergedRowKey => $unmergedRow) {
$value = isset($lines[$unmergedRowKey - $line]) ? $lines[$unmergedRowKey - $line] : '';
$unmergedRows[$unmergedRowKey][$column] = new TableCell($value, array('colspan' => $cell->getColspan()));
$unmergedRows[$unmergedRowKey][$column] = new TableCell($value, ['colspan' => $cell->getColspan()]);
if ($nbLines === $unmergedRowKey - $line) {
break;
}
@@ -451,7 +490,7 @@ class Table
if (isset($rows[$unmergedRowKey]) && \is_array($rows[$unmergedRowKey]) && ($this->getNumberOfColumns($rows[$unmergedRowKey]) + $this->getNumberOfColumns($unmergedRows[$unmergedRowKey]) <= $this->numberOfColumns)) {
foreach ($unmergedRow as $cellKey => $cell) {
// insert cell into row at cellKey position
array_splice($rows[$unmergedRowKey], $cellKey, 0, array($cell));
array_splice($rows[$unmergedRowKey], $cellKey, 0, [$cell]);
}
} else {
$row = $this->copyRow($rows, $unmergedRowKey - 1);
@@ -460,7 +499,7 @@ class Table
$row[$column] = $unmergedRow[$column];
}
}
array_splice($rows, $unmergedRowKey, 0, array($row));
array_splice($rows, $unmergedRowKey, 0, [$row]);
}
}
@@ -474,7 +513,7 @@ class Table
*/
private function fillCells($row)
{
$newRow = array();
$newRow = [];
foreach ($row as $column => $cell) {
$newRow[] = $cell;
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
@@ -489,8 +528,7 @@ class Table
}
/**
* @param array $rows
* @param int $line
* @param int $line
*
* @return array
*/
@@ -500,7 +538,7 @@ class Table
foreach ($row as $cellKey => $cellValue) {
$row[$cellKey] = '';
if ($cellValue instanceof TableCell) {
$row[$cellKey] = new TableCell('', array('colspan' => $cellValue->getColspan()));
$row[$cellKey] = new TableCell('', ['colspan' => $cellValue->getColspan()]);
}
}
@@ -542,13 +580,11 @@ class Table
/**
* Calculates columns widths.
*
* @param array $rows
*/
private function calculateColumnsWidth($rows)
private function calculateColumnsWidth(array $rows)
{
for ($column = 0; $column < $this->numberOfColumns; ++$column) {
$lengths = array();
$lengths = [];
foreach ($rows as $row) {
if ($row instanceof TableSeparator) {
continue;
@@ -570,7 +606,7 @@ class Table
$lengths[] = $this->getCellWidth($row, $column);
}
$this->columnWidths[$column] = max($lengths) + Helper::strlen($this->style->getCellRowContentFormat()) - 2;
$this->effectiveColumnWidths[$column] = max($lengths) + Helper::strlen($this->style->getCellRowContentFormat()) - 2;
}
}
@@ -587,21 +623,22 @@ class Table
/**
* Gets cell width.
*
* @param array $row
* @param int $column
* @param int $column
*
* @return int
*/
private function getCellWidth(array $row, $column)
{
$cellWidth = 0;
if (isset($row[$column])) {
$cell = $row[$column];
$cellWidth = Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
return $cellWidth;
}
return 0;
$columnWidth = isset($this->columnWidths[$column]) ? $this->columnWidths[$column] : 0;
return max($cellWidth, $columnWidth);
}
/**
@@ -609,7 +646,7 @@ class Table
*/
private function cleanup()
{
$this->columnWidths = array();
$this->effectiveColumnWidths = [];
$this->numberOfColumns = null;
}
@@ -638,12 +675,12 @@ class Table
->setCellHeaderFormat('%s')
;
return array(
return [
'default' => new TableStyle(),
'borderless' => $borderless,
'compact' => $compact,
'symfony-style-guide' => $styleGuide,
);
];
}
private function resolveStyle($name)