@@ -35,6 +35,9 @@ class Table
35
35
private const SEPARATOR_BOTTOM = 3 ;
36
36
private const BORDER_OUTSIDE = 0 ;
37
37
private const BORDER_INSIDE = 1 ;
38
+ private const DISPLAY_ORIENTATION_DEFAULT = 'default ' ;
39
+ private const DISPLAY_ORIENTATION_HORIZONTAL = 'horizontal ' ;
40
+ private const DISPLAY_ORIENTATION_VERTICAL = 'vertical ' ;
38
41
39
42
private $ headerTitle ;
40
43
private $ footerTitle ;
@@ -48,8 +51,14 @@ class Table
48
51
* Table rows.
49
52
*/
50
53
private $ rows = [];
51
- private $ horizontal = false ;
52
- private $ vertical = false ;
54
+
55
+ /**
56
+ * Defines the way the table will be displayed.
57
+ * It can be either 'default', 'horizontal' or 'vertical'.
58
+ *
59
+ * @var string
60
+ */
61
+ private $ displayOrientation = self ::DISPLAY_ORIENTATION_DEFAULT ;
53
62
54
63
/**
55
64
* Column widths cache.
@@ -317,14 +326,14 @@ public function setFooterTitle(?string $title): self
317
326
318
327
public function setHorizontal (bool $ horizontal = true ): self
319
328
{
320
- $ this ->horizontal = $ horizontal ;
329
+ $ this ->displayOrientation = $ horizontal ? self :: DISPLAY_ORIENTATION_HORIZONTAL : self :: DISPLAY_ORIENTATION_DEFAULT ;
321
330
322
331
return $ this ;
323
332
}
324
333
325
334
public function setVertical (bool $ vertical = true ): self
326
335
{
327
- $ this ->vertical = $ vertical ;
336
+ $ this ->displayOrientation = $ vertical ? self :: DISPLAY_ORIENTATION_VERTICAL : self :: DISPLAY_ORIENTATION_DEFAULT ;
328
337
329
338
return $ this ;
330
339
}
@@ -349,8 +358,11 @@ public function render()
349
358
return $ cell instanceof TableCell && $ cell ->getColspan () >= 2 ;
350
359
};
351
360
361
+ $ horizontal = self ::DISPLAY_ORIENTATION_HORIZONTAL === $ this ->displayOrientation ;
362
+ $ vertical = self ::DISPLAY_ORIENTATION_VERTICAL === $ this ->displayOrientation ;
363
+
352
364
$ rows = [];
353
- if ($ this -> horizontal ) {
365
+ if ($ horizontal ) {
354
366
foreach ($ this ->headers [0 ] ?? [] as $ i => $ header ) {
355
367
$ rows [$ i ] = [$ header ];
356
368
foreach ($ this ->rows as $ row ) {
@@ -366,7 +378,7 @@ public function render()
366
378
}
367
379
}
368
380
}
369
- } elseif ($ this -> vertical ) {
381
+ } elseif ($ vertical ) {
370
382
$ maxHeaderLength = max (array_map (static function (string $ header ) {
371
383
return mb_strlen ($ header );
372
384
}, $ this ->headers [0 ] ?? ['' ]));
@@ -408,8 +420,8 @@ public function render()
408
420
$ rows = $ this ->buildTableRows ($ rows );
409
421
$ this ->calculateColumnsWidth ($ rows );
410
422
411
- $ isHeader = !$ this -> horizontal ;
412
- $ isFirstRow = $ this -> horizontal ;
423
+ $ isHeader = !$ horizontal ;
424
+ $ isFirstRow = $ horizontal ;
413
425
$ hasTitle = (bool ) $ this ->headerTitle ;
414
426
foreach ($ rows as $ row ) {
415
427
if ($ divider === $ row ) {
@@ -436,12 +448,12 @@ public function render()
436
448
$ isFirstRow = false ;
437
449
$ hasTitle = false ;
438
450
}
439
- if ($ this -> vertical ) {
451
+ if ($ vertical ) {
440
452
$ isHeader = false ;
441
453
$ isFirstRow = false ;
442
454
}
443
455
444
- if ($ this -> horizontal ) {
456
+ if ($ horizontal ) {
445
457
$ this ->renderRow ($ row , $ this ->style ->getCellRowFormat (), $ this ->style ->getCellHeaderFormat ());
446
458
} else {
447
459
$ this ->renderRow ($ row , $ isHeader ? $ this ->style ->getCellHeaderFormat () : $ this ->style ->getCellRowFormat ());
0 commit comments