TableStyle.php 13 KB

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