@@ -383,41 +383,59 @@ public function render()
383383
384384 $ this ->calculateNumberOfColumns ($ rows );
385385
386- $ rows = $ this ->buildTableRows ($ rows );
387- $ this ->calculateColumnsWidth ($ rows );
386+ $ rowGroups = $ this ->buildTableRows ($ rows );
387+ $ this ->calculateColumnsWidth ($ rowGroups );
388388
389389 $ isHeader = !$ this ->horizontal ;
390390 $ isFirstRow = $ this ->horizontal ;
391391 $ hasTitle = (bool ) $ this ->headerTitle ;
392- foreach ($ rows as $ row ) {
393- if ($ divider === $ row ) {
394- $ isHeader = false ;
395- $ isFirstRow = true ;
396392
397- continue ;
398- }
399- if ($ row instanceof TableSeparator) {
400- $ this ->renderRowSeparator ();
393+ foreach ($ rowGroups as $ rowGroup ) {
394+ $ isHeaderSeparatorRendered = false ;
401395
402- continue ;
403- }
404- if (!$ row ) {
405- continue ;
406- }
396+ foreach ($ rowGroup as $ row ) {
397+ if ($ divider === $ row ) {
398+ $ isHeader = false ;
399+ $ isFirstRow = true ;
407400
408- if ($ isHeader || $ isFirstRow ) {
409- $ this ->renderRowSeparator (
410- $ isHeader ? self ::SEPARATOR_TOP : self ::SEPARATOR_TOP_BOTTOM ,
411- $ hasTitle ? $ this ->headerTitle : null ,
412- $ hasTitle ? $ this ->style ->getHeaderTitleFormat () : null
413- );
414- $ isFirstRow = false ;
415- $ hasTitle = false ;
416- }
417- if ($ this ->horizontal ) {
418- $ this ->renderRow ($ row , $ this ->style ->getCellRowFormat (), $ this ->style ->getCellHeaderFormat ());
419- } else {
420- $ this ->renderRow ($ row , $ isHeader ? $ this ->style ->getCellHeaderFormat () : $ this ->style ->getCellRowFormat ());
401+ continue ;
402+ }
403+
404+ if ($ row instanceof TableSeparator) {
405+ $ this ->renderRowSeparator ();
406+
407+ continue ;
408+ }
409+
410+ if (!$ row ) {
411+ continue ;
412+ }
413+
414+ if ($ isHeader && !$ isHeaderSeparatorRendered ) {
415+ $ this ->renderRowSeparator (
416+ $ isHeader ? self ::SEPARATOR_TOP : self ::SEPARATOR_TOP_BOTTOM ,
417+ $ hasTitle ? $ this ->headerTitle : null ,
418+ $ hasTitle ? $ this ->style ->getHeaderTitleFormat () : null
419+ );
420+ $ hasTitle = false ;
421+ $ isHeaderSeparatorRendered = true ;
422+ }
423+
424+ if ($ isFirstRow ) {
425+ $ this ->renderRowSeparator (
426+ $ isHeader ? self ::SEPARATOR_TOP : self ::SEPARATOR_TOP_BOTTOM ,
427+ $ hasTitle ? $ this ->headerTitle : null ,
428+ $ hasTitle ? $ this ->style ->getHeaderTitleFormat () : null
429+ );
430+ $ isFirstRow = false ;
431+ $ hasTitle = false ;
432+ }
433+
434+ if ($ this ->horizontal ) {
435+ $ this ->renderRow ($ row , $ this ->style ->getCellRowFormat (), $ this ->style ->getCellHeaderFormat ());
436+ } else {
437+ $ this ->renderRow ($ row , $ isHeader ? $ this ->style ->getCellHeaderFormat () : $ this ->style ->getCellRowFormat ());
438+ }
421439 }
422440 }
423441 $ this ->renderRowSeparator (self ::SEPARATOR_BOTTOM , $ this ->footerTitle , $ this ->style ->getFooterTitleFormat ());
@@ -624,13 +642,14 @@ private function buildTableRows(array $rows): TableRows
624642
625643 return new TableRows (function () use ($ rows , $ unmergedRows ): \Traversable {
626644 foreach ($ rows as $ rowKey => $ row ) {
627- yield $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row );
645+ $ rowGroup = [ $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row )] ;
628646
629647 if (isset ($ unmergedRows [$ rowKey ])) {
630648 foreach ($ unmergedRows [$ rowKey ] as $ row ) {
631- yield $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row );
649+ $ rowGroup [] = $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row );
632650 }
633651 }
652+ yield $ rowGroup ;
634653 }
635654 });
636655 }
@@ -771,14 +790,15 @@ private function getRowColumns(array $row): array
771790 /**
772791 * Calculates columns widths.
773792 */
774- private function calculateColumnsWidth (iterable $ rows )
793+ private function calculateColumnsWidth (iterable $ groups )
775794 {
776795 for ($ column = 0 ; $ column < $ this ->numberOfColumns ; ++$ column ) {
777796 $ lengths = [];
778- foreach ($ rows as $ row ) {
779- if ($ row instanceof TableSeparator) {
780- continue ;
781- }
797+ foreach ($ groups as $ group ) {
798+ foreach ($ group as $ row ) {
799+ if ($ row instanceof TableSeparator) {
800+ continue ;
801+ }
782802
783803 foreach ($ row as $ i => $ cell ) {
784804 if ($ cell instanceof TableCell) {
@@ -793,7 +813,8 @@ private function calculateColumnsWidth(iterable $rows)
793813 }
794814 }
795815
796- $ lengths [] = $ this ->getCellWidth ($ row , $ column );
816+ $ lengths [] = $ this ->getCellWidth ($ row , $ column );
817+ }
797818 }
798819
799820 $ this ->effectiveColumnWidths [$ column ] = max ($ lengths ) + Helper::width ($ this ->style ->getCellRowContentFormat ()) - 2 ;
0 commit comments