annotate vendor/symfony/console/Helper/Table.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Console\Helper;
Chris@0 13
Chris@17 14 use Symfony\Component\Console\Exception\InvalidArgumentException;
Chris@0 15 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Provides helpers to display a table.
Chris@0 19 *
Chris@0 20 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 21 * @author Саша Стаменковић <umpirsky@gmail.com>
Chris@0 22 * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
Chris@0 23 * @author Max Grigorian <maxakawizard@gmail.com>
Chris@0 24 */
Chris@0 25 class Table
Chris@0 26 {
Chris@0 27 /**
Chris@0 28 * Table headers.
Chris@0 29 */
Chris@17 30 private $headers = [];
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Table rows.
Chris@0 34 */
Chris@17 35 private $rows = [];
Chris@0 36
Chris@0 37 /**
Chris@0 38 * Column widths cache.
Chris@0 39 */
Chris@17 40 private $effectiveColumnWidths = [];
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Number of columns cache.
Chris@0 44 *
Chris@14 45 * @var int
Chris@0 46 */
Chris@0 47 private $numberOfColumns;
Chris@0 48
Chris@0 49 /**
Chris@0 50 * @var OutputInterface
Chris@0 51 */
Chris@0 52 private $output;
Chris@0 53
Chris@0 54 /**
Chris@0 55 * @var TableStyle
Chris@0 56 */
Chris@0 57 private $style;
Chris@0 58
Chris@0 59 /**
Chris@0 60 * @var array
Chris@0 61 */
Chris@17 62 private $columnStyles = [];
Chris@0 63
Chris@0 64 /**
Chris@0 65 * User set column widths.
Chris@0 66 *
Chris@0 67 * @var array
Chris@0 68 */
Chris@17 69 private $columnWidths = [];
Chris@0 70
Chris@0 71 private static $styles;
Chris@0 72
Chris@0 73 public function __construct(OutputInterface $output)
Chris@0 74 {
Chris@0 75 $this->output = $output;
Chris@0 76
Chris@0 77 if (!self::$styles) {
Chris@0 78 self::$styles = self::initStyles();
Chris@0 79 }
Chris@0 80
Chris@0 81 $this->setStyle('default');
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * Sets a style definition.
Chris@0 86 *
Chris@0 87 * @param string $name The style name
Chris@0 88 * @param TableStyle $style A TableStyle instance
Chris@0 89 */
Chris@0 90 public static function setStyleDefinition($name, TableStyle $style)
Chris@0 91 {
Chris@0 92 if (!self::$styles) {
Chris@0 93 self::$styles = self::initStyles();
Chris@0 94 }
Chris@0 95
Chris@0 96 self::$styles[$name] = $style;
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Gets a style definition by name.
Chris@0 101 *
Chris@0 102 * @param string $name The style name
Chris@0 103 *
Chris@0 104 * @return TableStyle
Chris@0 105 */
Chris@0 106 public static function getStyleDefinition($name)
Chris@0 107 {
Chris@0 108 if (!self::$styles) {
Chris@0 109 self::$styles = self::initStyles();
Chris@0 110 }
Chris@0 111
Chris@0 112 if (isset(self::$styles[$name])) {
Chris@0 113 return self::$styles[$name];
Chris@0 114 }
Chris@0 115
Chris@0 116 throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name));
Chris@0 117 }
Chris@0 118
Chris@0 119 /**
Chris@0 120 * Sets table style.
Chris@0 121 *
Chris@0 122 * @param TableStyle|string $name The style name or a TableStyle instance
Chris@0 123 *
Chris@0 124 * @return $this
Chris@0 125 */
Chris@0 126 public function setStyle($name)
Chris@0 127 {
Chris@0 128 $this->style = $this->resolveStyle($name);
Chris@0 129
Chris@0 130 return $this;
Chris@0 131 }
Chris@0 132
Chris@0 133 /**
Chris@0 134 * Gets the current table style.
Chris@0 135 *
Chris@0 136 * @return TableStyle
Chris@0 137 */
Chris@0 138 public function getStyle()
Chris@0 139 {
Chris@0 140 return $this->style;
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * Sets table column style.
Chris@0 145 *
Chris@0 146 * @param int $columnIndex Column index
Chris@0 147 * @param TableStyle|string $name The style name or a TableStyle instance
Chris@0 148 *
Chris@0 149 * @return $this
Chris@0 150 */
Chris@0 151 public function setColumnStyle($columnIndex, $name)
Chris@0 152 {
Chris@12 153 $columnIndex = (int) $columnIndex;
Chris@0 154
Chris@0 155 $this->columnStyles[$columnIndex] = $this->resolveStyle($name);
Chris@0 156
Chris@0 157 return $this;
Chris@0 158 }
Chris@0 159
Chris@0 160 /**
Chris@0 161 * Gets the current style for a column.
Chris@0 162 *
Chris@0 163 * If style was not set, it returns the global table style.
Chris@0 164 *
Chris@0 165 * @param int $columnIndex Column index
Chris@0 166 *
Chris@0 167 * @return TableStyle
Chris@0 168 */
Chris@0 169 public function getColumnStyle($columnIndex)
Chris@0 170 {
Chris@0 171 if (isset($this->columnStyles[$columnIndex])) {
Chris@0 172 return $this->columnStyles[$columnIndex];
Chris@0 173 }
Chris@0 174
Chris@0 175 return $this->getStyle();
Chris@0 176 }
Chris@0 177
Chris@0 178 /**
Chris@0 179 * Sets the minimum width of a column.
Chris@0 180 *
Chris@0 181 * @param int $columnIndex Column index
Chris@0 182 * @param int $width Minimum column width in characters
Chris@0 183 *
Chris@0 184 * @return $this
Chris@0 185 */
Chris@0 186 public function setColumnWidth($columnIndex, $width)
Chris@0 187 {
Chris@12 188 $this->columnWidths[(int) $columnIndex] = (int) $width;
Chris@0 189
Chris@0 190 return $this;
Chris@0 191 }
Chris@0 192
Chris@0 193 /**
Chris@0 194 * Sets the minimum width of all columns.
Chris@0 195 *
Chris@0 196 * @param array $widths
Chris@0 197 *
Chris@0 198 * @return $this
Chris@0 199 */
Chris@0 200 public function setColumnWidths(array $widths)
Chris@0 201 {
Chris@17 202 $this->columnWidths = [];
Chris@0 203 foreach ($widths as $index => $width) {
Chris@0 204 $this->setColumnWidth($index, $width);
Chris@0 205 }
Chris@0 206
Chris@0 207 return $this;
Chris@0 208 }
Chris@0 209
Chris@0 210 public function setHeaders(array $headers)
Chris@0 211 {
Chris@0 212 $headers = array_values($headers);
Chris@17 213 if (!empty($headers) && !\is_array($headers[0])) {
Chris@17 214 $headers = [$headers];
Chris@0 215 }
Chris@0 216
Chris@0 217 $this->headers = $headers;
Chris@0 218
Chris@0 219 return $this;
Chris@0 220 }
Chris@0 221
Chris@0 222 public function setRows(array $rows)
Chris@0 223 {
Chris@17 224 $this->rows = [];
Chris@0 225
Chris@0 226 return $this->addRows($rows);
Chris@0 227 }
Chris@0 228
Chris@0 229 public function addRows(array $rows)
Chris@0 230 {
Chris@0 231 foreach ($rows as $row) {
Chris@0 232 $this->addRow($row);
Chris@0 233 }
Chris@0 234
Chris@0 235 return $this;
Chris@0 236 }
Chris@0 237
Chris@0 238 public function addRow($row)
Chris@0 239 {
Chris@0 240 if ($row instanceof TableSeparator) {
Chris@0 241 $this->rows[] = $row;
Chris@0 242
Chris@0 243 return $this;
Chris@0 244 }
Chris@0 245
Chris@17 246 if (!\is_array($row)) {
Chris@0 247 throw new InvalidArgumentException('A row must be an array or a TableSeparator instance.');
Chris@0 248 }
Chris@0 249
Chris@0 250 $this->rows[] = array_values($row);
Chris@0 251
Chris@0 252 return $this;
Chris@0 253 }
Chris@0 254
Chris@0 255 public function setRow($column, array $row)
Chris@0 256 {
Chris@0 257 $this->rows[$column] = $row;
Chris@0 258
Chris@0 259 return $this;
Chris@0 260 }
Chris@0 261
Chris@0 262 /**
Chris@0 263 * Renders table to output.
Chris@0 264 *
Chris@0 265 * Example:
Chris@17 266 *
Chris@17 267 * +---------------+-----------------------+------------------+
Chris@17 268 * | ISBN | Title | Author |
Chris@17 269 * +---------------+-----------------------+------------------+
Chris@17 270 * | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
Chris@17 271 * | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
Chris@17 272 * | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
Chris@17 273 * +---------------+-----------------------+------------------+
Chris@0 274 */
Chris@0 275 public function render()
Chris@0 276 {
Chris@0 277 $this->calculateNumberOfColumns();
Chris@0 278 $rows = $this->buildTableRows($this->rows);
Chris@0 279 $headers = $this->buildTableRows($this->headers);
Chris@0 280
Chris@0 281 $this->calculateColumnsWidth(array_merge($headers, $rows));
Chris@0 282
Chris@0 283 $this->renderRowSeparator();
Chris@0 284 if (!empty($headers)) {
Chris@0 285 foreach ($headers as $header) {
Chris@0 286 $this->renderRow($header, $this->style->getCellHeaderFormat());
Chris@0 287 $this->renderRowSeparator();
Chris@0 288 }
Chris@0 289 }
Chris@0 290 foreach ($rows as $row) {
Chris@0 291 if ($row instanceof TableSeparator) {
Chris@0 292 $this->renderRowSeparator();
Chris@0 293 } else {
Chris@0 294 $this->renderRow($row, $this->style->getCellRowFormat());
Chris@0 295 }
Chris@0 296 }
Chris@0 297 if (!empty($rows)) {
Chris@0 298 $this->renderRowSeparator();
Chris@0 299 }
Chris@0 300
Chris@0 301 $this->cleanup();
Chris@0 302 }
Chris@0 303
Chris@0 304 /**
Chris@0 305 * Renders horizontal header separator.
Chris@0 306 *
Chris@17 307 * Example:
Chris@17 308 *
Chris@17 309 * +-----+-----------+-------+
Chris@0 310 */
Chris@0 311 private function renderRowSeparator()
Chris@0 312 {
Chris@0 313 if (0 === $count = $this->numberOfColumns) {
Chris@0 314 return;
Chris@0 315 }
Chris@0 316
Chris@0 317 if (!$this->style->getHorizontalBorderChar() && !$this->style->getCrossingChar()) {
Chris@0 318 return;
Chris@0 319 }
Chris@0 320
Chris@0 321 $markup = $this->style->getCrossingChar();
Chris@0 322 for ($column = 0; $column < $count; ++$column) {
Chris@0 323 $markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]).$this->style->getCrossingChar();
Chris@0 324 }
Chris@0 325
Chris@0 326 $this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
Chris@0 327 }
Chris@0 328
Chris@0 329 /**
Chris@0 330 * Renders vertical column separator.
Chris@0 331 */
Chris@0 332 private function renderColumnSeparator()
Chris@0 333 {
Chris@0 334 return sprintf($this->style->getBorderFormat(), $this->style->getVerticalBorderChar());
Chris@0 335 }
Chris@0 336
Chris@0 337 /**
Chris@0 338 * Renders table row.
Chris@0 339 *
Chris@17 340 * Example:
Chris@17 341 *
Chris@17 342 * | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
Chris@0 343 *
Chris@0 344 * @param array $row
Chris@0 345 * @param string $cellFormat
Chris@0 346 */
Chris@0 347 private function renderRow(array $row, $cellFormat)
Chris@0 348 {
Chris@0 349 if (empty($row)) {
Chris@0 350 return;
Chris@0 351 }
Chris@0 352
Chris@0 353 $rowContent = $this->renderColumnSeparator();
Chris@0 354 foreach ($this->getRowColumns($row) as $column) {
Chris@0 355 $rowContent .= $this->renderCell($row, $column, $cellFormat);
Chris@0 356 $rowContent .= $this->renderColumnSeparator();
Chris@0 357 }
Chris@0 358 $this->output->writeln($rowContent);
Chris@0 359 }
Chris@0 360
Chris@0 361 /**
Chris@0 362 * Renders table cell with padding.
Chris@0 363 *
Chris@0 364 * @param array $row
Chris@0 365 * @param int $column
Chris@0 366 * @param string $cellFormat
Chris@0 367 */
Chris@0 368 private function renderCell(array $row, $column, $cellFormat)
Chris@0 369 {
Chris@0 370 $cell = isset($row[$column]) ? $row[$column] : '';
Chris@0 371 $width = $this->effectiveColumnWidths[$column];
Chris@0 372 if ($cell instanceof TableCell && $cell->getColspan() > 1) {
Chris@0 373 // add the width of the following columns(numbers of colspan).
Chris@0 374 foreach (range($column + 1, $column + $cell->getColspan() - 1) as $nextColumn) {
Chris@0 375 $width += $this->getColumnSeparatorWidth() + $this->effectiveColumnWidths[$nextColumn];
Chris@0 376 }
Chris@0 377 }
Chris@0 378
Chris@0 379 // str_pad won't work properly with multi-byte strings, we need to fix the padding
Chris@0 380 if (false !== $encoding = mb_detect_encoding($cell, null, true)) {
Chris@17 381 $width += \strlen($cell) - mb_strwidth($cell, $encoding);
Chris@0 382 }
Chris@0 383
Chris@0 384 $style = $this->getColumnStyle($column);
Chris@0 385
Chris@0 386 if ($cell instanceof TableSeparator) {
Chris@0 387 return sprintf($style->getBorderFormat(), str_repeat($style->getHorizontalBorderChar(), $width));
Chris@0 388 }
Chris@0 389
Chris@0 390 $width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
Chris@0 391 $content = sprintf($style->getCellRowContentFormat(), $cell);
Chris@0 392
Chris@0 393 return sprintf($cellFormat, str_pad($content, $width, $style->getPaddingChar(), $style->getPadType()));
Chris@0 394 }
Chris@0 395
Chris@0 396 /**
Chris@0 397 * Calculate number of columns for this table.
Chris@0 398 */
Chris@0 399 private function calculateNumberOfColumns()
Chris@0 400 {
Chris@0 401 if (null !== $this->numberOfColumns) {
Chris@0 402 return;
Chris@0 403 }
Chris@0 404
Chris@17 405 $columns = [0];
Chris@0 406 foreach (array_merge($this->headers, $this->rows) as $row) {
Chris@0 407 if ($row instanceof TableSeparator) {
Chris@0 408 continue;
Chris@0 409 }
Chris@0 410
Chris@0 411 $columns[] = $this->getNumberOfColumns($row);
Chris@0 412 }
Chris@0 413
Chris@0 414 $this->numberOfColumns = max($columns);
Chris@0 415 }
Chris@0 416
Chris@0 417 private function buildTableRows($rows)
Chris@0 418 {
Chris@17 419 $unmergedRows = [];
Chris@17 420 for ($rowKey = 0; $rowKey < \count($rows); ++$rowKey) {
Chris@0 421 $rows = $this->fillNextRows($rows, $rowKey);
Chris@0 422
Chris@0 423 // Remove any new line breaks and replace it with a new line
Chris@0 424 foreach ($rows[$rowKey] as $column => $cell) {
Chris@0 425 if (!strstr($cell, "\n")) {
Chris@0 426 continue;
Chris@0 427 }
Chris@0 428 $lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
Chris@0 429 foreach ($lines as $lineKey => $line) {
Chris@0 430 if ($cell instanceof TableCell) {
Chris@17 431 $line = new TableCell($line, ['colspan' => $cell->getColspan()]);
Chris@0 432 }
Chris@0 433 if (0 === $lineKey) {
Chris@0 434 $rows[$rowKey][$column] = $line;
Chris@0 435 } else {
Chris@0 436 $unmergedRows[$rowKey][$lineKey][$column] = $line;
Chris@0 437 }
Chris@0 438 }
Chris@0 439 }
Chris@0 440 }
Chris@0 441
Chris@17 442 $tableRows = [];
Chris@0 443 foreach ($rows as $rowKey => $row) {
Chris@0 444 $tableRows[] = $this->fillCells($row);
Chris@0 445 if (isset($unmergedRows[$rowKey])) {
Chris@0 446 $tableRows = array_merge($tableRows, $unmergedRows[$rowKey]);
Chris@0 447 }
Chris@0 448 }
Chris@0 449
Chris@0 450 return $tableRows;
Chris@0 451 }
Chris@0 452
Chris@0 453 /**
Chris@0 454 * fill rows that contains rowspan > 1.
Chris@0 455 *
Chris@0 456 * @param array $rows
Chris@0 457 * @param int $line
Chris@0 458 *
Chris@0 459 * @return array
Chris@14 460 *
Chris@14 461 * @throws InvalidArgumentException
Chris@0 462 */
Chris@14 463 private function fillNextRows(array $rows, $line)
Chris@0 464 {
Chris@17 465 $unmergedRows = [];
Chris@0 466 foreach ($rows[$line] as $column => $cell) {
Chris@17 467 if (null !== $cell && !$cell instanceof TableCell && !is_scalar($cell) && !(\is_object($cell) && method_exists($cell, '__toString'))) {
Chris@17 468 throw new InvalidArgumentException(sprintf('A cell must be a TableCell, a scalar or an object implementing __toString, %s given.', \gettype($cell)));
Chris@14 469 }
Chris@0 470 if ($cell instanceof TableCell && $cell->getRowspan() > 1) {
Chris@0 471 $nbLines = $cell->getRowspan() - 1;
Chris@17 472 $lines = [$cell];
Chris@0 473 if (strstr($cell, "\n")) {
Chris@0 474 $lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
Chris@17 475 $nbLines = \count($lines) > $nbLines ? substr_count($cell, "\n") : $nbLines;
Chris@0 476
Chris@17 477 $rows[$line][$column] = new TableCell($lines[0], ['colspan' => $cell->getColspan()]);
Chris@0 478 unset($lines[0]);
Chris@0 479 }
Chris@0 480
Chris@0 481 // create a two dimensional array (rowspan x colspan)
Chris@17 482 $unmergedRows = array_replace_recursive(array_fill($line + 1, $nbLines, []), $unmergedRows);
Chris@0 483 foreach ($unmergedRows as $unmergedRowKey => $unmergedRow) {
Chris@0 484 $value = isset($lines[$unmergedRowKey - $line]) ? $lines[$unmergedRowKey - $line] : '';
Chris@17 485 $unmergedRows[$unmergedRowKey][$column] = new TableCell($value, ['colspan' => $cell->getColspan()]);
Chris@0 486 if ($nbLines === $unmergedRowKey - $line) {
Chris@0 487 break;
Chris@0 488 }
Chris@0 489 }
Chris@0 490 }
Chris@0 491 }
Chris@0 492
Chris@0 493 foreach ($unmergedRows as $unmergedRowKey => $unmergedRow) {
Chris@0 494 // we need to know if $unmergedRow will be merged or inserted into $rows
Chris@17 495 if (isset($rows[$unmergedRowKey]) && \is_array($rows[$unmergedRowKey]) && ($this->getNumberOfColumns($rows[$unmergedRowKey]) + $this->getNumberOfColumns($unmergedRows[$unmergedRowKey]) <= $this->numberOfColumns)) {
Chris@0 496 foreach ($unmergedRow as $cellKey => $cell) {
Chris@0 497 // insert cell into row at cellKey position
Chris@17 498 array_splice($rows[$unmergedRowKey], $cellKey, 0, [$cell]);
Chris@0 499 }
Chris@0 500 } else {
Chris@0 501 $row = $this->copyRow($rows, $unmergedRowKey - 1);
Chris@0 502 foreach ($unmergedRow as $column => $cell) {
Chris@0 503 if (!empty($cell)) {
Chris@0 504 $row[$column] = $unmergedRow[$column];
Chris@0 505 }
Chris@0 506 }
Chris@17 507 array_splice($rows, $unmergedRowKey, 0, [$row]);
Chris@0 508 }
Chris@0 509 }
Chris@0 510
Chris@0 511 return $rows;
Chris@0 512 }
Chris@0 513
Chris@0 514 /**
Chris@0 515 * fill cells for a row that contains colspan > 1.
Chris@0 516 *
Chris@0 517 * @return array
Chris@0 518 */
Chris@0 519 private function fillCells($row)
Chris@0 520 {
Chris@17 521 $newRow = [];
Chris@0 522 foreach ($row as $column => $cell) {
Chris@0 523 $newRow[] = $cell;
Chris@0 524 if ($cell instanceof TableCell && $cell->getColspan() > 1) {
Chris@0 525 foreach (range($column + 1, $column + $cell->getColspan() - 1) as $position) {
Chris@0 526 // insert empty value at column position
Chris@0 527 $newRow[] = '';
Chris@0 528 }
Chris@0 529 }
Chris@0 530 }
Chris@0 531
Chris@0 532 return $newRow ?: $row;
Chris@0 533 }
Chris@0 534
Chris@0 535 /**
Chris@0 536 * @param array $rows
Chris@0 537 * @param int $line
Chris@0 538 *
Chris@0 539 * @return array
Chris@0 540 */
Chris@14 541 private function copyRow(array $rows, $line)
Chris@0 542 {
Chris@0 543 $row = $rows[$line];
Chris@0 544 foreach ($row as $cellKey => $cellValue) {
Chris@0 545 $row[$cellKey] = '';
Chris@0 546 if ($cellValue instanceof TableCell) {
Chris@17 547 $row[$cellKey] = new TableCell('', ['colspan' => $cellValue->getColspan()]);
Chris@0 548 }
Chris@0 549 }
Chris@0 550
Chris@0 551 return $row;
Chris@0 552 }
Chris@0 553
Chris@0 554 /**
Chris@0 555 * Gets number of columns by row.
Chris@0 556 *
Chris@0 557 * @return int
Chris@0 558 */
Chris@0 559 private function getNumberOfColumns(array $row)
Chris@0 560 {
Chris@17 561 $columns = \count($row);
Chris@0 562 foreach ($row as $column) {
Chris@0 563 $columns += $column instanceof TableCell ? ($column->getColspan() - 1) : 0;
Chris@0 564 }
Chris@0 565
Chris@0 566 return $columns;
Chris@0 567 }
Chris@0 568
Chris@0 569 /**
Chris@0 570 * Gets list of columns for the given row.
Chris@0 571 *
Chris@0 572 * @return array
Chris@0 573 */
Chris@14 574 private function getRowColumns(array $row)
Chris@0 575 {
Chris@0 576 $columns = range(0, $this->numberOfColumns - 1);
Chris@0 577 foreach ($row as $cellKey => $cell) {
Chris@0 578 if ($cell instanceof TableCell && $cell->getColspan() > 1) {
Chris@0 579 // exclude grouped columns.
Chris@0 580 $columns = array_diff($columns, range($cellKey + 1, $cellKey + $cell->getColspan() - 1));
Chris@0 581 }
Chris@0 582 }
Chris@0 583
Chris@0 584 return $columns;
Chris@0 585 }
Chris@0 586
Chris@0 587 /**
Chris@0 588 * Calculates columns widths.
Chris@0 589 */
Chris@14 590 private function calculateColumnsWidth(array $rows)
Chris@0 591 {
Chris@0 592 for ($column = 0; $column < $this->numberOfColumns; ++$column) {
Chris@17 593 $lengths = [];
Chris@0 594 foreach ($rows as $row) {
Chris@0 595 if ($row instanceof TableSeparator) {
Chris@0 596 continue;
Chris@0 597 }
Chris@0 598
Chris@0 599 foreach ($row as $i => $cell) {
Chris@0 600 if ($cell instanceof TableCell) {
Chris@0 601 $textContent = Helper::removeDecoration($this->output->getFormatter(), $cell);
Chris@0 602 $textLength = Helper::strlen($textContent);
Chris@0 603 if ($textLength > 0) {
Chris@0 604 $contentColumns = str_split($textContent, ceil($textLength / $cell->getColspan()));
Chris@0 605 foreach ($contentColumns as $position => $content) {
Chris@0 606 $row[$i + $position] = $content;
Chris@0 607 }
Chris@0 608 }
Chris@0 609 }
Chris@0 610 }
Chris@0 611
Chris@0 612 $lengths[] = $this->getCellWidth($row, $column);
Chris@0 613 }
Chris@0 614
Chris@17 615 $this->effectiveColumnWidths[$column] = max($lengths) + Helper::strlen($this->style->getCellRowContentFormat()) - 2;
Chris@0 616 }
Chris@0 617 }
Chris@0 618
Chris@0 619 /**
Chris@0 620 * Gets column width.
Chris@0 621 *
Chris@0 622 * @return int
Chris@0 623 */
Chris@0 624 private function getColumnSeparatorWidth()
Chris@0 625 {
Chris@17 626 return Helper::strlen(sprintf($this->style->getBorderFormat(), $this->style->getVerticalBorderChar()));
Chris@0 627 }
Chris@0 628
Chris@0 629 /**
Chris@0 630 * Gets cell width.
Chris@0 631 *
Chris@0 632 * @param array $row
Chris@0 633 * @param int $column
Chris@0 634 *
Chris@0 635 * @return int
Chris@0 636 */
Chris@0 637 private function getCellWidth(array $row, $column)
Chris@0 638 {
Chris@0 639 $cellWidth = 0;
Chris@0 640
Chris@0 641 if (isset($row[$column])) {
Chris@0 642 $cell = $row[$column];
Chris@0 643 $cellWidth = Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
Chris@0 644 }
Chris@0 645
Chris@0 646 $columnWidth = isset($this->columnWidths[$column]) ? $this->columnWidths[$column] : 0;
Chris@0 647
Chris@0 648 return max($cellWidth, $columnWidth);
Chris@0 649 }
Chris@0 650
Chris@0 651 /**
Chris@0 652 * Called after rendering to cleanup cache data.
Chris@0 653 */
Chris@0 654 private function cleanup()
Chris@0 655 {
Chris@17 656 $this->effectiveColumnWidths = [];
Chris@0 657 $this->numberOfColumns = null;
Chris@0 658 }
Chris@0 659
Chris@0 660 private static function initStyles()
Chris@0 661 {
Chris@0 662 $borderless = new TableStyle();
Chris@0 663 $borderless
Chris@0 664 ->setHorizontalBorderChar('=')
Chris@0 665 ->setVerticalBorderChar(' ')
Chris@0 666 ->setCrossingChar(' ')
Chris@0 667 ;
Chris@0 668
Chris@0 669 $compact = new TableStyle();
Chris@0 670 $compact
Chris@0 671 ->setHorizontalBorderChar('')
Chris@0 672 ->setVerticalBorderChar(' ')
Chris@0 673 ->setCrossingChar('')
Chris@0 674 ->setCellRowContentFormat('%s')
Chris@0 675 ;
Chris@0 676
Chris@0 677 $styleGuide = new TableStyle();
Chris@0 678 $styleGuide
Chris@0 679 ->setHorizontalBorderChar('-')
Chris@0 680 ->setVerticalBorderChar(' ')
Chris@0 681 ->setCrossingChar(' ')
Chris@0 682 ->setCellHeaderFormat('%s')
Chris@0 683 ;
Chris@0 684
Chris@17 685 return [
Chris@0 686 'default' => new TableStyle(),
Chris@0 687 'borderless' => $borderless,
Chris@0 688 'compact' => $compact,
Chris@0 689 'symfony-style-guide' => $styleGuide,
Chris@17 690 ];
Chris@0 691 }
Chris@0 692
Chris@0 693 private function resolveStyle($name)
Chris@0 694 {
Chris@0 695 if ($name instanceof TableStyle) {
Chris@0 696 return $name;
Chris@0 697 }
Chris@0 698
Chris@0 699 if (isset(self::$styles[$name])) {
Chris@0 700 return self::$styles[$name];
Chris@0 701 }
Chris@0 702
Chris@0 703 throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name));
Chris@0 704 }
Chris@0 705 }