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