PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Helper / TextGrid.php

TextGrid.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Helper/TextGrid.php

206 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Helper;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper;
6
7 class TextGrid
8 {
9 protected bool $isCli;
10
11 /** @var mixed[][] */
12 protected array $matrix;
13
14 /** @var int[] */
15 protected array $rows;
16
17 /** @var string[] */
18 protected array $columns;
19
20 protected string $gridDisplay;
21
22 protected bool $rowDividers = false;
23
24 protected bool $rowHeaders = true;
25
26 protected bool $columnHeaders = true;
27
28 protected TextGridRightAlign $numbersRight = TextGridRightAlign::none;
29
30 /** @param mixed[][] $matrix
31 * @param mixed $numbersRight */
32 public function __construct(array $matrix, bool $isCli = true, bool $rowDividers = false, bool $rowHeaders = true, bool $columnHeaders = true, $numbersRight = TextGridRightAlign::none)
33 {
34 $this->rows = array_keys($matrix);
35 $this->columns = array_keys($matrix[$this->rows[0]]);
36
37 $matrix = array_values($matrix);
38 array_walk(
39 $matrix,
40 function (&$row): void {
41 $row = array_values($row);
42 }
43 );
44
45 $this->matrix = $matrix;
46 $this->isCli = $isCli;
47 $this->rowDividers = $rowDividers;
48 $this->rowHeaders = $rowHeaders;
49 $this->columnHeaders = $columnHeaders;
50 $this->numbersRight = $numbersRight;
51 }
52
53 /**
54 * @param mixed $numbersRight
55 */
56 public function setNumbersRight($numbersRight): void
57 {
58 $this->numbersRight = $numbersRight;
59 }
60
61 public function render(): string
62 {
63 $this->gridDisplay = $this->isCli ? '' : ('<pre>' . PHP_EOL);
64
65 if (!empty($this->rows)) {
66 $maxRow = max($this->rows);
67 $maxRowLength = $this->strlen((string) $maxRow) + 1;
68 $columnWidths = $this->getColumnWidths();
69
70 $this->renderColumnHeader($maxRowLength, $columnWidths);
71 $this->renderRows($maxRowLength, $columnWidths);
72 if (!$this->rowDividers) {
73 $this->renderFooter($maxRowLength, $columnWidths);
74 }
75 }
76
77 $this->gridDisplay .= $this->isCli ? '' : '</pre>';
78
79 return $this->gridDisplay;
80 }
81
82 /** @param int[] $columnWidths */
83 protected function renderRows(int $maxRowLength, array $columnWidths): void
84 {
85 foreach ($this->matrix as $row => $rowData) {
86 if ($this->rowHeaders) {
87 $this->gridDisplay .= '|' . str_pad((string) $this->rows[$row], $maxRowLength, ' ', STR_PAD_LEFT) . ' ';
88 }
89 $this->renderCells($rowData, $columnWidths);
90 $this->gridDisplay .= '|' . PHP_EOL;
91 if ($this->rowDividers) {
92 $this->renderFooter($maxRowLength, $columnWidths);
93 }
94 }
95 }
96
97 /**
98 * @param mixed[] $rowData
99 * @param int[] $columnWidths
100 */
101 protected function renderCells(array $rowData, array $columnWidths): void
102 {
103 foreach ($rowData as $column => $cell) {
104 $valueForLength = $this->getString($cell);
105 $displayCell = $this->isCli ? $valueForLength : htmlentities($valueForLength);
106 $this->gridDisplay .= '| ';
107 if ($this->rightAlign($displayCell, $cell)) {
108 $this->gridDisplay .= str_repeat(' ', $columnWidths[$column] - $this->strlen($valueForLength)) . $displayCell . ' ';
109 } else {
110 $this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - $this->strlen($valueForLength) + 1);
111 }
112 }
113 }
114
115 /**
116 * @param mixed $cell
117 */
118 protected function rightAlign(string $displayCell, $cell = null): bool
119 {
120 return ($this->numbersRight === TextGridRightAlign::numeric && is_numeric($displayCell)) || ($this->numbersRight === TextGridRightAlign::floatOrInt && (is_int($cell) || is_float($cell)));
121 }
122
123 /** @param int[] $columnWidths */
124 protected function renderColumnHeader(int $maxRowLength, array &$columnWidths): void
125 {
126 if (!$this->columnHeaders) {
127 $this->renderFooter($maxRowLength, $columnWidths);
128
129 return;
130 }
131 foreach ($this->columns as $column => $reference) {
132 /** @var string $reference */
133 $columnWidths[$column] = max($columnWidths[$column], $this->strlen($reference));
134 }
135 if ($this->rowHeaders) {
136 $this->gridDisplay .= str_repeat(' ', $maxRowLength + 2);
137 }
138 foreach ($this->columns as $column => $reference) {
139 $this->gridDisplay .= '+-' . str_repeat('-', $columnWidths[$column] + 1);
140 }
141 $this->gridDisplay .= '+' . PHP_EOL;
142
143 if ($this->rowHeaders) {
144 $this->gridDisplay .= str_repeat(' ', $maxRowLength + 2);
145 }
146 foreach ($this->columns as $column => $reference) {
147 /** @var scalar $reference */
148 $this->gridDisplay .= '| ' . str_pad((string) $reference, $columnWidths[$column] + 1, ' ');
149 }
150 $this->gridDisplay .= '|' . PHP_EOL;
151
152 $this->renderFooter($maxRowLength, $columnWidths);
153 }
154
155 /** @param int[] $columnWidths */
156 protected function renderFooter(int $maxRowLength, array $columnWidths): void
157 {
158 if ($this->rowHeaders) {
159 $this->gridDisplay .= '+' . str_repeat('-', $maxRowLength + 1);
160 }
161 foreach ($this->columns as $column => $reference) {
162 $this->gridDisplay .= '+-';
163 $this->gridDisplay .= str_pad((string) '', $columnWidths[$column] + 1, '-');
164 }
165 $this->gridDisplay .= '+' . PHP_EOL;
166 }
167
168 /** @return int[] */
169 protected function getColumnWidths(): array
170 {
171 $columnCount = count($this->matrix, COUNT_RECURSIVE) / count($this->matrix);
172 $columnWidths = [];
173 for ($column = 0; $column < $columnCount; ++$column) {
174 $columnWidths[] = $this->getColumnWidth(array_column($this->matrix, $column));
175 }
176
177 return $columnWidths;
178 }
179
180 /** @param mixed[] $columnData */
181 protected function getColumnWidth(array $columnData): int
182 {
183 $columnWidth = 0;
184 $columnData = array_values($columnData);
185
186 foreach ($columnData as $columnValue) {
187 $columnWidth = max($columnWidth, $this->strlen($this->getString($columnValue)));
188 }
189
190 return $columnWidth;
191 }
192
193 /**
194 * @param mixed $value
195 */
196 protected function getString($value): string
197 {
198 return StringHelper::convertToString($value, true, '', true);
199 }
200
201 protected function strlen(string $value): int
202 {
203 return mb_strlen($value);
204 }
205 }
206