@@ -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.
@@ -314,14 +323,14 @@ public function setFooterTitle(?string $title): self
314
323
315
324
public function setHorizontal (bool $ horizontal = true ): self
316
325
{
317
- $ this ->horizontal = $ horizontal ;
326
+ $ this ->displayOrientation = $ horizontal ? self :: DISPLAY_ORIENTATION_HORIZONTAL : self :: DISPLAY_ORIENTATION_DEFAULT ;
318
327
319
328
return $ this ;
320
329
}
321
330
322
331
public function setVertical (bool $ vertical = true ): self
323
332
{
324
- $ this ->vertical = $ vertical ;
333
+ $ this ->displayOrientation = $ vertical ? self :: DISPLAY_ORIENTATION_VERTICAL : self :: DISPLAY_ORIENTATION_DEFAULT ;
325
334
326
335
return $ this ;
327
336
}
@@ -346,8 +355,11 @@ public function render()
346
355
return $ cell instanceof TableCell && $ cell ->getColspan () >= 2 ;
347
356
};
348
357
358
+ $ horizontal = self ::DISPLAY_ORIENTATION_HORIZONTAL === $ this ->displayOrientation ;
359
+ $ vertical = self ::DISPLAY_ORIENTATION_VERTICAL === $ this ->displayOrientation ;
360
+
349
361
$ rows = [];
350
- if ($ this -> horizontal ) {
362
+ if ($ horizontal ) {
351
363
foreach ($ this ->headers [0 ] ?? [] as $ i => $ header ) {
352
364
$ rows [$ i ] = [$ header ];
353
365
foreach ($ this ->rows as $ row ) {
@@ -363,7 +375,7 @@ public function render()
363
375
}
364
376
}
365
377
}
366
- } elseif ($ this -> vertical ) {
378
+ } elseif ($ vertical ) {
367
379
$ maxHeaderLength = max (array_map (static function (string $ header ) {
368
380
return mb_strlen ($ header );
369
381
}, $ this ->headers [0 ] ?? ['' ]));
@@ -405,8 +417,8 @@ public function render()
405
417
$ rows = $ this ->buildTableRows ($ rows );
406
418
$ this ->calculateColumnsWidth ($ rows );
407
419
408
- $ isHeader = !$ this -> horizontal ;
409
- $ isFirstRow = $ this -> horizontal ;
420
+ $ isHeader = !$ horizontal ;
421
+ $ isFirstRow = $ horizontal ;
410
422
foreach ($ rows as $ row ) {
411
423
if ($ divider === $ row ) {
412
424
$ isHeader = false ;
@@ -431,12 +443,12 @@ public function render()
431
443
$ this ->renderRowSeparator (self ::SEPARATOR_TOP , $ this ->headerTitle , $ this ->style ->getHeaderTitleFormat ());
432
444
}
433
445
}
434
- if ($ this -> vertical ) {
446
+ if ($ vertical ) {
435
447
$ isHeader = false ;
436
448
$ isFirstRow = false ;
437
449
}
438
450
439
- if ($ this -> horizontal ) {
451
+ if ($ horizontal ) {
440
452
$ this ->renderRow ($ row , $ this ->style ->getCellRowFormat (), $ this ->style ->getCellHeaderFormat ());
441
453
} else {
442
454
$ this ->renderRow ($ row , $ isHeader ? $ this ->style ->getCellHeaderFormat () : $ this ->style ->getCellRowFormat ());
0 commit comments