12 namespace Symfony\Component\Console\Helper;
70 self::$styles = self::initStyles();
85 self::$styles = self::initStyles();
88 self::$styles[$name] =
$style;
100 if (!self::$styles) {
101 self::$styles = self::initStyles();
104 if (!self::$styles[$name]) {
105 throw new \InvalidArgumentException(sprintf(
'Style "%s" is not defined.', $name));
108 return self::$styles[$name];
121 $this->style = $name;
122 }
elseif (isset(self::$styles[$name])) {
123 $this->style = self::$styles[$name];
125 throw new \InvalidArgumentException(sprintf(
'Style "%s" is not defined.', $name));
143 $headers = array_values($headers);
144 if (!empty($headers) && !is_array($headers[0])) {
145 $headers = array($headers);
155 $this->rows = array();
162 foreach ($rows as $row) {
172 $this->rows[] = $row;
177 if (!is_array($row)) {
178 throw new \InvalidArgumentException(
'A row must be an array or a TableSeparator instance.');
181 $this->rows[] = array_values($row);
186 public function setRow($column, array $row)
188 $this->rows[$column] = $row;
212 if (!empty($this->headers)) {
213 foreach ($this->headers as $header) {
214 $this->
renderRow($header, $this->style->getCellHeaderFormat());
218 foreach ($this->rows as $row) {
222 $this->
renderRow($row, $this->style->getCellRowFormat());
225 if (!empty($this->rows)) {
239 if (0 === $count = $this->numberOfColumns) {
243 if (!$this->style->getHorizontalBorderChar() && !$this->style->getCrossingChar()) {
247 $markup = $this->style->getCrossingChar();
248 for ($column = 0; $column < $count; ++$column) {
249 $markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->
getColumnWidth($column)).$this->style->getCrossingChar();
252 $this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
260 $this->output->write(sprintf($this->style->getBorderFormat(), $this->style->getVerticalBorderChar()));
279 $this->
renderCell($row, $column, $cellFormat);
282 $this->output->writeln(
'');
292 private function renderCell(array $row, $column, $cellFormat)
294 $cell = isset($row[$column]) ? $row[$column] :
'';
298 foreach (range($column + 1, $column + $cell->getColspan() - 1) as $nextColumn) {
304 if (function_exists(
'mb_strwidth') &&
false !== $encoding = mb_detect_encoding($cell)) {
305 $width += strlen($cell) - mb_strwidth($cell, $encoding);
309 $this->output->write(sprintf($this->style->getBorderFormat(), str_repeat($this->style->getHorizontalBorderChar(), $width)));
312 $content = sprintf($this->style->getCellRowContentFormat(), $cell);
313 $this->output->write(sprintf($cellFormat, str_pad($content, $width, $this->style->getPaddingChar(), $this->style->getPadType())));
322 if (null !== $this->numberOfColumns) {
327 foreach (array_merge($this->headers, $this->rows) as $row) {
335 return $this->numberOfColumns = max($columns);
340 $unmergedRows = array();
341 for ($rowKey = 0; $rowKey < count(
$rows); ++$rowKey) {
345 foreach (
$rows[$rowKey] as $column => $cell) {
347 if (!strstr($cell,
"\n")) {
350 $lines = explode(
"\n", $cell);
351 foreach ($lines as $lineKey => $line) {
353 $line =
new TableCell($line, array(
'colspan' => $cell->getColspan()));
355 if (0 === $lineKey) {
356 $rows[$rowKey][$column] = $line;
358 $unmergedRows[$rowKey][$lineKey][$column] = $line;
364 $tableRows = array();
365 foreach (
$rows as $rowKey => $row) {
367 if (isset($unmergedRows[$rowKey])) {
368 $tableRows = array_merge($tableRows, $unmergedRows[$rowKey]);
385 $unmergedRows = array();
386 foreach (
$rows[$line] as $column => $cell) {
388 $nbLines = $cell->getRowspan() - 1;
389 $lines = array($cell);
390 if (strstr($cell,
"\n")) {
391 $lines = explode(
"\n", $cell);
392 $nbLines = count($lines) > $nbLines ? substr_count($cell,
"\n") : $nbLines;
394 $rows[$line][$column] =
new TableCell($lines[0], array(
'colspan' => $cell->getColspan()));
399 $unmergedRows = array_replace_recursive(array_fill($line + 1, $nbLines,
''), $unmergedRows);
400 foreach ($unmergedRows as $unmergedRowKey => $unmergedRow) {
401 $value = isset($lines[$unmergedRowKey - $line]) ? $lines[$unmergedRowKey - $line] :
'';
402 $unmergedRows[$unmergedRowKey][$column] =
new TableCell($value, array(
'colspan' => $cell->getColspan()));
407 foreach ($unmergedRows as $unmergedRowKey => $unmergedRow) {
410 foreach ($unmergedRow as $cellKey => $cell) {
412 array_splice(
$rows[$unmergedRowKey], $cellKey, 0, array($cell));
416 foreach ($unmergedRow as $column => $cell) {
418 $row[$column] = $unmergedRow[$column];
421 array_splice(
$rows, $unmergedRowKey, 0, array($row));
438 $cell = $row[$column];
440 foreach (range($column + 1, $column + $cell->getColspan() - 1) as $position) {
442 array_splice($row, $position, 0,
'');
458 foreach ($row as $cellKey => $cellValue) {
461 $row[$cellKey] =
new TableCell(
'', array(
'colspan' => $cellValue->getColspan()));
477 $columns = count($row);
478 foreach ($row as $column) {
479 $columns += $column instanceof
TableCell ? ($column->getColspan() - 1) : 0;
494 $columns = range(0, $this->numberOfColumns - 1);
495 foreach ($row as $cellKey => $cell) {
498 $columns = array_diff($columns, range($cellKey + 1, $cellKey + $cell->getColspan() - 1));
514 if (isset($this->columnWidths[$column])) {
515 return $this->columnWidths[$column];
518 foreach (array_merge($this->headers, $this->rows) as $row) {
526 return $this->columnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2;
538 return strlen(sprintf($this->style->getBorderFormat(), $this->style->getVerticalBorderChar()));
551 if (isset($row[$column])) {
552 $cell = $row[$column];
556 $cellWidth = $cellWidth / $cell->getColspan();
570 $this->columnWidths = array();
571 $this->numberOfColumns = null;
578 ->setHorizontalBorderChar(
'=')
579 ->setVerticalBorderChar(
' ')
580 ->setCrossingChar(
' ')
585 ->setHorizontalBorderChar(
'')
586 ->setVerticalBorderChar(
' ')
587 ->setCrossingChar(
'')
588 ->setCellRowContentFormat(
'%s')
593 ->setHorizontalBorderChar(
'-')
594 ->setVerticalBorderChar(
' ')
595 ->setCrossingChar(
' ')
596 ->setCellHeaderFormat(
'%s')
601 'borderless' => $borderless,
602 'compact' => $compact,
603 'symfony-style-guide' => $styleGuide,