Passed
Push — master ( 902d4b...cd5836 )
by
unknown
17:07 queued 06:33
created

TextGrid::strlen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Helper;
4
5
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
6
7
class TextGrid
8
{
9
    private bool $isCli;
10
11
    protected array $matrix;
12
13
    protected array $rows;
14
15
    protected array $columns;
16
17
    private string $gridDisplay;
18
19
    private bool $rowDividers = false;
20
21
    private bool $rowHeaders = true;
22
23
    private bool $columnHeaders = true;
24
25 43
    public function __construct(array $matrix, bool $isCli = true, bool $rowDividers = false, bool $rowHeaders = true, bool $columnHeaders = true)
26
    {
27 43
        $this->rows = array_keys($matrix);
28 43
        $this->columns = array_keys($matrix[$this->rows[0]]);
29
30 43
        $matrix = array_values($matrix);
31 43
        array_walk(
32 43
            $matrix,
33 43
            function (&$row): void {
34 43
                $row = array_values($row);
35 43
            }
36 43
        );
37
38 43
        $this->matrix = $matrix;
39 43
        $this->isCli = $isCli;
40 43
        $this->rowDividers = $rowDividers;
41 43
        $this->rowHeaders = $rowHeaders;
42 43
        $this->columnHeaders = $columnHeaders;
43
    }
44
45 43
    public function render(): string
46
    {
47 43
        $this->gridDisplay = $this->isCli ? '' : ('<pre>' . PHP_EOL);
48
49 43
        if (!empty($this->rows)) {
50 43
            $maxRow = max($this->rows);
51 43
            $maxRowLength = strlen((string) $maxRow) + 1;
52 43
            $columnWidths = $this->getColumnWidths();
53
54 43
            $this->renderColumnHeader($maxRowLength, $columnWidths);
55 43
            $this->renderRows($maxRowLength, $columnWidths);
56 43
            if (!$this->rowDividers) {
57 41
                $this->renderFooter($maxRowLength, $columnWidths);
58
            }
59
        }
60
61 43
        $this->gridDisplay .= $this->isCli ? '' : '</pre>';
62
63 43
        return $this->gridDisplay;
64
    }
65
66 43
    private function renderRows(int $maxRowLength, array $columnWidths): void
67
    {
68 43
        foreach ($this->matrix as $row => $rowData) {
69 43
            if ($this->rowHeaders) {
70 40
                $this->gridDisplay .= '|' . str_pad((string) $this->rows[$row], $maxRowLength, ' ', STR_PAD_LEFT) . ' ';
71
            }
72 43
            $this->renderCells($rowData, $columnWidths);
73 43
            $this->gridDisplay .= '|' . PHP_EOL;
74 43
            if ($this->rowDividers) {
75 2
                $this->renderFooter($maxRowLength, $columnWidths);
76
            }
77
        }
78
    }
79
80 43
    private function renderCells(array $rowData, array $columnWidths): void
81
    {
82 43
        foreach ($rowData as $column => $cell) {
83 43
            $valueForLength = $this->getString($cell);
84 43
            $displayCell = $this->isCli ? $valueForLength : htmlentities($valueForLength);
85 43
            $this->gridDisplay .= '| ';
86 43
            $this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - $this->strlen($valueForLength) + 1);
87
        }
88
    }
89
90 43
    private function renderColumnHeader(int $maxRowLength, array &$columnWidths): void
91
    {
92 43
        if (!$this->columnHeaders) {
93 3
            $this->renderFooter($maxRowLength, $columnWidths);
94
95 3
            return;
96
        }
97 40
        foreach ($this->columns as $column => $reference) {
98 40
            $columnWidths[$column] = max($columnWidths[$column], $this->strlen($reference));
99
        }
100 40
        if ($this->rowHeaders) {
101 39
            $this->gridDisplay .= str_repeat(' ', $maxRowLength + 2);
102
        }
103 40
        foreach ($this->columns as $column => $reference) {
104 40
            $this->gridDisplay .= '+-' . str_repeat('-', $columnWidths[$column] + 1);
105
        }
106 40
        $this->gridDisplay .= '+' . PHP_EOL;
107
108 40
        if ($this->rowHeaders) {
109 39
            $this->gridDisplay .= str_repeat(' ', $maxRowLength + 2);
110
        }
111 40
        foreach ($this->columns as $column => $reference) {
112 40
            $this->gridDisplay .= '| ' . str_pad((string) $reference, $columnWidths[$column] + 1, ' ');
113
        }
114 40
        $this->gridDisplay .= '|' . PHP_EOL;
115
116 40
        $this->renderFooter($maxRowLength, $columnWidths);
117
    }
118
119 43
    private function renderFooter(int $maxRowLength, array $columnWidths): void
120
    {
121 43
        if ($this->rowHeaders) {
122 40
            $this->gridDisplay .= '+' . str_repeat('-', $maxRowLength + 1);
123
        }
124 43
        foreach ($this->columns as $column => $reference) {
125 43
            $this->gridDisplay .= '+-';
126 43
            $this->gridDisplay .= str_pad((string) '', $columnWidths[$column] + 1, '-');
127
        }
128 43
        $this->gridDisplay .= '+' . PHP_EOL;
129
    }
130
131 43
    private function getColumnWidths(): array
132
    {
133 43
        $columnCount = count($this->matrix, COUNT_RECURSIVE) / count($this->matrix);
134 43
        $columnWidths = [];
135 43
        for ($column = 0; $column < $columnCount; ++$column) {
136 43
            $columnWidths[] = $this->getColumnWidth(array_column($this->matrix, $column));
137
        }
138
139 43
        return $columnWidths;
140
    }
141
142 43
    private function getColumnWidth(array $columnData): int
143
    {
144 43
        $columnWidth = 0;
145 43
        $columnData = array_values($columnData);
146
147 43
        foreach ($columnData as $columnValue) {
148 43
            $columnWidth = max($columnWidth, $this->strlen($this->getString($columnValue)));
149
        }
150
151 43
        return $columnWidth;
152
    }
153
154 43
    protected function getString(mixed $value): string
155
    {
156 43
        return StringHelper::convertToString($value, convertBool: true);
157
    }
158
159 43
    protected function strlen(string $value): int
160
    {
161 43
        return mb_strlen($value);
162
    }
163
}
164