comparison vendor/symfony/console/Helper/Table.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
24 */ 24 */
25 class Table 25 class Table
26 { 26 {
27 /** 27 /**
28 * Table headers. 28 * Table headers.
29 *
30 * @var array
31 */ 29 */
32 private $headers = array(); 30 private $headers = array();
33 31
34 /** 32 /**
35 * Table rows. 33 * Table rows.
36 *
37 * @var array
38 */ 34 */
39 private $rows = array(); 35 private $rows = array();
40 36
41 /** 37 /**
42 * Column widths cache. 38 * Column widths cache.
43 *
44 * @var array
45 */ 39 */
46 private $effectiveColumnWidths = array(); 40 private $effectiveColumnWidths = array();
47 41
48 /** 42 /**
49 * Number of columns cache. 43 * Number of columns cache.
50 * 44 *
51 * @var array 45 * @var int
52 */ 46 */
53 private $numberOfColumns; 47 private $numberOfColumns;
54 48
55 /** 49 /**
56 * @var OutputInterface 50 * @var OutputInterface
267 261
268 /** 262 /**
269 * Renders table to output. 263 * Renders table to output.
270 * 264 *
271 * Example: 265 * Example:
266 * <code>
272 * +---------------+-----------------------+------------------+ 267 * +---------------+-----------------------+------------------+
273 * | ISBN | Title | Author | 268 * | ISBN | Title | Author |
274 * +---------------+-----------------------+------------------+ 269 * +---------------+-----------------------+------------------+
275 * | 99921-58-10-7 | Divine Comedy | Dante Alighieri | 270 * | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
276 * | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 271 * | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
277 * | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien | 272 * | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
278 * +---------------+-----------------------+------------------+ 273 * +---------------+-----------------------+------------------+
274 * </code>
279 */ 275 */
280 public function render() 276 public function render()
281 { 277 {
282 $this->calculateNumberOfColumns(); 278 $this->calculateNumberOfColumns();
283 $rows = $this->buildTableRows($this->rows); 279 $rows = $this->buildTableRows($this->rows);
307 } 303 }
308 304
309 /** 305 /**
310 * Renders horizontal header separator. 306 * Renders horizontal header separator.
311 * 307 *
312 * Example: +-----+-----------+-------+ 308 * Example: <code>+-----+-----------+-------+</code>
313 */ 309 */
314 private function renderRowSeparator() 310 private function renderRowSeparator()
315 { 311 {
316 if (0 === $count = $this->numberOfColumns) { 312 if (0 === $count = $this->numberOfColumns) {
317 return; 313 return;
338 } 334 }
339 335
340 /** 336 /**
341 * Renders table row. 337 * Renders table row.
342 * 338 *
343 * Example: | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 339 * Example: <code>| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |</code>
344 * 340 *
345 * @param array $row 341 * @param array $row
346 * @param string $cellFormat 342 * @param string $cellFormat
347 */ 343 */
348 private function renderRow(array $row, $cellFormat) 344 private function renderRow(array $row, $cellFormat)
456 * 452 *
457 * @param array $rows 453 * @param array $rows
458 * @param int $line 454 * @param int $line
459 * 455 *
460 * @return array 456 * @return array
461 */ 457 *
462 private function fillNextRows($rows, $line) 458 * @throws InvalidArgumentException
459 */
460 private function fillNextRows(array $rows, $line)
463 { 461 {
464 $unmergedRows = array(); 462 $unmergedRows = array();
465 foreach ($rows[$line] as $column => $cell) { 463 foreach ($rows[$line] as $column => $cell) {
464 if (null !== $cell && !$cell instanceof TableCell && !is_scalar($cell) && !(is_object($cell) && method_exists($cell, '__toString'))) {
465 throw new InvalidArgumentException(sprintf('A cell must be a TableCell, a scalar or an object implementing __toString, %s given.', gettype($cell)));
466 }
466 if ($cell instanceof TableCell && $cell->getRowspan() > 1) { 467 if ($cell instanceof TableCell && $cell->getRowspan() > 1) {
467 $nbLines = $cell->getRowspan() - 1; 468 $nbLines = $cell->getRowspan() - 1;
468 $lines = array($cell); 469 $lines = array($cell);
469 if (strstr($cell, "\n")) { 470 if (strstr($cell, "\n")) {
470 $lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell)); 471 $lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
508 } 509 }
509 510
510 /** 511 /**
511 * fill cells for a row that contains colspan > 1. 512 * fill cells for a row that contains colspan > 1.
512 * 513 *
513 * @param array $row
514 *
515 * @return array 514 * @return array
516 */ 515 */
517 private function fillCells($row) 516 private function fillCells($row)
518 { 517 {
519 $newRow = array(); 518 $newRow = array();
534 * @param array $rows 533 * @param array $rows
535 * @param int $line 534 * @param int $line
536 * 535 *
537 * @return array 536 * @return array
538 */ 537 */
539 private function copyRow($rows, $line) 538 private function copyRow(array $rows, $line)
540 { 539 {
541 $row = $rows[$line]; 540 $row = $rows[$line];
542 foreach ($row as $cellKey => $cellValue) { 541 foreach ($row as $cellKey => $cellValue) {
543 $row[$cellKey] = ''; 542 $row[$cellKey] = '';
544 if ($cellValue instanceof TableCell) { 543 if ($cellValue instanceof TableCell) {
550 } 549 }
551 550
552 /** 551 /**
553 * Gets number of columns by row. 552 * Gets number of columns by row.
554 * 553 *
555 * @param array $row
556 *
557 * @return int 554 * @return int
558 */ 555 */
559 private function getNumberOfColumns(array $row) 556 private function getNumberOfColumns(array $row)
560 { 557 {
561 $columns = count($row); 558 $columns = count($row);
567 } 564 }
568 565
569 /** 566 /**
570 * Gets list of columns for the given row. 567 * Gets list of columns for the given row.
571 * 568 *
572 * @param array $row
573 *
574 * @return array 569 * @return array
575 */ 570 */
576 private function getRowColumns($row) 571 private function getRowColumns(array $row)
577 { 572 {
578 $columns = range(0, $this->numberOfColumns - 1); 573 $columns = range(0, $this->numberOfColumns - 1);
579 foreach ($row as $cellKey => $cell) { 574 foreach ($row as $cellKey => $cell) {
580 if ($cell instanceof TableCell && $cell->getColspan() > 1) { 575 if ($cell instanceof TableCell && $cell->getColspan() > 1) {
581 // exclude grouped columns. 576 // exclude grouped columns.
586 return $columns; 581 return $columns;
587 } 582 }
588 583
589 /** 584 /**
590 * Calculates columns widths. 585 * Calculates columns widths.
591 * 586 */
592 * @param array $rows 587 private function calculateColumnsWidth(array $rows)
593 */
594 private function calculateColumnsWidth($rows)
595 { 588 {
596 for ($column = 0; $column < $this->numberOfColumns; ++$column) { 589 for ($column = 0; $column < $this->numberOfColumns; ++$column) {
597 $lengths = array(); 590 $lengths = array();
598 foreach ($rows as $row) { 591 foreach ($rows as $row) {
599 if ($row instanceof TableSeparator) { 592 if ($row instanceof TableSeparator) {