Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Helper;
4
 
5
class TextGrid
6
{
7
    private bool $isCli;
8
 
9
    protected array $matrix;
10
 
11
    protected array $rows;
12
 
13
    protected array $columns;
14
 
15
    private string $gridDisplay;
16
 
17
    public function __construct(array $matrix, bool $isCli = true)
18
    {
19
        $this->rows = array_keys($matrix);
20
        $this->columns = array_keys($matrix[$this->rows[0]]);
21
 
22
        $matrix = array_values($matrix);
23
        array_walk(
24
            $matrix,
25
            function (&$row): void {
26
                $row = array_values($row);
27
            }
28
        );
29
 
30
        $this->matrix = $matrix;
31
        $this->isCli = $isCli;
32
    }
33
 
34
    public function render(): string
35
    {
36
        $this->gridDisplay = $this->isCli ? '' : '<pre>';
37
 
38
        if (!empty($this->rows)) {
39
            $maxRow = max($this->rows);
40
            $maxRowLength = mb_strlen((string) $maxRow) + 1;
41
            $columnWidths = $this->getColumnWidths();
42
 
43
            $this->renderColumnHeader($maxRowLength, $columnWidths);
44
            $this->renderRows($maxRowLength, $columnWidths);
45
            $this->renderFooter($maxRowLength, $columnWidths);
46
        }
47
 
48
        $this->gridDisplay .= $this->isCli ? '' : '</pre>';
49
 
50
        return $this->gridDisplay;
51
    }
52
 
53
    private function renderRows(int $maxRowLength, array $columnWidths): void
54
    {
55
        foreach ($this->matrix as $row => $rowData) {
56
            $this->gridDisplay .= '|' . str_pad((string) $this->rows[$row], $maxRowLength, ' ', STR_PAD_LEFT) . ' ';
57
            $this->renderCells($rowData, $columnWidths);
58
            $this->gridDisplay .= '|' . PHP_EOL;
59
        }
60
    }
61
 
62
    private function renderCells(array $rowData, array $columnWidths): void
63
    {
64
        foreach ($rowData as $column => $cell) {
65
            $displayCell = ($this->isCli) ? (string) $cell : htmlentities((string) $cell);
66
            $this->gridDisplay .= '| ';
67
            $this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - mb_strlen($cell ?? '') + 1);
68
        }
69
    }
70
 
71
    private function renderColumnHeader(int $maxRowLength, array $columnWidths): void
72
    {
73
        $this->gridDisplay .= str_repeat(' ', $maxRowLength + 2);
74
        foreach ($this->columns as $column => $reference) {
75
            $this->gridDisplay .= '+-' . str_repeat('-', $columnWidths[$column] + 1);
76
        }
77
        $this->gridDisplay .= '+' . PHP_EOL;
78
 
79
        $this->gridDisplay .= str_repeat(' ', $maxRowLength + 2);
80
        foreach ($this->columns as $column => $reference) {
81
            $this->gridDisplay .= '| ' . str_pad((string) $reference, $columnWidths[$column] + 1, ' ');
82
        }
83
        $this->gridDisplay .= '|' . PHP_EOL;
84
 
85
        $this->renderFooter($maxRowLength, $columnWidths);
86
    }
87
 
88
    private function renderFooter(int $maxRowLength, array $columnWidths): void
89
    {
90
        $this->gridDisplay .= '+' . str_repeat('-', $maxRowLength + 1);
91
        foreach ($this->columns as $column => $reference) {
92
            $this->gridDisplay .= '+-';
93
            $this->gridDisplay .= str_pad((string) '', $columnWidths[$column] + 1, '-');
94
        }
95
        $this->gridDisplay .= '+' . PHP_EOL;
96
    }
97
 
98
    private function getColumnWidths(): array
99
    {
100
        $columnCount = count($this->matrix, COUNT_RECURSIVE) / count($this->matrix);
101
        $columnWidths = [];
102
        for ($column = 0; $column < $columnCount; ++$column) {
103
            $columnWidths[] = $this->getColumnWidth(array_column($this->matrix, $column));
104
        }
105
 
106
        return $columnWidths;
107
    }
108
 
109
    private function getColumnWidth(array $columnData): int
110
    {
111
        $columnWidth = 0;
112
        $columnData = array_values($columnData);
113
 
114
        foreach ($columnData as $columnValue) {
115
            if (is_string($columnValue)) {
116
                $columnWidth = max($columnWidth, mb_strlen($columnValue));
117
            } elseif (is_bool($columnValue)) {
118
                $columnWidth = max($columnWidth, mb_strlen($columnValue ? 'TRUE' : 'FALSE'));
119
            }
120
 
121
            $columnWidth = max($columnWidth, mb_strlen((string) $columnWidth));
122
        }
123
 
124
        return $columnWidth;
125
    }
126
}