@@ -49,6 +49,8 @@ class Table
49
49
*/
50
50
private $ rows = [];
51
51
52
+ private $ horizontal = false ;
53
+
52
54
/**
53
55
* Column widths cache.
54
56
*/
@@ -325,6 +327,13 @@ public function setFooterTitle(?string $title): self
325
327
return $ this ;
326
328
}
327
329
330
+ public function setHorizontal (bool $ horizontal = true ): self
331
+ {
332
+ $ this ->horizontal = $ horizontal ;
333
+
334
+ return $ this ;
335
+ }
336
+
328
337
/**
329
338
* Renders table to output.
330
339
*
@@ -340,14 +349,26 @@ public function setFooterTitle(?string $title): self
340
349
*/
341
350
public function render ()
342
351
{
343
- $ rows = array_merge ($ this ->headers , [$ divider = new TableSeparator ()], $ this ->rows );
352
+ $ divider = new TableSeparator ();
353
+ if ($ this ->horizontal ) {
354
+ $ rows = [];
355
+ foreach ($ this ->headers [0 ] as $ i => $ header ) {
356
+ $ rows [$ i ] = [$ header ];
357
+ foreach ($ this ->rows as $ row ) {
358
+ $ rows [$ i ][] = $ row [$ i ];
359
+ }
360
+ }
361
+ } else {
362
+ $ rows = array_merge ($ this ->headers , [$ divider ], $ this ->rows );
363
+ }
364
+
344
365
$ this ->calculateNumberOfColumns ($ rows );
345
366
346
367
$ rows = $ this ->buildTableRows ($ rows );
347
368
$ this ->calculateColumnsWidth ($ rows );
348
369
349
- $ isHeader = true ;
350
- $ isFirstRow = false ;
370
+ $ isHeader = ! $ this -> horizontal ;
371
+ $ isFirstRow = $ this -> horizontal ;
351
372
foreach ($ rows as $ row ) {
352
373
if ($ divider === $ row ) {
353
374
$ isHeader = false ;
@@ -372,8 +393,11 @@ public function render()
372
393
$ this ->renderRowSeparator (self ::SEPARATOR_TOP , $ this ->headerTitle , $ this ->style ->getHeaderTitleFormat ());
373
394
}
374
395
}
375
-
376
- $ this ->renderRow ($ row , $ isHeader ? $ this ->style ->getCellHeaderFormat () : $ this ->style ->getCellRowFormat ());
396
+ if ($ this ->horizontal ) {
397
+ $ this ->renderRow ($ row , $ this ->style ->getCellRowFormat (), $ this ->style ->getCellHeaderFormat ());
398
+ } else {
399
+ $ this ->renderRow ($ row , $ isHeader ? $ this ->style ->getCellHeaderFormat () : $ this ->style ->getCellRowFormat ());
400
+ }
377
401
}
378
402
$ this ->renderRowSeparator (self ::SEPARATOR_BOTTOM , $ this ->footerTitle , $ this ->style ->getFooterTitleFormat ());
379
403
@@ -453,13 +477,17 @@ private function renderColumnSeparator($type = self::BORDER_OUTSIDE)
453
477
*
454
478
* | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
455
479
*/
456
- private function renderRow (array $ row , string $ cellFormat )
480
+ private function renderRow (array $ row , string $ cellFormat, string $ firstCellFormat = null )
457
481
{
458
482
$ rowContent = $ this ->renderColumnSeparator (self ::BORDER_OUTSIDE );
459
483
$ columns = $ this ->getRowColumns ($ row );
460
484
$ last = \count ($ columns ) - 1 ;
461
485
foreach ($ columns as $ i => $ column ) {
462
- $ rowContent .= $ this ->renderCell ($ row , $ column , $ cellFormat );
486
+ if ($ firstCellFormat && 0 === $ i ) {
487
+ $ rowContent .= $ this ->renderCell ($ row , $ column , $ firstCellFormat );
488
+ } else {
489
+ $ rowContent .= $ this ->renderCell ($ row , $ column , $ cellFormat );
490
+ }
463
491
$ rowContent .= $ this ->renderColumnSeparator ($ last === $ i ? self ::BORDER_OUTSIDE : self ::BORDER_INSIDE );
464
492
}
465
493
$ this ->output ->writeln ($ rowContent );
0 commit comments