8000 Using an enum instead of 2 separated booleans · symfony/symfony@ebc61b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit ebc61b1

Browse files
committed
Using an enum instead of 2 separated booleans
1 parent 8e79f90 commit ebc61b1

File tree

1 file changed

+22
-10
lines changed
  • src/Symfony/Component/Console/Helper

1 file changed

+22
-10
lines changed

src/Symfony/Component/Console/Helper/Table.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class Table
3535
private const SEPARATOR_BOTTOM = 3;
3636
private const BORDER_OUTSIDE = 0;
3737
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';
3841

3942
private $headerTitle;
4043
private $footerTitle;
@@ -48,8 +51,14 @@ class Table
4851
* Table rows.
4952
*/
5053
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;
5362

5463
/**
5564
* Column widths cache.
@@ -317,14 +326,14 @@ public function setFooterTitle(?string $title): self
317326

318327
public function setHorizontal(bool $horizontal = true): self
319328
{
320-
$this->horizontal = $horizontal;
329+
$this->displayOrientation = $horizontal ? self::DISPLAY_ORIENTATION_HORIZONTAL : self::DISPLAY_ORIENTATION_DEFAULT;
321330

322331
return $this;
323332
}
324333

325334
public function setVertical(bool $vertical = true): self
326335
{
327-
$this->vertical = $vertical;
336+
$this->displayOrientation = $vertical ? self::DISPLAY_ORIENTATION_VERTICAL : self::DISPLAY_ORIENTATION_DEFAULT;
328337

329338
return $this;
330339
}
@@ -349,8 +358,11 @@ public function render()
349358
return $cell instanceof TableCell && $cell->getColspan() >= 2;
350359
};
351360

361+
$horizontal = self::DISPLAY_ORIENTATION_HORIZONTAL === $this->displayOrientation;
362+
$vertical = self::DISPLAY_ORIENTATION_VERTICAL === $this->displayOrientation;
363+
352364
$rows = [];
353-
if ($this->horizontal) {
365+
if ($horizontal) {
354366
foreach ($this->headers[0] ?? [] as $i => $header) {
355367
$rows[$i] = [$header];
356368
foreach ($this->rows as $row) {
@@ -366,7 +378,7 @@ public function render()
366378
}
367379
}
368380
}
369-
} elseif ($this->vertical) {
381+
} elseif ($vertical) {
370382
$maxHeaderLength = max(array_map(static function (string $header) {
371383
return mb_strlen($header);
372384
}, $this->headers[0] ?? ['']));
@@ -408,8 +420,8 @@ public function render()
408420
$rows = $this->buildTableRows($rows);
409421
$this->calculateColumnsWidth($rows);
410422

411-
$isHeader = !$this->horizontal;
412-
$isFirstRow = $this->horizontal;
423+
$isHeader = !$horizontal;
424+
$isFirstRow = $horizontal;
413425
$hasTitle = (bool) $this->headerTitle;
414426
foreach ($rows as $row) {
415427
if ($divider === $row) {
@@ -436,12 +448,12 @@ public function render()
436448
$isFirstRow = false;
437449
$hasTitle = false;
438450
}
439-
if ($this->vertical) {
451+
if ($vertical) {
440452
$isHeader = false;
441453
$isFirstRow = false;
442454
}
443455

444-
if ($this->horizontal) {
456+
if ($horizontal) {
445457
$this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat());
446458
} else {
447459
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());

0 commit comments

Comments
 (0)
0