@@ -275,29 +275,40 @@ public function setRow($column, array $row)
275
275
*/
276
276
public function render ()
277
277
{
278
- $ this ->calculateNumberOfColumns ();
279
- $ rows = $ this ->buildTableRows ($ this ->rows );
280
- $ headers = $ this ->buildTableRows ($ this ->headers );
278
+ $ rows = array_merge ($ this ->headers , array ($ divider = new TableSeparator ()), $ this ->rows );
279
+ $ this ->calculateNumberOfColumns ($ rows );
281
280
282
- $ this ->calculateColumnsWidth (array_merge ($ headers , $ rows ));
281
+ $ rows = $ this ->buildTableRows ($ rows );
282
+ $ this ->calculateColumnsWidth ($ rows );
283
283
284
- $ this ->renderRowSeparator ();
285
- if (!empty ($ headers )) {
286
- foreach ($ headers as $ header ) {
287
- $ this ->renderRow ($ header , $ this ->style ->getCellHeaderFormat ());
288
- $ this ->renderRowSeparator ();
289
- }
290
- }
284
+ $ isHeader = true ;
285
+ $ isFirstRow = false ;
291
286
foreach ($ rows as $ row ) {
287
+ if ($ divider === $ row ) {
288
+ $ isHeader = false ;
289
+ $ isFirstRow = true ;
290
+
291
+ continue ;
292
+ }
292
293
if ($ row instanceof TableSeparator) {
293
294
$ this ->renderRowSeparator ();
294
- } else {
295
- $ this -> renderRow ( $ row , $ this -> style -> getCellRowFormat ()) ;
295
+
296
+ continue ;
296
297
}
298
+ if (!$ row ) {
299
+ continue ;
300
+ }
301
+
302
+ if ($ isHeader || $ isFirstRow ) {
303
+ $ this ->renderRowSeparator ();
304
+ if ($ isFirstRow ) {
305
+ $ isFirstRow = false ;
306
+ }
307
+ }
308
+
309
+ $ this ->renderRow ($ row , $ isHeader ? $ this ->style ->getCellHeaderFormat () : $ this ->style ->getCellRowFormat ());
297
310
}
298
- if (!empty ($ rows )) {
299
- $ this ->renderRowSeparator ();
300
- }
311
+ $ this ->renderRowSeparator ();
301
312
302
313
$ this ->cleanup ();
303
314
}
@@ -340,10 +351,6 @@ private function renderColumnSeparator()
340
351
*/
341
352
private function renderRow (array $ row , string $ cellFormat )
342
353
{
343
- if (empty ($ row )) {
344
- return ;
345
- }
346
-
347
354
$ rowContent = $ this ->renderColumnSeparator ();
348
355
foreach ($ this ->getRowColumns ($ row ) as $ column ) {
349
356
$ rowContent .= $ this ->renderCell ($ row , $ column , $ cellFormat );
@@ -386,14 +393,10 @@ private function renderCell(array $row, int $column, string $cellFormat)
386
393
/**
387
394
* Calculate number of columns for this table.
388
395
*/
389
- private function calculateNumberOfColumns ()
396
+ private function calculateNumberOfColumns ($ rows )
390
397
{
391
- if (null !== $ this ->numberOfColumns ) {
392
- return ;
393
- }
394
-
395
398
$ columns = array (0 );
396
- foreach (array_merge ( $ this -> headers , $ this -> rows ) as $ row ) {
399
+ foreach ($ rows as $ row ) {
397
400
if ($ row instanceof TableSeparator) {
398
401
continue ;
399
402
}
@@ -429,15 +432,17 @@ private function buildTableRows($rows)
429
432
}
430
433
}
431
434
432
- $ tableRows = array ();
433
- foreach ($ rows as $ rowKey => $ row ) {
434
- $ tableRows [] = $ this ->fillCells ($ row );
435
- if (isset ($ unmergedRows [$ rowKey ])) {
436
- $ tableRows = array_merge ($ tableRows , $ unmergedRows [$ rowKey ]);
437
- }
438
- }
435
+ return new TableRows (function () use ($ rows , $ unmergedRows ) {
436
+ foreach ($ rows as $ rowKey => $ row ) {
437
+ yield $ this ->fillCells ($ row );
439
438
440
- return $ tableRows ;
439
+ if (isset ($ unmergedRows [$ rowKey ])) {
440
+ foreach ($ unmergedRows [$ rowKey ] as $ row ) {
441
+ yield $ row ;
442
+ }
443
+ }
444
+ }
445
+ });
441
446
}
442
447
443
448
/**
0 commit comments