| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Console\Helper;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Console\Exception\InvalidArgumentException;
|
|
|
15 |
use Symfony\Component\Console\Exception\LogicException;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Defines the styles for a Table.
|
|
|
19 |
*
|
|
|
20 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
21 |
* @author Саша Стаменковић <umpirsky@gmail.com>
|
|
|
22 |
* @author Dany Maillard <danymaillard93b@gmail.com>
|
|
|
23 |
*/
|
|
|
24 |
class TableStyle
|
|
|
25 |
{
|
|
|
26 |
private string $paddingChar = ' ';
|
|
|
27 |
private string $horizontalOutsideBorderChar = '-';
|
|
|
28 |
private string $horizontalInsideBorderChar = '-';
|
|
|
29 |
private string $verticalOutsideBorderChar = '|';
|
|
|
30 |
private string $verticalInsideBorderChar = '|';
|
|
|
31 |
private string $crossingChar = '+';
|
|
|
32 |
private string $crossingTopRightChar = '+';
|
|
|
33 |
private string $crossingTopMidChar = '+';
|
|
|
34 |
private string $crossingTopLeftChar = '+';
|
|
|
35 |
private string $crossingMidRightChar = '+';
|
|
|
36 |
private string $crossingBottomRightChar = '+';
|
|
|
37 |
private string $crossingBottomMidChar = '+';
|
|
|
38 |
private string $crossingBottomLeftChar = '+';
|
|
|
39 |
private string $crossingMidLeftChar = '+';
|
|
|
40 |
private string $crossingTopLeftBottomChar = '+';
|
|
|
41 |
private string $crossingTopMidBottomChar = '+';
|
|
|
42 |
private string $crossingTopRightBottomChar = '+';
|
|
|
43 |
private string $headerTitleFormat = '<fg=black;bg=white;options=bold> %s </>';
|
|
|
44 |
private string $footerTitleFormat = '<fg=black;bg=white;options=bold> %s </>';
|
|
|
45 |
private string $cellHeaderFormat = '<info>%s</info>';
|
|
|
46 |
private string $cellRowFormat = '%s';
|
|
|
47 |
private string $cellRowContentFormat = ' %s ';
|
|
|
48 |
private string $borderFormat = '%s';
|
|
|
49 |
private int $padType = \STR_PAD_RIGHT;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Sets padding character, used for cell padding.
|
|
|
53 |
*
|
|
|
54 |
* @return $this
|
|
|
55 |
*/
|
|
|
56 |
public function setPaddingChar(string $paddingChar): static
|
|
|
57 |
{
|
|
|
58 |
if (!$paddingChar) {
|
|
|
59 |
throw new LogicException('The padding char must not be empty.');
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
$this->paddingChar = $paddingChar;
|
|
|
63 |
|
|
|
64 |
return $this;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Gets padding character, used for cell padding.
|
|
|
69 |
*/
|
|
|
70 |
public function getPaddingChar(): string
|
|
|
71 |
{
|
|
|
72 |
return $this->paddingChar;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Sets horizontal border characters.
|
|
|
77 |
*
|
|
|
78 |
* <code>
|
|
|
79 |
* ╔═══════════════╤══════════════════════════╤══════════════════╗
|
|
|
80 |
* 1 ISBN 2 Title │ Author ║
|
|
|
81 |
* ╠═══════════════╪══════════════════════════╪══════════════════╣
|
|
|
82 |
* ║ 99921-58-10-7 │ Divine Comedy │ Dante Alighieri ║
|
|
|
83 |
* ║ 9971-5-0210-0 │ A Tale of Two Cities │ Charles Dickens ║
|
|
|
84 |
* ║ 960-425-059-0 │ The Lord of the Rings │ J. R. R. Tolkien ║
|
|
|
85 |
* ║ 80-902734-1-6 │ And Then There Were None │ Agatha Christie ║
|
|
|
86 |
* ╚═══════════════╧══════════════════════════╧══════════════════╝
|
|
|
87 |
* </code>
|
|
|
88 |
*
|
|
|
89 |
* @return $this
|
|
|
90 |
*/
|
|
|
91 |
public function setHorizontalBorderChars(string $outside, string $inside = null): static
|
|
|
92 |
{
|
|
|
93 |
$this->horizontalOutsideBorderChar = $outside;
|
|
|
94 |
$this->horizontalInsideBorderChar = $inside ?? $outside;
|
|
|
95 |
|
|
|
96 |
return $this;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Sets vertical border characters.
|
|
|
101 |
*
|
|
|
102 |
* <code>
|
|
|
103 |
* ╔═══════════════╤══════════════════════════╤══════════════════╗
|
|
|
104 |
* ║ ISBN │ Title │ Author ║
|
|
|
105 |
* ╠═══════1═══════╪══════════════════════════╪══════════════════╣
|
|
|
106 |
* ║ 99921-58-10-7 │ Divine Comedy │ Dante Alighieri ║
|
|
|
107 |
* ║ 9971-5-0210-0 │ A Tale of Two Cities │ Charles Dickens ║
|
|
|
108 |
* ╟───────2───────┼──────────────────────────┼──────────────────╢
|
|
|
109 |
* ║ 960-425-059-0 │ The Lord of the Rings │ J. R. R. Tolkien ║
|
|
|
110 |
* ║ 80-902734-1-6 │ And Then There Were None │ Agatha Christie ║
|
|
|
111 |
* ╚═══════════════╧══════════════════════════╧══════════════════╝
|
|
|
112 |
* </code>
|
|
|
113 |
*
|
|
|
114 |
* @return $this
|
|
|
115 |
*/
|
|
|
116 |
public function setVerticalBorderChars(string $outside, string $inside = null): static
|
|
|
117 |
{
|
|
|
118 |
$this->verticalOutsideBorderChar = $outside;
|
|
|
119 |
$this->verticalInsideBorderChar = $inside ?? $outside;
|
|
|
120 |
|
|
|
121 |
return $this;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Gets border characters.
|
|
|
126 |
*
|
|
|
127 |
* @internal
|
|
|
128 |
*/
|
|
|
129 |
public function getBorderChars(): array
|
|
|
130 |
{
|
|
|
131 |
return [
|
|
|
132 |
$this->horizontalOutsideBorderChar,
|
|
|
133 |
$this->verticalOutsideBorderChar,
|
|
|
134 |
$this->horizontalInsideBorderChar,
|
|
|
135 |
$this->verticalInsideBorderChar,
|
|
|
136 |
];
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Sets crossing characters.
|
|
|
141 |
*
|
|
|
142 |
* Example:
|
|
|
143 |
* <code>
|
|
|
144 |
* 1═══════════════2══════════════════════════2══════════════════3
|
|
|
145 |
* ║ ISBN │ Title │ Author ║
|
|
|
146 |
* 8'══════════════0'═════════════════════════0'═════════════════4'
|
|
|
147 |
* ║ 99921-58-10-7 │ Divine Comedy │ Dante Alighieri ║
|
|
|
148 |
* ║ 9971-5-0210-0 │ A Tale of Two Cities │ Charles Dickens ║
|
|
|
149 |
* 8───────────────0──────────────────────────0──────────────────4
|
|
|
150 |
* ║ 960-425-059-0 │ The Lord of the Rings │ J. R. R. Tolkien ║
|
|
|
151 |
* ║ 80-902734-1-6 │ And Then There Were None │ Agatha Christie ║
|
|
|
152 |
* 7═══════════════6══════════════════════════6══════════════════5
|
|
|
153 |
* </code>
|
|
|
154 |
*
|
|
|
155 |
* @param string $cross Crossing char (see #0 of example)
|
|
|
156 |
* @param string $topLeft Top left char (see #1 of example)
|
|
|
157 |
* @param string $topMid Top mid char (see #2 of example)
|
|
|
158 |
* @param string $topRight Top right char (see #3 of example)
|
|
|
159 |
* @param string $midRight Mid right char (see #4 of example)
|
|
|
160 |
* @param string $bottomRight Bottom right char (see #5 of example)
|
|
|
161 |
* @param string $bottomMid Bottom mid char (see #6 of example)
|
|
|
162 |
* @param string $bottomLeft Bottom left char (see #7 of example)
|
|
|
163 |
* @param string $midLeft Mid left char (see #8 of example)
|
|
|
164 |
* @param string|null $topLeftBottom Top left bottom char (see #8' of example), equals to $midLeft if null
|
|
|
165 |
* @param string|null $topMidBottom Top mid bottom char (see #0' of example), equals to $cross if null
|
|
|
166 |
* @param string|null $topRightBottom Top right bottom char (see #4' of example), equals to $midRight if null
|
|
|
167 |
*
|
|
|
168 |
* @return $this
|
|
|
169 |
*/
|
|
|
170 |
public function setCrossingChars(string $cross, string $topLeft, string $topMid, string $topRight, string $midRight, string $bottomRight, string $bottomMid, string $bottomLeft, string $midLeft, string $topLeftBottom = null, string $topMidBottom = null, string $topRightBottom = null): static
|
|
|
171 |
{
|
|
|
172 |
$this->crossingChar = $cross;
|
|
|
173 |
$this->crossingTopLeftChar = $topLeft;
|
|
|
174 |
$this->crossingTopMidChar = $topMid;
|
|
|
175 |
$this->crossingTopRightChar = $topRight;
|
|
|
176 |
$this->crossingMidRightChar = $midRight;
|
|
|
177 |
$this->crossingBottomRightChar = $bottomRight;
|
|
|
178 |
$this->crossingBottomMidChar = $bottomMid;
|
|
|
179 |
$this->crossingBottomLeftChar = $bottomLeft;
|
|
|
180 |
$this->crossingMidLeftChar = $midLeft;
|
|
|
181 |
$this->crossingTopLeftBottomChar = $topLeftBottom ?? $midLeft;
|
|
|
182 |
$this->crossingTopMidBottomChar = $topMidBottom ?? $cross;
|
|
|
183 |
$this->crossingTopRightBottomChar = $topRightBottom ?? $midRight;
|
|
|
184 |
|
|
|
185 |
return $this;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
* Sets default crossing character used for each cross.
|
|
|
190 |
*
|
|
|
191 |
* @see {@link setCrossingChars()} for setting each crossing individually.
|
|
|
192 |
*/
|
|
|
193 |
public function setDefaultCrossingChar(string $char): self
|
|
|
194 |
{
|
|
|
195 |
return $this->setCrossingChars($char, $char, $char, $char, $char, $char, $char, $char, $char);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Gets crossing character.
|
|
|
200 |
*/
|
|
|
201 |
public function getCrossingChar(): string
|
|
|
202 |
{
|
|
|
203 |
return $this->crossingChar;
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Gets crossing characters.
|
|
|
208 |
*
|
|
|
209 |
* @internal
|
|
|
210 |
*/
|
|
|
211 |
public function getCrossingChars(): array
|
|
|
212 |
{
|
|
|
213 |
return [
|
|
|
214 |
$this->crossingChar,
|
|
|
215 |
$this->crossingTopLeftChar,
|
|
|
216 |
$this->crossingTopMidChar,
|
|
|
217 |
$this->crossingTopRightChar,
|
|
|
218 |
$this->crossingMidRightChar,
|
|
|
219 |
$this->crossingBottomRightChar,
|
|
|
220 |
$this->crossingBottomMidChar,
|
|
|
221 |
$this->crossingBottomLeftChar,
|
|
|
222 |
$this->crossingMidLeftChar,
|
|
|
223 |
$this->crossingTopLeftBottomChar,
|
|
|
224 |
$this->crossingTopMidBottomChar,
|
|
|
225 |
$this->crossingTopRightBottomChar,
|
|
|
226 |
];
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Sets header cell format.
|
|
|
231 |
*
|
|
|
232 |
* @return $this
|
|
|
233 |
*/
|
|
|
234 |
public function setCellHeaderFormat(string $cellHeaderFormat): static
|
|
|
235 |
{
|
|
|
236 |
$this->cellHeaderFormat = $cellHeaderFormat;
|
|
|
237 |
|
|
|
238 |
return $this;
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* Gets header cell format.
|
|
|
243 |
*/
|
|
|
244 |
public function getCellHeaderFormat(): string
|
|
|
245 |
{
|
|
|
246 |
return $this->cellHeaderFormat;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
/**
|
|
|
250 |
* Sets row cell format.
|
|
|
251 |
*
|
|
|
252 |
* @return $this
|
|
|
253 |
*/
|
|
|
254 |
public function setCellRowFormat(string $cellRowFormat): static
|
|
|
255 |
{
|
|
|
256 |
$this->cellRowFormat = $cellRowFormat;
|
|
|
257 |
|
|
|
258 |
return $this;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Gets row cell format.
|
|
|
263 |
*/
|
|
|
264 |
public function getCellRowFormat(): string
|
|
|
265 |
{
|
|
|
266 |
return $this->cellRowFormat;
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
/**
|
|
|
270 |
* Sets row cell content format.
|
|
|
271 |
*
|
|
|
272 |
* @return $this
|
|
|
273 |
*/
|
|
|
274 |
public function setCellRowContentFormat(string $cellRowContentFormat): static
|
|
|
275 |
{
|
|
|
276 |
$this->cellRowContentFormat = $cellRowContentFormat;
|
|
|
277 |
|
|
|
278 |
return $this;
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* Gets row cell content format.
|
|
|
283 |
*/
|
|
|
284 |
public function getCellRowContentFormat(): string
|
|
|
285 |
{
|
|
|
286 |
return $this->cellRowContentFormat;
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* Sets table border format.
|
|
|
291 |
*
|
|
|
292 |
* @return $this
|
|
|
293 |
*/
|
|
|
294 |
public function setBorderFormat(string $borderFormat): static
|
|
|
295 |
{
|
|
|
296 |
$this->borderFormat = $borderFormat;
|
|
|
297 |
|
|
|
298 |
return $this;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
/**
|
|
|
302 |
* Gets table border format.
|
|
|
303 |
*/
|
|
|
304 |
public function getBorderFormat(): string
|
|
|
305 |
{
|
|
|
306 |
return $this->borderFormat;
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
/**
|
|
|
310 |
* Sets cell padding type.
|
|
|
311 |
*
|
|
|
312 |
* @return $this
|
|
|
313 |
*/
|
|
|
314 |
public function setPadType(int $padType): static
|
|
|
315 |
{
|
|
|
316 |
if (!\in_array($padType, [\STR_PAD_LEFT, \STR_PAD_RIGHT, \STR_PAD_BOTH], true)) {
|
|
|
317 |
throw new InvalidArgumentException('Invalid padding type. Expected one of (STR_PAD_LEFT, STR_PAD_RIGHT, STR_PAD_BOTH).');
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
$this->padType = $padType;
|
|
|
321 |
|
|
|
322 |
return $this;
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
/**
|
|
|
326 |
* Gets cell padding type.
|
|
|
327 |
*/
|
|
|
328 |
public function getPadType(): int
|
|
|
329 |
{
|
|
|
330 |
return $this->padType;
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
public function getHeaderTitleFormat(): string
|
|
|
334 |
{
|
|
|
335 |
return $this->headerTitleFormat;
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
/**
|
|
|
339 |
* @return $this
|
|
|
340 |
*/
|
|
|
341 |
public function setHeaderTitleFormat(string $format): static
|
|
|
342 |
{
|
|
|
343 |
$this->headerTitleFormat = $format;
|
|
|
344 |
|
|
|
345 |
return $this;
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
public function getFooterTitleFormat(): string
|
|
|
349 |
{
|
|
|
350 |
return $this->footerTitleFormat;
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
/**
|
|
|
354 |
* @return $this
|
|
|
355 |
*/
|
|
|
356 |
public function setFooterTitleFormat(string $format): static
|
|
|
357 |
{
|
|
|
358 |
$this->footerTitleFormat = $format;
|
|
|
359 |
|
|
|
360 |
return $this;
|
|
|
361 |
}
|
|
|
362 |
}
|