TableStyle.php 12 KB

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