TableStyle.php 13 KB

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