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@0
|
14 use Symfony\Component\Console\Output\OutputInterface;
|
Chris@0
|
15 use Symfony\Component\Console\Exception\InvalidArgumentException;
|
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@0
|
30 private $headers = array();
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Table rows.
|
Chris@0
|
34 */
|
Chris@0
|
35 private $rows = array();
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Column widths cache.
|
Chris@0
|
39 */
|
Chris@0
|
40 private $effectiveColumnWidths = array();
|
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@0
|
62 private $columnStyles = array();
|
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@0
|
69 private $columnWidths = array();
|
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@0
|
202 $this->columnWidths = array();
|
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@0
|
213 if (!empty($headers) && !is_array($headers[0])) {
|
Chris@0
|
214 $headers = array($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@0
|
224 $this->rows = array();
|
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@0
|
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@14
|
266 * <code>
|
Chris@0
|
267 * +---------------+-----------------------+------------------+
|
Chris@0
|
268 * | ISBN | Title | Author |
|
Chris@0
|
269 * +---------------+-----------------------+------------------+
|
Chris@0
|
270 * | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
|
Chris@0
|
271 * | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
|
Chris@0
|
272 * | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
|
Chris@0
|
273 * +---------------+-----------------------+------------------+
|
Chris@14
|
274 * </code>
|
Chris@0
|
275 */
|
Chris@0
|
276 public function render()
|
Chris@0
|
277 {
|
Chris@0
|
278 $this->calculateNumberOfColumns();
|
Chris@0
|
279 $rows = $this->buildTableRows($this->rows);
|
Chris@0
|
280 $headers = $this->buildTableRows($this->headers);
|
Chris@0
|
281
|
Chris@0
|
282 $this->calculateColumnsWidth(array_merge($headers, $rows));
|
Chris@0
|
283
|
Chris@0
|
284 $this->renderRowSeparator();
|
Chris@0
|
285 if (!empty($headers)) {
|
Chris@0
|
286 foreach ($headers as $header) {
|
Chris@0
|
287 $this->renderRow($header, $this->style->getCellHeaderFormat());
|
Chris@0
|
288 $this->renderRowSeparator();
|
Chris@0
|
289 }
|
Chris@0
|
290 }
|
Chris@0
|
291 foreach ($rows as $row) {
|
Chris@0
|
292 if ($row instanceof TableSeparator) {
|
Chris@0
|
293 $this->renderRowSeparator();
|
Chris@0
|
294 } else {
|
Chris@0
|
295 $this->renderRow($row, $this->style->getCellRowFormat());
|
Chris@0
|
296 }
|
Chris@0
|
297 }
|
Chris@0
|
298 if (!empty($rows)) {
|
Chris@0
|
299 $this->renderRowSeparator();
|
Chris@0
|
300 }
|
Chris@0
|
301
|
Chris@0
|
302 $this->cleanup();
|
Chris@0
|
303 }
|
Chris@0
|
304
|
Chris@0
|
305 /**
|
Chris@0
|
306 * Renders horizontal header separator.
|
Chris@0
|
307 *
|
Chris@14
|
308 * Example: <code>+-----+-----------+-------+</code>
|
Chris@0
|
309 */
|
Chris@0
|
310 private function renderRowSeparator()
|
Chris@0
|
311 {
|
Chris@0
|
312 if (0 === $count = $this->numberOfColumns) {
|
Chris@0
|
313 return;
|
Chris@0
|
314 }
|
Chris@0
|
315
|
Chris@0
|
316 if (!$this->style->getHorizontalBorderChar() && !$this->style->getCrossingChar()) {
|
Chris@0
|
317 return;
|
Chris@0
|
318 }
|
Chris@0
|
319
|
Chris@0
|
320 $markup = $this->style->getCrossingChar();
|
Chris@0
|
321 for ($column = 0; $column < $count; ++$column) {
|
Chris@0
|
322 $markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]).$this->style->getCrossingChar();
|
Chris@0
|
323 }
|
Chris@0
|
324
|
Chris@0
|
325 $this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
|
Chris@0
|
326 }
|
Chris@0
|
327
|
Chris@0
|
328 /**
|
Chris@0
|
329 * Renders vertical column separator.
|
Chris@0
|
330 */
|
Chris@0
|
331 private function renderColumnSeparator()
|
Chris@0
|
332 {
|
Chris@0
|
333 return sprintf($this->style->getBorderFormat(), $this->style->getVerticalBorderChar());
|
Chris@0
|
334 }
|
Chris@0
|
335
|
Chris@0
|
336 /**
|
Chris@0
|
337 * Renders table row.
|
Chris@0
|
338 *
|
Chris@14
|
339 * Example: <code>| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |</code>
|
Chris@0
|
340 *
|
Chris@0
|
341 * @param array $row
|
Chris@0
|
342 * @param string $cellFormat
|
Chris@0
|
343 */
|
Chris@0
|
344 private function renderRow(array $row, $cellFormat)
|
Chris@0
|
345 {
|
Chris@0
|
346 if (empty($row)) {
|
Chris@0
|
347 return;
|
Chris@0
|
348 }
|
Chris@0
|
349
|
Chris@0
|
350 $rowContent = $this->renderColumnSeparator();
|
Chris@0
|
351 foreach ($this->getRowColumns($row) as $column) {
|
Chris@0
|
352 $rowContent .= $this->renderCell($row, $column, $cellFormat);
|
Chris@0
|
353 $rowContent .= $this->renderColumnSeparator();
|
Chris@0
|
354 }
|
Chris@0
|
355 $this->output->writeln($rowContent);
|
Chris@0
|
356 }
|
Chris@0
|
357
|
Chris@0
|
358 /**
|
Chris@0
|
359 * Renders table cell with padding.
|
Chris@0
|
360 *
|
Chris@0
|
361 * @param array $row
|
Chris@0
|
362 * @param int $column
|
Chris@0
|
363 * @param string $cellFormat
|
Chris@0
|
364 */
|
Chris@0
|
365 private function renderCell(array $row, $column, $cellFormat)
|
Chris@0
|
366 {
|
Chris@0
|
367 $cell = isset($row[$column]) ? $row[$column] : '';
|
Chris@0
|
368 $width = $this->effectiveColumnWidths[$column];
|
Chris@0
|
369 if ($cell instanceof TableCell && $cell->getColspan() > 1) {
|
Chris@0
|
370 // add the width of the following columns(numbers of colspan).
|
Chris@0
|
371 foreach (range($column + 1, $column + $cell->getColspan() - 1) as $nextColumn) {
|
Chris@0
|
372 $width += $this->getColumnSeparatorWidth() + $this->effectiveColumnWidths[$nextColumn];
|
Chris@0
|
373 }
|
Chris@0
|
374 }
|
Chris@0
|
375
|
Chris@0
|
376 // str_pad won't work properly with multi-byte strings, we need to fix the padding
|
Chris@0
|
377 if (false !== $encoding = mb_detect_encoding($cell, null, true)) {
|
Chris@0
|
378 $width += strlen($cell) - mb_strwidth($cell, $encoding);
|
Chris@0
|
379 }
|
Chris@0
|
380
|
Chris@0
|
381 $style = $this->getColumnStyle($column);
|
Chris@0
|
382
|
Chris@0
|
383 if ($cell instanceof TableSeparator) {
|
Chris@0
|
384 return sprintf($style->getBorderFormat(), str_repeat($style->getHorizontalBorderChar(), $width));
|
Chris@0
|
385 }
|
Chris@0
|
386
|
Chris@0
|
387 $width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
|
Chris@0
|
388 $content = sprintf($style->getCellRowContentFormat(), $cell);
|
Chris@0
|
389
|
Chris@0
|
390 return sprintf($cellFormat, str_pad($content, $width, $style->getPaddingChar(), $style->getPadType()));
|
Chris@0
|
391 }
|
Chris@0
|
392
|
Chris@0
|
393 /**
|
Chris@0
|
394 * Calculate number of columns for this table.
|
Chris@0
|
395 */
|
Chris@0
|
396 private function calculateNumberOfColumns()
|
Chris@0
|
397 {
|
Chris@0
|
398 if (null !== $this->numberOfColumns) {
|
Chris@0
|
399 return;
|
Chris@0
|
400 }
|
Chris@0
|
401
|
Chris@0
|
402 $columns = array(0);
|
Chris@0
|
403 foreach (array_merge($this->headers, $this->rows) as $row) {
|
Chris@0
|
404 if ($row instanceof TableSeparator) {
|
Chris@0
|
405 continue;
|
Chris@0
|
406 }
|
Chris@0
|
407
|
Chris@0
|
408 $columns[] = $this->getNumberOfColumns($row);
|
Chris@0
|
409 }
|
Chris@0
|
410
|
Chris@0
|
411 $this->numberOfColumns = max($columns);
|
Chris@0
|
412 }
|
Chris@0
|
413
|
Chris@0
|
414 private function buildTableRows($rows)
|
Chris@0
|
415 {
|
Chris@0
|
416 $unmergedRows = array();
|
Chris@0
|
417 for ($rowKey = 0; $rowKey < count($rows); ++$rowKey) {
|
Chris@0
|
418 $rows = $this->fillNextRows($rows, $rowKey);
|
Chris@0
|
419
|
Chris@0
|
420 // Remove any new line breaks and replace it with a new line
|
Chris@0
|
421 foreach ($rows[$rowKey] as $column => $cell) {
|
Chris@0
|
422 if (!strstr($cell, "\n")) {
|
Chris@0
|
423 continue;
|
Chris@0
|
424 }
|
Chris@0
|
425 $lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
|
Chris@0
|
426 foreach ($lines as $lineKey => $line) {
|
Chris@0
|
427 if ($cell instanceof TableCell) {
|
Chris@0
|
428 $line = new TableCell($line, array('colspan' => $cell->getColspan()));
|
Chris@0
|
429 }
|
Chris@0
|
430 if (0 === $lineKey) {
|
Chris@0
|
431 $rows[$rowKey][$column] = $line;
|
Chris@0
|
432 } else {
|
Chris@0
|
433 $unmergedRows[$rowKey][$lineKey][$column] = $line;
|
Chris@0
|
434 }
|
Chris@0
|
435 }
|
Chris@0
|
436 }
|
Chris@0
|
437 }
|
Chris@0
|
438
|
Chris@0
|
439 $tableRows = array();
|
Chris@0
|
440 foreach ($rows as $rowKey => $row) {
|
Chris@0
|
441 $tableRows[] = $this->fillCells($row);
|
Chris@0
|
442 if (isset($unmergedRows[$rowKey])) {
|
Chris@0
|
443 $tableRows = array_merge($tableRows, $unmergedRows[$rowKey]);
|
Chris@0
|
444 }
|
Chris@0
|
445 }
|
Chris@0
|
446
|
Chris@0
|
447 return $tableRows;
|
Chris@0
|
448 }
|
Chris@0
|
449
|
Chris@0
|
450 /**
|
Chris@0
|
451 * fill rows that contains rowspan > 1.
|
Chris@0
|
452 *
|
Chris@0
|
453 * @param array $rows
|
Chris@0
|
454 * @param int $line
|
Chris@0
|
455 *
|
Chris@0
|
456 * @return array
|
Chris@14
|
457 *
|
Chris@14
|
458 * @throws InvalidArgumentException
|
Chris@0
|
459 */
|
Chris@14
|
460 private function fillNextRows(array $rows, $line)
|
Chris@0
|
461 {
|
Chris@0
|
462 $unmergedRows = array();
|
Chris@0
|
463 foreach ($rows[$line] as $column => $cell) {
|
Chris@14
|
464 if (null !== $cell && !$cell instanceof TableCell && !is_scalar($cell) && !(is_object($cell) && method_exists($cell, '__toString'))) {
|
Chris@14
|
465 throw new InvalidArgumentException(sprintf('A cell must be a TableCell, a scalar or an object implementing __toString, %s given.', gettype($cell)));
|
Chris@14
|
466 }
|
Chris@0
|
467 if ($cell instanceof TableCell && $cell->getRowspan() > 1) {
|
Chris@0
|
468 $nbLines = $cell->getRowspan() - 1;
|
Chris@0
|
469 $lines = array($cell);
|
Chris@0
|
470 if (strstr($cell, "\n")) {
|
Chris@0
|
471 $lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
|
Chris@0
|
472 $nbLines = count($lines) > $nbLines ? substr_count($cell, "\n") : $nbLines;
|
Chris@0
|
473
|
Chris@0
|
474 $rows[$line][$column] = new TableCell($lines[0], array('colspan' => $cell->getColspan()));
|
Chris@0
|
475 unset($lines[0]);
|
Chris@0
|
476 }
|
Chris@0
|
477
|
Chris@0
|
478 // create a two dimensional array (rowspan x colspan)
|
Chris@0
|
479 $unmergedRows = array_replace_recursive(array_fill($line + 1, $nbLines, array()), $unmergedRows);
|
Chris@0
|
480 foreach ($unmergedRows as $unmergedRowKey => $unmergedRow) {
|
Chris@0
|
481 $value = isset($lines[$unmergedRowKey - $line]) ? $lines[$unmergedRowKey - $line] : '';
|
Chris@0
|
482 $unmergedRows[$unmergedRowKey][$column] = new TableCell($value, array('colspan' => $cell->getColspan()));
|
Chris@0
|
483 if ($nbLines === $unmergedRowKey - $line) {
|
Chris@0
|
484 break;
|
Chris@0
|
485 }
|
Chris@0
|
486 }
|
Chris@0
|
487 }
|
Chris@0
|
488 }
|
Chris@0
|
489
|
Chris@0
|
490 foreach ($unmergedRows as $unmergedRowKey => $unmergedRow) {
|
Chris@0
|
491 // we need to know if $unmergedRow will be merged or inserted into $rows
|
Chris@0
|
492 if (isset($rows[$unmergedRowKey]) && is_array($rows[$unmergedRowKey]) && ($this->getNumberOfColumns($rows[$unmergedRowKey]) + $this->getNumberOfColumns($unmergedRows[$unmergedRowKey]) <= $this->numberOfColumns)) {
|
Chris@0
|
493 foreach ($unmergedRow as $cellKey => $cell) {
|
Chris@0
|
494 // insert cell into row at cellKey position
|
Chris@0
|
495 array_splice($rows[$unmergedRowKey], $cellKey, 0, array($cell));
|
Chris@0
|
496 }
|
Chris@0
|
497 } else {
|
Chris@0
|
498 $row = $this->copyRow($rows, $unmergedRowKey - 1);
|
Chris@0
|
499 foreach ($unmergedRow as $column => $cell) {
|
Chris@0
|
500 if (!empty($cell)) {
|
Chris@0
|
501 $row[$column] = $unmergedRow[$column];
|
Chris@0
|
502 }
|
Chris@0
|
503 }
|
Chris@0
|
504 array_splice($rows, $unmergedRowKey, 0, array($row));
|
Chris@0
|
505 }
|
Chris@0
|
506 }
|
Chris@0
|
507
|
Chris@0
|
508 return $rows;
|
Chris@0
|
509 }
|
Chris@0
|
510
|
Chris@0
|
511 /**
|
Chris@0
|
512 * fill cells for a row that contains colspan > 1.
|
Chris@0
|
513 *
|
Chris@0
|
514 * @return array
|
Chris@0
|
515 */
|
Chris@0
|
516 private function fillCells($row)
|
Chris@0
|
517 {
|
Chris@0
|
518 $newRow = array();
|
Chris@0
|
519 foreach ($row as $column => $cell) {
|
Chris@0
|
520 $newRow[] = $cell;
|
Chris@0
|
521 if ($cell instanceof TableCell && $cell->getColspan() > 1) {
|
Chris@0
|
522 foreach (range($column + 1, $column + $cell->getColspan() - 1) as $position) {
|
Chris@0
|
523 // insert empty value at column position
|
Chris@0
|
524 $newRow[] = '';
|
Chris@0
|
525 }
|
Chris@0
|
526 }
|
Chris@0
|
527 }
|
Chris@0
|
528
|
Chris@0
|
529 return $newRow ?: $row;
|
Chris@0
|
530 }
|
Chris@0
|
531
|
Chris@0
|
532 /**
|
Chris@0
|
533 * @param array $rows
|
Chris@0
|
534 * @param int $line
|
Chris@0
|
535 *
|
Chris@0
|
536 * @return array
|
Chris@0
|
537 */
|
Chris@14
|
538 private function copyRow(array $rows, $line)
|
Chris@0
|
539 {
|
Chris@0
|
540 $row = $rows[$line];
|
Chris@0
|
541 foreach ($row as $cellKey => $cellValue) {
|
Chris@0
|
542 $row[$cellKey] = '';
|
Chris@0
|
543 if ($cellValue instanceof TableCell) {
|
Chris@0
|
544 $row[$cellKey] = new TableCell('', array('colspan' => $cellValue->getColspan()));
|
Chris@0
|
545 }
|
Chris@0
|
546 }
|
Chris@0
|
547
|
Chris@0
|
548 return $row;
|
Chris@0
|
549 }
|
Chris@0
|
550
|
Chris@0
|
551 /**
|
Chris@0
|
552 * Gets number of columns by row.
|
Chris@0
|
553 *
|
Chris@0
|
554 * @return int
|
Chris@0
|
555 */
|
Chris@0
|
556 private function getNumberOfColumns(array $row)
|
Chris@0
|
557 {
|
Chris@0
|
558 $columns = count($row);
|
Chris@0
|
559 foreach ($row as $column) {
|
Chris@0
|
560 $columns += $column instanceof TableCell ? ($column->getColspan() - 1) : 0;
|
Chris@0
|
561 }
|
Chris@0
|
562
|
Chris@0
|
563 return $columns;
|
Chris@0
|
564 }
|
Chris@0
|
565
|
Chris@0
|
566 /**
|
Chris@0
|
567 * Gets list of columns for the given row.
|
Chris@0
|
568 *
|
Chris@0
|
569 * @return array
|
Chris@0
|
570 */
|
Chris@14
|
571 private function getRowColumns(array $row)
|
Chris@0
|
572 {
|
Chris@0
|
573 $columns = range(0, $this->numberOfColumns - 1);
|
Chris@0
|
574 foreach ($row as $cellKey => $cell) {
|
Chris@0
|
575 if ($cell instanceof TableCell && $cell->getColspan() > 1) {
|
Chris@0
|
576 // exclude grouped columns.
|
Chris@0
|
577 $columns = array_diff($columns, range($cellKey + 1, $cellKey + $cell->getColspan() - 1));
|
Chris@0
|
578 }
|
Chris@0
|
579 }
|
Chris@0
|
580
|
Chris@0
|
581 return $columns;
|
Chris@0
|
582 }
|
Chris@0
|
583
|
Chris@0
|
584 /**
|
Chris@0
|
585 * Calculates columns widths.
|
Chris@0
|
586 */
|
Chris@14
|
587 private function calculateColumnsWidth(array $rows)
|
Chris@0
|
588 {
|
Chris@0
|
589 for ($column = 0; $column < $this->numberOfColumns; ++$column) {
|
Chris@0
|
590 $lengths = array();
|
Chris@0
|
591 foreach ($rows as $row) {
|
Chris@0
|
592 if ($row instanceof TableSeparator) {
|
Chris@0
|
593 continue;
|
Chris@0
|
594 }
|
Chris@0
|
595
|
Chris@0
|
596 foreach ($row as $i => $cell) {
|
Chris@0
|
597 if ($cell instanceof TableCell) {
|
Chris@0
|
598 $textContent = Helper::removeDecoration($this->output->getFormatter(), $cell);
|
Chris@0
|
599 $textLength = Helper::strlen($textContent);
|
Chris@0
|
600 if ($textLength > 0) {
|
Chris@0
|
601 $contentColumns = str_split($textContent, ceil($textLength / $cell->getColspan()));
|
Chris@0
|
602 foreach ($contentColumns as $position => $content) {
|
Chris@0
|
603 $row[$i + $position] = $content;
|
Chris@0
|
604 }
|
Chris@0
|
605 }
|
Chris@0
|
606 }
|
Chris@0
|
607 }
|
Chris@0
|
608
|
Chris@0
|
609 $lengths[] = $this->getCellWidth($row, $column);
|
Chris@0
|
610 }
|
Chris@0
|
611
|
Chris@0
|
612 $this->effectiveColumnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2;
|
Chris@0
|
613 }
|
Chris@0
|
614 }
|
Chris@0
|
615
|
Chris@0
|
616 /**
|
Chris@0
|
617 * Gets column width.
|
Chris@0
|
618 *
|
Chris@0
|
619 * @return int
|
Chris@0
|
620 */
|
Chris@0
|
621 private function getColumnSeparatorWidth()
|
Chris@0
|
622 {
|
Chris@0
|
623 return strlen(sprintf($this->style->getBorderFormat(), $this->style->getVerticalBorderChar()));
|
Chris@0
|
624 }
|
Chris@0
|
625
|
Chris@0
|
626 /**
|
Chris@0
|
627 * Gets cell width.
|
Chris@0
|
628 *
|
Chris@0
|
629 * @param array $row
|
Chris@0
|
630 * @param int $column
|
Chris@0
|
631 *
|
Chris@0
|
632 * @return int
|
Chris@0
|
633 */
|
Chris@0
|
634 private function getCellWidth(array $row, $column)
|
Chris@0
|
635 {
|
Chris@0
|
636 $cellWidth = 0;
|
Chris@0
|
637
|
Chris@0
|
638 if (isset($row[$column])) {
|
Chris@0
|
639 $cell = $row[$column];
|
Chris@0
|
640 $cellWidth = Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
|
Chris@0
|
641 }
|
Chris@0
|
642
|
Chris@0
|
643 $columnWidth = isset($this->columnWidths[$column]) ? $this->columnWidths[$column] : 0;
|
Chris@0
|
644
|
Chris@0
|
645 return max($cellWidth, $columnWidth);
|
Chris@0
|
646 }
|
Chris@0
|
647
|
Chris@0
|
648 /**
|
Chris@0
|
649 * Called after rendering to cleanup cache data.
|
Chris@0
|
650 */
|
Chris@0
|
651 private function cleanup()
|
Chris@0
|
652 {
|
Chris@0
|
653 $this->effectiveColumnWidths = array();
|
Chris@0
|
654 $this->numberOfColumns = null;
|
Chris@0
|
655 }
|
Chris@0
|
656
|
Chris@0
|
657 private static function initStyles()
|
Chris@0
|
658 {
|
Chris@0
|
659 $borderless = new TableStyle();
|
Chris@0
|
660 $borderless
|
Chris@0
|
661 ->setHorizontalBorderChar('=')
|
Chris@0
|
662 ->setVerticalBorderChar(' ')
|
Chris@0
|
663 ->setCrossingChar(' ')
|
Chris@0
|
664 ;
|
Chris@0
|
665
|
Chris@0
|
666 $compact = new TableStyle();
|
Chris@0
|
667 $compact
|
Chris@0
|
668 ->setHorizontalBorderChar('')
|
Chris@0
|
669 ->setVerticalBorderChar(' ')
|
Chris@0
|
670 ->setCrossingChar('')
|
Chris@0
|
671 ->setCellRowContentFormat('%s')
|
Chris@0
|
672 ;
|
Chris@0
|
673
|
Chris@0
|
674 $styleGuide = new TableStyle();
|
Chris@0
|
675 $styleGuide
|
Chris@0
|
676 ->setHorizontalBorderChar('-')
|
Chris@0
|
677 ->setVerticalBorderChar(' ')
|
Chris@0
|
678 ->setCrossingChar(' ')
|
Chris@0
|
679 ->setCellHeaderFormat('%s')
|
Chris@0
|
680 ;
|
Chris@0
|
681
|
Chris@0
|
682 return array(
|
Chris@0
|
683 'default' => new TableStyle(),
|
Chris@0
|
684 'borderless' => $borderless,
|
Chris@0
|
685 'compact' => $compact,
|
Chris@0
|
686 'symfony-style-guide' => $styleGuide,
|
Chris@0
|
687 );
|
Chris@0
|
688 }
|
Chris@0
|
689
|
Chris@0
|
690 private function resolveStyle($name)
|
Chris@0
|
691 {
|
Chris@0
|
692 if ($name instanceof TableStyle) {
|
Chris@0
|
693 return $name;
|
Chris@0
|
694 }
|
Chris@0
|
695
|
Chris@0
|
696 if (isset(self::$styles[$name])) {
|
Chris@0
|
697 return self::$styles[$name];
|
Chris@0
|
698 }
|
Chris@0
|
699
|
Chris@0
|
700 throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name));
|
Chris@0
|
701 }
|
Chris@0
|
702 }
|