TableHelper.php 6.0 KB

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