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