@@ -383,41 +383,59 @@ public function render()
383
383
384
384
$ this ->calculateNumberOfColumns ($ rows );
385
385
386
- $ rows = $ this ->buildTableRows ($ rows );
387
- $ this ->calculateColumnsWidth ($ rows );
386
+ $ rowGroups = $ this ->buildTableRows ($ rows );
387
+ $ this ->calculateColumnsWidth ($ rowGroups );
388
388
389
389
$ isHeader = !$ this ->horizontal ;
390
390
$ isFirstRow = $ this ->horizontal ;
391
391
$ hasTitle = (bool ) $ this ->headerTitle ;
392
- foreach ($ rows as $ row ) {
393
- if ($ divider === $ row ) {
394
- $ isHeader = false ;
395
- $ isFirstRow = true ;
396
392
397
- continue ;
398
- }
399
- if ($ row instanceof TableSeparator) {
400
- $ this ->renderRowSeparator ();
393
+ foreach ($ rowGroups as $ rowGroup ) {
394
+ $ isHeaderSeparatorRendered = false ;
401
395
402
- continue ;
403
- }
404
- if (!$ row ) {
405
- continue ;
406
- }
396
+ foreach ($ rowGroup as $ row ) {
397
+ if ($ divider === $ row ) {
398
+ $ isHeader = false ;
399
+ $ isFirstRow = true ;
407
400
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
+ }
421
439
}
422
440
}
423
441
$ this ->renderRowSeparator (self ::SEPARATOR_BOTTOM , $ this ->footerTitle , $ this ->style ->getFooterTitleFormat ());
@@ -624,13 +642,16 @@ private function buildTableRows(array $rows): TableRows
624
642
625
643
return new TableRows (function () use ($ rows , $ unmergedRows ): \Traversable {
626
644
foreach ($ rows as $ rowKey => $ row ) {
627
- yield $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row );
645
+ $ rowGroup = [
646
+ $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row ),
647
+ ];
628
648
629
649
if (isset ($ unmergedRows [$ rowKey ])) {
630
650
foreach ($ unmergedRows [$ rowKey ] as $ row ) {
631
- yield $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row );
651
+ $ rowGroup [] = $ row instanceof TableSeparator ? $ row : $ this ->fillCells ($ row );
632
652
}
633
653
}
654
+ yield $ rowGroup ;
634
655
}
635
656
});
636
657
}
@@ -659,7 +680,7 @@ private function fillNextRows(array $rows, int $line): array
659
680
{
660
681
$ unmergedRows = [];
661
682
foreach ($ rows [$ line ] as $ column => $ cell ) {
662
- if (null !== $ cell && !$ cell instanceof TableCell && !is_scalar ($ cell ) && !(\is_object ($ cell ) && method_exists ($ cell , '__toString ' ))) {
683
+ if (null !== $ cell && !$ cell instanceof TableCell && !\ is_scalar ($ cell ) && !(\is_object ($ cell ) && method_exists ($ cell , '__toString ' ))) {
663
684
throw new InvalidArgumentException (sprintf ('A cell must be a TableCell, a scalar or an object implementing "__toString()", "%s" given. ' , get_debug_type ($ cell )));
664
685
}
665
686
if ($ cell instanceof TableCell && $ cell ->getRowspan () > 1 ) {
@@ -771,29 +792,31 @@ private function getRowColumns(array $row): array
771
792
/**
772
793
* Calculates columns widths.
773
794
*/
774
- private function calculateColumnsWidth (iterable $ rows )
795
+ private function calculateColumnsWidth (iterable $ groups )
775
796
{
776
797
for ($ column = 0 ; $ column < $ this ->numberOfColumns ; ++$ column ) {
777
798
$ lengths = [];
778
- foreach ($ rows as $ row ) {
779
- if ($ row instanceof TableSeparator) {
780
- continue ;
781
- }
799
+ foreach ($ groups as $ group ) {
800
+ foreach ($ group as $ row ) {
801
+ if ($ row instanceof TableSeparator) {
802
+ continue ;
803
+ }
782
804
783
- foreach ($ row as $ i => $ cell ) {
784
- if ($ cell instanceof TableCell) {
785
- $ textContent = Helper::removeDecoration ($ this ->output ->getFormatter (), $ cell );
786
- $ textLength = Helper::width ($ textContent );
787
- if ($ textLength > 0 ) {
788
- $ contentColumns = str_split ($ textContent , ceil ($ textLength / $ cell ->getColspan ()));
789
- foreach ($ contentColumns as $ position => $ content ) {
790
- $ row [$ i + $ position ] = $ content ;
805
+ foreach ($ row as $ i => $ cell ) {
806
+ if ($ cell instanceof TableCell) {
807
+ $ textContent = Helper::removeDecoration ($ this ->output ->getFormatter (), $ cell );
808
+ $ textLength = Helper::width ($ textContent );
809
+ if ($ textLength > 0 ) {
810
+ $ contentColumns = str_split ($ textContent , ceil ($ textLength / $ cell ->getColspan ()));
811
+ foreach ($ contentColumns as $ position => $ content ) {
812
+ $ row [$ i + $ position ] = $ content ;
813
+ }
791
814
}
792
815
}
793
816
}
794
- }
795
817
796
- $ lengths [] = $ this ->getCellWidth ($ row , $ column );
818
+ $ lengths [] = $ this ->getCellWidth ($ row , $ column );
819
+ }
797
820
}
798
821
799
822
$ this ->effectiveColumnWidths [$ column ] = max ($ lengths ) + Helper::width ($ this ->style ->getCellRowContentFormat ()) - 2 ;
0 commit comments