CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • ProgressHelper
  • TableHelper
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
 11:  * @since         3.1.0
 12:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 13:  */
 14: namespace Cake\Shell\Helper;
 15: 
 16: use Cake\Console\Helper;
 17: 
 18: /**
 19:  * Create a visually pleasing ASCII art table
 20:  * from 2 dimensional array data.
 21:  */
 22: class TableHelper extends Helper
 23: {
 24:     /**
 25:      * Default config for this helper.
 26:      *
 27:      * @var array
 28:      */
 29:     protected $_defaultConfig = [
 30:         'headers' => true,
 31:         'rowSeparator' => false,
 32:         'headerStyle' => 'info',
 33:     ];
 34: 
 35:     /**
 36:      * Calculate the column widths
 37:      *
 38:      * @param array $rows The rows on which the columns width will be calculated on.
 39:      * @return int[]
 40:      */
 41:     protected function _calculateWidths($rows)
 42:     {
 43:         $widths = [];
 44:         foreach ($rows as $line) {
 45:             foreach (array_values($line) as $k => $v) {
 46:                 $columnLength = $this->_cellWidth($v);
 47:                 if ($columnLength >= (isset($widths[$k]) ? $widths[$k] : 0)) {
 48:                     $widths[$k] = $columnLength;
 49:                 }
 50:             }
 51:         }
 52: 
 53:         return $widths;
 54:     }
 55: 
 56:     /**
 57:      * Get the width of a cell exclusive of style tags.
 58:      *
 59:      * @param string $text The text to calculate a width for.
 60:      * @return int The width of the textual content in visible characters.
 61:      */
 62:     protected function _cellWidth($text)
 63:     {
 64:         if (strpos($text, '<') === false && strpos($text, '>') === false) {
 65:             return mb_strwidth($text);
 66:         }
 67: 
 68:         /** @var array $styles */
 69:         $styles = $this->_io->styles();
 70:         $tags = implode('|', array_keys($styles));
 71:         $text = preg_replace('#</?(?:' . $tags . ')>#', '', $text);
 72: 
 73:         return mb_strwidth($text);
 74:     }
 75: 
 76:     /**
 77:      * Output a row separator.
 78:      *
 79:      * @param int[] $widths The widths of each column to output.
 80:      * @return void
 81:      */
 82:     protected function _rowSeparator($widths)
 83:     {
 84:         $out = '';
 85:         foreach ($widths as $column) {
 86:             $out .= '+' . str_repeat('-', $column + 2);
 87:         }
 88:         $out .= '+';
 89:         $this->_io->out($out);
 90:     }
 91: 
 92:     /**
 93:      * Output a row.
 94:      *
 95:      * @param array $row The row to output.
 96:      * @param int[] $widths The widths of each column to output.
 97:      * @param array $options Options to be passed.
 98:      * @return void
 99:      */
100:     protected function _render(array $row, $widths, $options = [])
101:     {
102:         if (count($row) === 0) {
103:             return;
104:         }
105: 
106:         $out = '';
107:         foreach (array_values($row) as $i => $column) {
108:             $pad = $widths[$i] - $this->_cellWidth($column);
109:             if (!empty($options['style'])) {
110:                 $column = $this->_addStyle($column, $options['style']);
111:             }
112:             $out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
113:         }
114:         $out .= '|';
115:         $this->_io->out($out);
116:     }
117: 
118:     /**
119:      * Output a table.
120:      *
121:      * Data will be output based on the order of the values
122:      * in the array. The keys will not be used to align data.
123:      *
124:      * @param array $rows The data to render out.
125:      * @return void
126:      */
127:     public function output($rows)
128:     {
129:         if (!is_array($rows) || count($rows) === 0) {
130:             return;
131:         }
132: 
133:         $config = $this->getConfig();
134:         $widths = $this->_calculateWidths($rows);
135: 
136:         $this->_rowSeparator($widths);
137:         if ($config['headers'] === true) {
138:             $this->_render(array_shift($rows), $widths, ['style' => $config['headerStyle']]);
139:             $this->_rowSeparator($widths);
140:         }
141: 
142:         if (!$rows) {
143:             return;
144:         }
145: 
146:         foreach ($rows as $line) {
147:             $this->_render($line, $widths);
148:             if ($config['rowSeparator'] === true) {
149:                 $this->_rowSeparator($widths);
150:             }
151:         }
152:         if ($config['rowSeparator'] !== true) {
153:             $this->_rowSeparator($widths);
154:         }
155:     }
156: 
157:     /**
158:      * Add style tags
159:      *
160:      * @param string $text The text to be surrounded
161:      * @param string $style The style to be applied
162:      * @return string
163:      */
164:     protected function _addStyle($text, $style)
165:     {
166:         return '<' . $style . '>' . $text . '</' . $style . '>';
167:     }
168: }
169: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs