TableStyle.php 15KB

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