TableHelper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 3.1.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Shell\Helper;
  16. use Cake\Console\Helper;
  17. /**
  18. * Create a visually pleasing ASCII art table
  19. * from 2 dimensional array data.
  20. */
  21. class TableHelper extends Helper
  22. {
  23. /**
  24. * Calculate the column widths
  25. *
  26. * @param array $rows The rows on which the columns width will be calculated on.
  27. * @return array
  28. */
  29. protected function _calculateWidths($rows)
  30. {
  31. $widths = [];
  32. foreach ($rows as $line) {
  33. for ($i = 0, $len = count($line); $i < $len; $i++) {
  34. $columnLength = mb_strlen($line[$i]);
  35. if ($columnLength > (isset($widths[$i]) ? $widths[$i] : 0)) {
  36. $widths[$i] = $columnLength;
  37. }
  38. }
  39. }
  40. return $widths;
  41. }
  42. /**
  43. * Output a row separator.
  44. *
  45. * @param array $widths The widths of each column to output.
  46. * @return void
  47. */
  48. protected function _rowSeparator($widths)
  49. {
  50. $out = '';
  51. foreach ($widths as $column) {
  52. $out .= '+' . str_repeat('-', $column + 2);
  53. }
  54. $out .= '+';
  55. $this->_io->out($out);
  56. }
  57. /**
  58. * Output a row.
  59. *
  60. * @param array $row The row to ouptut.
  61. * @param array $widths The widths of each column to output.
  62. * @return void
  63. */
  64. protected function _render($row, $widths)
  65. {
  66. $out = '';
  67. foreach ($row as $i => $column) {
  68. $pad = $widths[$i] - mb_strlen($column);
  69. $out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
  70. }
  71. $out .= '|';
  72. $this->_io->out($out);
  73. }
  74. /**
  75. * Output a table.
  76. *
  77. * @param array $rows The data to render out.
  78. * @return void
  79. */
  80. public function output($rows)
  81. {
  82. if (count($rows) === 1) {
  83. $rows = $rows[0];
  84. }
  85. $widths = $this->_calculateWidths($rows);
  86. $this->_rowSeparator($widths);
  87. $this->_render(array_shift($rows), $widths);
  88. $this->_rowSeparator($widths);
  89. foreach ($rows as $line) {
  90. $this->_render($line, $widths);
  91. }
  92. $this->_rowSeparator($widths);
  93. }
  94. }