TYPO3  7.6
Table.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Helper;
13 
15 
23 class Table
24 {
30  private $headers = array();
31 
37  private $rows = array();
38 
44  private $columnWidths = array();
45 
52 
56  private $output;
57 
61  private $style;
62 
63  private static $styles;
64 
66  {
67  $this->output = $output;
68 
69  if (!self::$styles) {
70  self::$styles = self::initStyles();
71  }
72 
73  $this->setStyle('default');
74  }
75 
82  public static function setStyleDefinition($name, TableStyle $style)
83  {
84  if (!self::$styles) {
85  self::$styles = self::initStyles();
86  }
87 
88  self::$styles[$name] = $style;
89  }
90 
98  public static function getStyleDefinition($name)
99  {
100  if (!self::$styles) {
101  self::$styles = self::initStyles();
102  }
103 
104  if (!self::$styles[$name]) {
105  throw new \InvalidArgumentException(sprintf('Style "%s" is not defined.', $name));
106  }
107 
108  return self::$styles[$name];
109  }
110 
118  public function setStyle($name)
119  {
120  if ($name instanceof TableStyle) {
121  $this->style = $name;
122  } elseif (isset(self::$styles[$name])) {
123  $this->style = self::$styles[$name];
124  } else {
125  throw new \InvalidArgumentException(sprintf('Style "%s" is not defined.', $name));
126  }
127 
128  return $this;
129  }
130 
136  public function getStyle()
137  {
138  return $this->style;
139  }
140 
141  public function setHeaders(array $headers)
142  {
143  $headers = array_values($headers);
144  if (!empty($headers) && !is_array($headers[0])) {
145  $headers = array($headers);
146  }
147 
148  $this->headers = $headers;
149 
150  return $this;
151  }
152 
153  public function setRows(array $rows)
154  {
155  $this->rows = array();
156 
157  return $this->addRows($rows);
158  }
159 
160  public function addRows(array $rows)
161  {
162  foreach ($rows as $row) {
163  $this->addRow($row);
164  }
165 
166  return $this;
167  }
168 
169  public function addRow($row)
170  {
171  if ($row instanceof TableSeparator) {
172  $this->rows[] = $row;
173 
174  return $this;
175  }
176 
177  if (!is_array($row)) {
178  throw new \InvalidArgumentException('A row must be an array or a TableSeparator instance.');
179  }
180 
181  $this->rows[] = array_values($row);
182 
183  return $this;
184  }
185 
186  public function setRow($column, array $row)
187  {
188  $this->rows[$column] = $row;
189 
190  return $this;
191  }
192 
205  public function render()
206  {
207  $this->calculateNumberOfColumns();
208  $this->rows = $this->buildTableRows($this->rows);
209  $this->headers = $this->buildTableRows($this->headers);
210 
211  $this->renderRowSeparator();
212  if (!empty($this->headers)) {
213  foreach ($this->headers as $header) {
214  $this->renderRow($header, $this->style->getCellHeaderFormat());
215  $this->renderRowSeparator();
216  }
217  }
218  foreach ($this->rows as $row) {
219  if ($row instanceof TableSeparator) {
220  $this->renderRowSeparator();
221  } else {
222  $this->renderRow($row, $this->style->getCellRowFormat());
223  }
224  }
225  if (!empty($this->rows)) {
226  $this->renderRowSeparator();
227  }
228 
229  $this->cleanup();
230  }
231 
237  private function renderRowSeparator()
238  {
239  if (0 === $count = $this->numberOfColumns) {
240  return;
241  }
242 
243  if (!$this->style->getHorizontalBorderChar() && !$this->style->getCrossingChar()) {
244  return;
245  }
246 
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();
250  }
251 
252  $this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
253  }
254 
258  private function renderColumnSeparator()
259  {
260  $this->output->write(sprintf($this->style->getBorderFormat(), $this->style->getVerticalBorderChar()));
261  }
262 
271  private function renderRow(array $row, $cellFormat)
272  {
273  if (empty($row)) {
274  return;
275  }
276 
277  $this->renderColumnSeparator();
278  foreach ($this->getRowColumns($row) as $column) {
279  $this->renderCell($row, $column, $cellFormat);
280  $this->renderColumnSeparator();
281  }
282  $this->output->writeln('');
283  }
284 
292  private function renderCell(array $row, $column, $cellFormat)
293  {
294  $cell = isset($row[$column]) ? $row[$column] : '';
295  $width = $this->getColumnWidth($column);
296  if ($cell instanceof TableCell && $cell->getColspan() > 1) {
297  // add the width of the following columns(numbers of colspan).
298  foreach (range($column + 1, $column + $cell->getColspan() - 1) as $nextColumn) {
299  $width += $this->getColumnSeparatorWidth() + $this->getColumnWidth($nextColumn);
300  }
301  }
302 
303  // str_pad won't work properly with multi-byte strings, we need to fix the padding
304  if (function_exists('mb_strwidth') && false !== $encoding = mb_detect_encoding($cell)) {
305  $width += strlen($cell) - mb_strwidth($cell, $encoding);
306  }
307 
308  if ($cell instanceof TableSeparator) {
309  $this->output->write(sprintf($this->style->getBorderFormat(), str_repeat($this->style->getHorizontalBorderChar(), $width)));
310  } else {
311  $width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
312  $content = sprintf($this->style->getCellRowContentFormat(), $cell);
313  $this->output->write(sprintf($cellFormat, str_pad($content, $width, $this->style->getPaddingChar(), $this->style->getPadType())));
314  }
315  }
316 
320  private function calculateNumberOfColumns()
321  {
322  if (null !== $this->numberOfColumns) {
323  return;
324  }
325 
326  $columns = array(0);
327  foreach (array_merge($this->headers, $this->rows) as $row) {
328  if ($row instanceof TableSeparator) {
329  continue;
330  }
331 
332  $columns[] = $this->getNumberOfColumns($row);
333  }
334 
335  return $this->numberOfColumns = max($columns);
336  }
337 
338  private function buildTableRows($rows)
339  {
340  $unmergedRows = array();
341  for ($rowKey = 0; $rowKey < count($rows); ++$rowKey) {
342  $rows = $this->fillNextRows($rows, $rowKey);
343 
344  // Remove any new line breaks and replace it with a new line
345  foreach ($rows[$rowKey] as $column => $cell) {
346  $rows[$rowKey] = $this->fillCells($rows[$rowKey], $column);
347  if (!strstr($cell, "\n")) {
348  continue;
349  }
350  $lines = explode("\n", $cell);
351  foreach ($lines as $lineKey => $line) {
352  if ($cell instanceof TableCell) {
353  $line = new TableCell($line, array('colspan' => $cell->getColspan()));
354  }
355  if (0 === $lineKey) {
356  $rows[$rowKey][$column] = $line;
357  } else {
358  $unmergedRows[$rowKey][$lineKey][$column] = $line;
359  }
360  }
361  }
362  }
363 
364  $tableRows = array();
365  foreach ($rows as $rowKey => $row) {
366  $tableRows[] = $row;
367  if (isset($unmergedRows[$rowKey])) {
368  $tableRows = array_merge($tableRows, $unmergedRows[$rowKey]);
369  }
370  }
371 
372  return $tableRows;
373  }
374 
383  private function fillNextRows($rows, $line)
384  {
385  $unmergedRows = array();
386  foreach ($rows[$line] as $column => $cell) {
387  if ($cell instanceof TableCell && $cell->getRowspan() > 1) {
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;
393 
394  $rows[$line][$column] = new TableCell($lines[0], array('colspan' => $cell->getColspan()));
395  unset($lines[0]);
396  }
397 
398  // create a two dimensional array (rowspan x colspan)
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()));
403  }
404  }
405  }
406 
407  foreach ($unmergedRows as $unmergedRowKey => $unmergedRow) {
408  // we need to know if $unmergedRow will be merged or inserted into $rows
409  if (isset($rows[$unmergedRowKey]) && is_array($rows[$unmergedRowKey]) && ($this->getNumberOfColumns($rows[$unmergedRowKey]) + $this->getNumberOfColumns($unmergedRows[$unmergedRowKey]) <= $this->numberOfColumns)) {
410  foreach ($unmergedRow as $cellKey => $cell) {
411  // insert cell into row at cellKey position
412  array_splice($rows[$unmergedRowKey], $cellKey, 0, array($cell));
413  }
414  } else {
415  $row = $this->copyRow($rows, $unmergedRowKey - 1);
416  foreach ($unmergedRow as $column => $cell) {
417  if (!empty($cell)) {
418  $row[$column] = $unmergedRow[$column];
419  }
420  }
421  array_splice($rows, $unmergedRowKey, 0, array($row));
422  }
423  }
424 
425  return $rows;
426  }
427 
436  private function fillCells($row, $column)
437  {
438  $cell = $row[$column];
439  if ($cell instanceof TableCell && $cell->getColspan() > 1) {
440  foreach (range($column + 1, $column + $cell->getColspan() - 1) as $position) {
441  // insert empty value into rows at column position
442  array_splice($row, $position, 0, '');
443  }
444  }
445 
446  return $row;
447  }
448 
455  private function copyRow($rows, $line)
456  {
457  $row = $rows[$line];
458  foreach ($row as $cellKey => $cellValue) {
459  $row[$cellKey] = '';
460  if ($cellValue instanceof TableCell) {
461  $row[$cellKey] = new TableCell('', array('colspan' => $cellValue->getColspan()));
462  }
463  }
464 
465  return $row;
466  }
467 
475  private function getNumberOfColumns(array $row)
476  {
477  $columns = count($row);
478  foreach ($row as $column) {
479  $columns += $column instanceof TableCell ? ($column->getColspan() - 1) : 0;
480  }
481 
482  return $columns;
483  }
484 
492  private function getRowColumns($row)
493  {
494  $columns = range(0, $this->numberOfColumns - 1);
495  foreach ($row as $cellKey => $cell) {
496  if ($cell instanceof TableCell && $cell->getColspan() > 1) {
497  // exclude grouped columns.
498  $columns = array_diff($columns, range($cellKey + 1, $cellKey + $cell->getColspan() - 1));
499  }
500  }
501 
502  return $columns;
503  }
504 
512  private function getColumnWidth($column)
513  {
514  if (isset($this->columnWidths[$column])) {
515  return $this->columnWidths[$column];
516  }
517 
518  foreach (array_merge($this->headers, $this->rows) as $row) {
519  if ($row instanceof TableSeparator) {
520  continue;
521  }
522 
523  $lengths[] = $this->getCellWidth($row, $column);
524  }
525 
526  return $this->columnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2;
527  }
528 
536  private function getColumnSeparatorWidth()
537  {
538  return strlen(sprintf($this->style->getBorderFormat(), $this->style->getVerticalBorderChar()));
539  }
540 
549  private function getCellWidth(array $row, $column)
550  {
551  if (isset($row[$column])) {
552  $cell = $row[$column];
553  $cellWidth = Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
554  if ($cell instanceof TableCell && $cell->getColspan() > 1) {
555  // we assume that cell value will be across more than one column.
556  $cellWidth = $cellWidth / $cell->getColspan();
557  }
558 
559  return $cellWidth;
560  }
561 
562  return 0;
563  }
564 
568  private function cleanup()
569  {
570  $this->columnWidths = array();
571  $this->numberOfColumns = null;
572  }
573 
574  private static function initStyles()
575  {
576  $borderless = new TableStyle();
577  $borderless
578  ->setHorizontalBorderChar('=')
579  ->setVerticalBorderChar(' ')
580  ->setCrossingChar(' ')
581  ;
582 
583  $compact = new TableStyle();
584  $compact
585  ->setHorizontalBorderChar('')
586  ->setVerticalBorderChar(' ')
587  ->setCrossingChar('')
588  ->setCellRowContentFormat('%s')
589  ;
590 
591  $styleGuide = new TableStyle();
592  $styleGuide
593  ->setHorizontalBorderChar('-')
594  ->setVerticalBorderChar(' ')
595  ->setCrossingChar(' ')
596  ->setCellHeaderFormat('%s')
597  ;
598 
599  return array(
600  'default' => new TableStyle(),
601  'borderless' => $borderless,
602  'compact' => $compact,
603  'symfony-style-guide' => $styleGuide,
604  );
605  }
606 }