TableStyle.php 12 KB

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