|
20 | 20 | * @author Fabien Potencier <fabien@symfony.com>
|
21 | 21 | * @author Саша Стаменковић <umpirsky@gmail.com>
|
22 | 22 | * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
|
| 23 | + * @author Max Grigorian <maxakawizard@gmail.com> |
23 | 24 | */
|
24 | 25 | class Table
|
25 | 26 | {
|
@@ -61,6 +62,11 @@ class Table
|
61 | 62 | */
|
62 | 63 | private $style;
|
63 | 64 |
|
| 65 | + /** |
| 66 | + * @var array |
| 67 | + */ |
| 68 | + private $columnStyles = array(); |
| 69 | + |
64 | 70 | private static $styles;
|
65 | 71 |
|
66 | 72 | public function __construct(OutputInterface $output)
|
@@ -139,6 +145,47 @@ public function getStyle()
|
139 | 145 | return $this->style;
|
140 | 146 | }
|
141 | 147 |
|
| 148 | + /** |
| 149 | + * Sets table column style. |
| 150 | + * |
| 151 | + * @param int $columnIndex Column index |
| 152 | + * @param TableStyle|string $name The style name or a TableStyle instance |
| 153 | + * |
| 154 | + * @return Table |
| 155 | + */ |
| 156 | + public function setColumnStyle($columnIndex, $name) |
| 157 | + { |
| 158 | + $columnIndex = intval($columnIndex); |
| 159 | + |
| 160 | + if ($name instanceof TableStyle) { |
| 161 | + $this->columnStyles[$columnIndex] = $name; |
| 162 | + } elseif (isset(self::$styles[$name])) { |
| 163 | + $this->columnStyles[$columnIndex] = self::$styles[$name]; |
| 164 | + } else { |
| 165 | + throw new \InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); |
| 166 | + } |
| 167 | + |
| 168 | + return $this; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Gets the current style for a column. |
| 173 | + * |
| 174 | + * If style was not set, it returns the global table style. |
| 175 | + * |
| 176 | + * @param int $columnIndex Column index |
| 177 | + * |
| 178 | + * @return TableStyle |
| 179 | + */ |
| 180 | + public function getColumnStyle($columnIndex) |
| 181 | + { |
| 182 | + if (isset($this->columnStyles[$columnIndex])) { |
| 183 | + return $this->columnStyles[$columnIndex]; |
| 184 | + } |
| 185 | + |
| 186 | + return $this->getStyle(); |
| 187 | + } |
| 188 | + |
142 | 189 | public function setHeaders(array $headers)
|
143 | 190 | {
|
144 | 191 | $headers = array_values($headers);
|
@@ -308,12 +355,14 @@ private function renderCell(array $row, $column, $cellFormat)
|
308 | 355 | $width += strlen($cell) - mb_strwidth($cell, $encoding);
|
309 | 356 | }
|
310 | 357 |
|
| 358 | + $style = $this->getColumnStyle($column); |
| 359 | + |
311 | 360 | if ($cell instanceof TableSeparator) {
|
312 |
| - $this->output->write(sprintf($this->style->getBorderFormat(), str_repeat($this->style->getHorizontalBorderChar(), $width))); |
| 361 | + $this->output->write(sprintf($style->getBorderFormat(), str_repeat($style->getHorizontalBorderChar(), $width))); |
313 | 362 | } else {
|
314 | 363 | $width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
|
315 |
| - $content = sprintf($this->style->getCellRowContentFormat(), $cell); |
316 |
| - $this->output->write(sprintf($cellFormat, str_pad($content, $width, $this->style->getPaddingChar(), $this->style->getPadType()))); |
| 364 | + $content = sprintf($style->getCellRowContentFormat(), $cell); |
| 365 | + $this->output->write(sprintf($cellFormat, str_pad($content, $width, $style->getPaddingChar(), $style->getPadType()))); |
317 | 366 | }
|
318 | 367 | }
|
319 | 368 |
|
|
0 commit comments