@@ -49,6 +49,8 @@ class Table
4949 */
5050 private $ rows = [];
5151
52+ private $ horizontal = false ;
53+
5254 /**
5355 * Column widths cache.
5456 */
@@ -325,6 +327,13 @@ public function setFooterTitle(?string $title): self
325327 return $ this ;
326328 }
327329
330+ public function setHorizontal (bool $ horizontal = true ): self
331+ {
332+ $ this ->horizontal = $ horizontal ;
333+
334+ return $ this ;
335+ }
336+
328337 /**
329338 * Renders table to output.
330339 *
@@ -340,14 +349,26 @@ public function setFooterTitle(?string $title): self
340349 */
341350 public function render ()
342351 {
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+
344365 $ this ->calculateNumberOfColumns ($ rows );
345366
346367 $ rows = $ this ->buildTableRows ($ rows );
347368 $ this ->calculateColumnsWidth ($ rows );
348369
349- $ isHeader = true ;
350- $ isFirstRow = false ;
370+ $ isHeader = ! $ this -> horizontal ;
371+ $ isFirstRow = $ this -> horizontal ;
351372 foreach ($ rows as $ row ) {
352373 if ($ divider === $ row ) {
353374 $ isHeader = false ;
@@ -372,8 +393,11 @@ public function render()
372393 $ this ->renderRowSeparator (self ::SEPARATOR_TOP , $ this ->headerTitle , $ this ->style ->getHeaderTitleFormat ());
373394 }
374395 }
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+ }
377401 }
378402 $ this ->renderRowSeparator (self ::SEPARATOR_BOTTOM , $ this ->footerTitle , $ this ->style ->getFooterTitleFormat ());
379403
@@ -453,13 +477,17 @@ private function renderColumnSeparator($type = self::BORDER_OUTSIDE)
453477 *
454478 * | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
455479 */
456- private function renderRow (array $ row , string $ cellFormat )
480+ private function renderRow (array $ row , string $ cellFormat, string $ firstCellFormat = null )
457481 {
458482 $ rowContent = $ this ->renderColumnSeparator (self ::BORDER_OUTSIDE );
459483 $ columns = $ this ->getRowColumns ($ row );
460484 $ last = \count ($ columns ) - 1 ;
461485 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+ }
463491 $ rowContent .= $ this ->renderColumnSeparator ($ last === $ i ? self ::BORDER_OUTSIDE : self ::BORDER_INSIDE );
464492 }
465493 $ this ->output ->writeln ($ rowContent );
0 commit comments