TableStyle.php 15 KB

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