Debian Codegraph @

vendor/symfony/console/Helper/TableStyle.php

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