No Description

TableHelper.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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\Output\OutputInterface;
  12. use Symfony\Component\Console\Output\NullOutput;
  13. /**
  14. * Provides helpers to display table output.
  15. *
  16. * @author Саша Стаменковић <umpirsky@gmail.com>
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. *
  19. * @deprecated Deprecated since 2.5, to be removed in 3.0; use Table instead.
  20. */
  21. class TableHelper extends Helper
  22. {
  23. const LAYOUT_DEFAULT = 0;
  24. const LAYOUT_BORDERLESS = 1;
  25. const LAYOUT_COMPACT = 2;
  26. /**
  27. * @var Table
  28. */
  29. private $table;
  30. public function __construct()
  31. {
  32. $this->table = new Table(new NullOutput());
  33. }
  34. /**
  35. * Sets table layout type.
  36. *
  37. * @param int $layout self::LAYOUT_*
  38. *
  39. * @return TableHelper
  40. *
  41. * @throws \InvalidArgumentException when the table layout is not known
  42. */
  43. public function setLayout($layout)
  44. {
  45. switch ($layout) {
  46. case self::LAYOUT_BORDERLESS:
  47. $this->table->setStyle('borderless');
  48. break;
  49. case self::LAYOUT_COMPACT:
  50. $this->table->setStyle('compact');
  51. break;
  52. case self::LAYOUT_DEFAULT:
  53. $this->table->setStyle('default');
  54. break;
  55. default:
  56. throw new \InvalidArgumentException(sprintf('Invalid table layout "%s".', $layout));
  57. };
  58. return $this;
  59. }
  60. public function setHeaders(array $headers)
  61. {
  62. $this->table->setHeaders($headers);
  63. return $this;
  64. }
  65. public function setRows(array $rows)
  66. {
  67. $this->table->setRows($rows);
  68. return $this;
  69. }
  70. public function addRows(array $rows)
  71. {
  72. $this->table->addRows($rows);
  73. return $this;
  74. }
  75. public function addRow(array $row)
  76. {
  77. $this->table->addRow($row);
  78. return $this;
  79. }
  80. public function setRow($column, array $row)
  81. {
  82. $this->table->setRow($column, $row);
  83. return $this;
  84. }
  85. /**
  86. * Sets padding character, used for cell padding.
  87. *
  88. * @param string $paddingChar
  89. *
  90. * @return TableHelper
  91. */
  92. public function setPaddingChar($paddingChar)
  93. {
  94. $this->table->getStyle()->setPaddingChar($paddingChar);
  95. return $this;
  96. }
  97. /**
  98. * Sets horizontal border character.
  99. *
  100. * @param string $horizontalBorderChar
  101. *
  102. * @return TableHelper
  103. */
  104. public function setHorizontalBorderChar($horizontalBorderChar)
  105. {
  106. $this->table->getStyle()->setHorizontalBorderChar($horizontalBorderChar);
  107. return $this;
  108. }
  109. /**
  110. * Sets vertical border character.
  111. *
  112. * @param string $verticalBorderChar
  113. *
  114. * @return TableHelper
  115. */
  116. public function setVerticalBorderChar($verticalBorderChar)
  117. {
  118. $this->table->getStyle()->setVerticalBorderChar($verticalBorderChar);
  119. return $this;
  120. }
  121. /**
  122. * Sets crossing character.
  123. *
  124. * @param string $crossingChar
  125. *
  126. * @return TableHelper
  127. */
  128. public function setCrossingChar($crossingChar)
  129. {
  130. $this->table->getStyle()->setCrossingChar($crossingChar);
  131. return $this;
  132. }
  133. /**
  134. * Sets header cell format.
  135. *
  136. * @param string $cellHeaderFormat
  137. *
  138. * @return TableHelper
  139. */
  140. public function setCellHeaderFormat($cellHeaderFormat)
  141. {
  142. $this->table->getStyle()->setCellHeaderFormat($cellHeaderFormat);
  143. return $this;
  144. }
  145. /**
  146. * Sets row cell format.
  147. *
  148. * @param string $cellRowFormat
  149. *
  150. * @return TableHelper
  151. */
  152. public function setCellRowFormat($cellRowFormat)
  153. {
  154. $this->table->getStyle()->setCellHeaderFormat($cellRowFormat);
  155. return $this;
  156. }
  157. /**
  158. * Sets row cell content format.
  159. *
  160. * @param string $cellRowContentFormat
  161. *
  162. * @return TableHelper
  163. */
  164. public function setCellRowContentFormat($cellRowContentFormat)
  165. {
  166. $this->table->getStyle()->setCellRowContentFormat($cellRowContentFormat);
  167. return $this;
  168. }
  169. /**
  170. * Sets table border format.
  171. *
  172. * @param string $borderFormat
  173. *
  174. * @return TableHelper
  175. */
  176. public function setBorderFormat($borderFormat)
  177. {
  178. $this->table->getStyle()->setBorderFormat($borderFormat);
  179. return $this;
  180. }
  181. /**
  182. * Sets cell padding type.
  183. *
  184. * @param int $padType STR_PAD_*
  185. *
  186. * @return TableHelper
  187. */
  188. public function setPadType($padType)
  189. {
  190. $this->table->getStyle()->setPadType($padType);
  191. return $this;
  192. }
  193. /**
  194. * Renders table to output.
  195. *
  196. * Example:
  197. * +---------------+-----------------------+------------------+
  198. * | ISBN | Title | Author |
  199. * +---------------+-----------------------+------------------+
  200. * | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
  201. * | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
  202. * | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
  203. * +---------------+-----------------------+------------------+
  204. *
  205. * @param OutputInterface $output
  206. */
  207. public function render(OutputInterface $output)
  208. {
  209. $p = new \ReflectionProperty($this->table, 'output');
  210. $p->setAccessible(true);
  211. $p->setValue($this->table, $output);
  212. $this->table->render();
  213. }
  214. /**
  215. * {@inheritdoc}
  216. */
  217. public function getName()
  218. {
  219. return 'table';
  220. }
  221. }