@@ -369,41 +369,59 @@ public function render()
369369
370370 $ this ->calculateNumberOfColumns ($ rows );
371371
372- $ rows = $ this ->buildTableRows ($ rows );
373- $ this ->calculateColumnsWidth ($ rows );
372+ $ rowGroups = $ this ->buildTableRows ($ rows );
373+ $ this ->calculateColumnsWidth ($ rowGroups );
374374
375375 $ isHeader = !$ this ->horizontal ;
376376 $ isFirstRow = $ this ->horizontal ;
377377 $ hasTitle = (bool ) $ this ->headerTitle ;
378- foreach ($ rows as $ row ) {
379- if ($ divider === $ row ) {
380- $ isHeader = false ;
381- $ isFirstRow = true ;
382378
383- continue ;
384- }
385- if ($ row instanceof TableSeparator) {
386- $ this ->renderRowSeparator ();
379+ foreach ($ rowGroups as $ rowGroup ) {
380+ $ isHeaderSeparatorRendered = false ;
387381
388- continue ;
389- }
390- if (!$ row ) {
391- continue ;
392- }
382+ foreach ($ rowGroup as $ row ) {
383+ if ($ divider === $ row ) {
384+ $ isHeader = false ;
385+ $ isFirstRow = true ;
393386
394- if ($ isHeader || $ isFirstRow ) {
395- $ this ->renderRowSeparator (
396- $ isHeader ? self ::SEPARATOR_TOP : self ::SEPARATOR_TOP_BOTTOM ,
397- $ hasTitle ? $ this ->headerTitle : null ,
398- $ hasTitle ? $ this ->style ->getHeaderTitleFormat () : null
399- );
400- $ isFirstRow = false ;
401- $ hasTitle = false ;
402- }
403- if ($ this ->horizontal ) {
404- $ this ->renderRow ($ row , $ this ->style ->getCellRowFormat (), $ this ->style ->getCellHeaderFormat ());
405- } else {
406- $ this ->renderRow ($ row , $ isHeader ? $ this ->style ->getCellHeaderFormat () : $ this ->style ->getCellRowFormat ());
387+ continue ;
388+ }
389+
390+ if ($ row instanceof TableSeparator) {
391+ $ this ->renderRowSeparator ();
392+
393+ continue ;
394+ }
395+
396+ if (!$ row ) {
397+ continue ;
398+ }
399+
400+ if ($ isHeader && !$ isHeaderSeparatorRendered ) {
401+ $ this ->renderRowSeparator (
402+ $ isHeader ? self ::SEPARATOR_TOP : self ::SEPARATOR_TOP_BOTTOM ,
403+ $ hasTitle ? $ this ->headerTitle : null ,
404+ $ hasTitle ? $ this ->style ->getHeaderTitleFormat () : null
405+ );
406+ $ hasTitle = false ;
407+ $ isHeaderSeparatorRendered = true ;
408+ }
409+
410+ if ($ isFirstRow ) {
411+ $ this ->renderRowSeparator (
412+ $ isHeader ? self ::SEPARATOR_TOP : self ::SEPARATOR_TOP_BOTTOM ,
413+ $ hasTitle ? $ this ->headerTitle : null ,
414+ $ hasTitle ? $ this ->style ->getHeaderTitleFormat () : null
415+ );
416+ $ isFirstRow = false ;
417+ $ hasTitle = false ;
418+ }
419+
420+ if ($ this ->horizontal ) {
421+ $ this ->renderRow ($ row , $ this ->style ->getCellRowFormat (), $ this ->style ->getCellHeaderFormat ());
422+ } else {
423+ $ this ->renderRow ($ row , $ isHeader ? $ this ->style ->getCellHeaderFormat () : $ this ->style ->getCellRowFormat ());
424+ }
407425 }
408426 }
409427 $ this ->renderRowSeparator (self ::SEPARATOR_BOTTOM , $ this ->footerTitle , $ this ->style ->getFooterTitleFormat ());
@@ -587,13 +605,14 @@ private function buildTableRows(array $rows): TableRows
587605
588606 return new TableRows (function () use ($ rows , $ unmergedRows ): \Traversable {
589607 foreach ($ rows as $ rowKey => $ row ) {
590- yield $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row );
608+ $ rowGroup = [ $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row )] ;
591609
592610 if (isset ($ unmergedRows [$ rowKey ])) {
593611 foreach ($ unmergedRows [$ rowKey ] as $ row ) {
594- yield $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row );
612+ $ rowGroup [] = $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row );
595613 }
596614 }
615+ yield $ rowGroup ;
597616 }
598617 });
599618 }
@@ -734,14 +753,15 @@ private function getRowColumns(array $row): array
734753 /**
735754 * Calculates columns widths.
736755 */
737- private function calculateColumnsWidth (iterable $ rows )
756+ private function calculateColumnsWidth (iterable $ groups )
738757 {
739758 for ($ column = 0 ; $ column < $ this ->numberOfColumns ; ++$ column ) {
740759 $ lengths = [];
741- foreach ($ rows as $ row ) {
742- if ($ row instanceof TableSeparator) {
743- continue ;
744- }
760+ foreach ($ groups as $ group ) {
761+ foreach ($ group as $ row ) {
762+ if ($ row instanceof TableSeparator) {
763+ continue ;
764+ }
745765
746766 foreach ($ row as $ i => $ cell ) {
747767 if ($ cell instanceof TableCell) {
@@ -756,7 +776,8 @@ private function calculateColumnsWidth(iterable $rows)
756776 }
757777 }
758778
759- $ lengths [] = $ this ->getCellWidth ($ row , $ column );
779+ $ lengths [] = $ this ->getCellWidth ($ row , $ column );
780+ }
760781 }
761782
762783 $ this ->effectiveColumnWidths [$ column ] = max ($ lengths ) + Helper::strlen ($ this ->style ->getCellRowContentFormat ()) - 2 ;
0 commit comments