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