TableStyle.php 12 KB

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