TableHelper.php 6.0 KB

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