Passed
Push — master ( 7c23c1...b88c58 )
by Kris
08:12 queued 06:30
created

TableStyle   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 59
c 1
b 0
f 0
dl 0
loc 261
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A tableSeparator() 0 12 4
A tableRowStart() 0 3 1
A tableRowCell() 0 6 1
A resetDefaults() 0 7 1
A tableRowEmpty() 0 17 3
A tableRow() 0 23 3
A tableRowSeparator() 0 13 4
1
<?php
2
3
/** 
4
 *        _    _        _ _
5
 *  _ __ (_)__| |_  ___| | |
6
 * | '  \| (_-< ' \/ -_) | |
7
 * |_|_|_|_/__/_||_\___|_|_|
8
 *
9
 * This file is part of Kristuff\Mishell.
10
 * (c) Kr1s7uff For the full copyright and license information, 
11
 * please view the LICENSE file that was distributed with this 
12
 * source code.
13
 *
14
 * @version    1.6.2
15
 * @copyright  2017-2024 Kr157uff
16
 */
17
namespace Kristuff\Mishell;
18
 
19
class TableStyle extends \Kristuff\Mishell\ShellColoredPrinter
20
{
21
    /**
22
     * Align left constant
23
     *
24
     * @const int
25
     */
26
    const ALIGN_LEFT = STR_PAD_RIGHT;
27
28
    /**
29
     * Align right constant
30
     *
31
     * @const int
32
     */
33
    const ALIGN_RIGHT = STR_PAD_LEFT;
34
35
    /**
36
     * Align center constant
37
     *
38
     * @const int
39
     */
40
    const ALIGN_CENTER = STR_PAD_BOTH;
41
42
    /**
43
     * The default horizontal sign. 
44
     *
45
     * @access protected
46
     * @var    string
47
     */
48
    protected static $defaultHorizontalSign = '-';
49
50
    /**
51
     * The default vertical sign. 
52
     *
53
     * @access protected
54
     * @var    string
55
     */
56
    protected static $defaultVerticalSign = '|';
57
58
     /**
59
     * The default vertical sign. 
60
     *
61
     * @access protected
62
     * @var    string
63
     */
64
    protected static $defaultVerticalInnerSign = '+';
65
66
    /**
67
     * The default table cell padding. 
68
     *
69
     * @access protected
70
     * @var    string
71
     */
72
    protected static $defaultCellPadding = ' ';
73
74
    /**
75
     * The cell padding. 
76
     *
77
     * @access public
78
     * @var    string
79
     */
80
    public static $tableCellPadding    = ' ';
81
82
    /**
83
     * The horizontal separator sign. 
84
     *
85
     * @access public
86
     * @var    string
87
     */
88
    public static $horizontalSeparator = '-';
89
90
    /**
91
     * The vertical separator sign. 
92
     *
93
     * @access public
94
     * @var    string
95
     */
96
    public static $verticalSeparator   = '|';
97
98
    /**
99
     * The vertical header separator sign. 
100
     *
101
     * @access public
102
     * @var    string
103
     */
104
    public static $verticalHeaderSeparator   = '+';
105
106
   /**
107
     * The vertical separator sign. 
108
     *
109
     * @access public
110
     * @var    string
111
     */
112
    public static $verticalInnerSeparator   = '+';
113
           
114
    /**
115
     * Resets the default options
116
     *
117
     * @access public
118
     * @static
119
     *
120
     * @return void         
121
     */
122
    public static function resetDefaults()
123
    {
124
        self::$verticalSeparator        = self::$defaultVerticalSign;
125
        self::$horizontalSeparator      = self::$defaultHorizontalSign;
126
        self::$tableCellPadding         = self::$defaultCellPadding;
127
        self::$verticalInnerSeparator   = self::$defaultVerticalInnerSign;
128
        self::$tableCellPadding         = self::$defaultCellPadding;
129
    }
130
131
    /**
132
     * Gets a formatted table row separator
133
     *
134
     * @access public
135
     * @static
136
     * @param  string   [$color]        The text color for the wall row
137
     * @param  string   [$bgcolor]      The back color for the wall row
138
     * @param  string   [$option]+...   The text styles for the wall row
139
     *
140
     * @return string         
141
     */
142
    public static function tableRowSeparator()
143
    {
144
        $args = func_get_args();
145
        $columnsPads = !empty($args) ? $args[0] : [];
146
        array_shift($args);
147
148
        $str = self::$verticalSeparator ;
149
        $cellPaddingLenght = mb_strlen(self::$tableCellPadding) *2;
150
        foreach ($columnsPads as $key => $pad){
151
            $str .= str_repeat(self::$horizontalSeparator , $pad + $cellPaddingLenght) .self::$verticalInnerSeparator;
152
        }
153
154
        return self::getCliString(self::$verticalInnerSeparator === '' ? $str : substr($str, 0, mb_strlen($str) -1). self::$verticalSeparator, $args);
155
    }
156
157
    /**
158
     * Gets a formatted table row separator
159
     *
160
     * @access public
161
     * @static
162
     * @param  string   [$color]        The text color for the wall row
163
     * @param  string   [$bgcolor]      The back color for the wall row
164
     * @param  string   [$option]+...   The text styles for the wall row
165
     *
166
     * @return string         
167
     */
168
    public static function tableSeparator()
169
    {
170
        $args = func_get_args();
171
        $columnsPads = !empty($args) ? $args[0] : [];
172
        array_shift($args);
173
174
        $str = self::$verticalHeaderSeparator ;
175
        $cellPaddingLenght = mb_strlen(self::$tableCellPadding) *2;
176
        foreach ($columnsPads as $key => $pad){
177
            $str .= str_repeat(self::$horizontalSeparator , $pad + $cellPaddingLenght) .self::$verticalInnerSeparator ;
178
        }
179
        return self::getCliString(self::$verticalInnerSeparator === '' ? $str : substr($str, 0, mb_strlen($str) -1). self::$verticalHeaderSeparator, $args);
180
181
    }
182
183
    /**
184
     * Gets a formatted table blank row 
185
     *
186
     * @access public
187
     * @static
188
     * @param  string   [$color]        The text color for the wall row
189
     * @param  string   [$bgcolor]      The back color for the wall row
190
     * @param  string   [$option]+...   The text styles for the wall row
191
     *
192
     * @return string         
193
     */
194
    public static function tableRowEmpty()
195
    {
196
        // get and parse arguments:
197
        //  - extract first argument (columns list)
198
        //  - keep following arguments (if exist) as styles
199
        $args = func_get_args();
200
        $columnsPads = !empty($args) ? $args[0] : [];
201
        array_shift($args);
202
203
        $str = self::$verticalSeparator ;
204
        foreach ($columnsPads as $pad){
205
            $str .= self::$tableCellPadding. 
206
                    str_pad(' ', $pad).
207
                    self::$tableCellPadding. 
208
                    self::$verticalSeparator ;
209
        }
210
        return self::getCliString($str, $args);
211
    }
212
213
    /**
214
     * Return a table row start.  
215
     *
216
     * @access public
217
     * @static
218
     *
219
     * @return string
220
     */
221
    public static function tableRowStart()
222
    {
223
        return self::$verticalSeparator;
224
    }
225
226
    /**
227
     * Return a table cell string.
228
     *
229
     * @access public
230
     * @static
231
     * @param  string   $column         The column text. 
232
     * @param  int      $lenght         The column length. Default is 0 (no pad)
233
     * @param  int      $align          The column alignement (Console::ALIGN_LEFT, Console::ALIGN_RIGHT or Console::ALIGN_CENTER). Default is Console::ALIGN_LEFT.
234
     *
235
     * @return string          
236
     */
237
    public static function tableRowCell($column, $length = 0, $align = self::ALIGN_LEFT)
238
    {
239
        return  self::$tableCellPadding. 
240
                self::pad($column, $length, ' ', $align).
241
                self::$tableCellPadding. 
242
                self::$verticalSeparator;
243
    }
244
245
    /**
246
     * Return a table row string.
247
     *
248
     * @access public
249
     * @static
250
     * @param  array    $columns        The columns arrays. 
251
     * @param  string   [$color]        The text color for the wall row
252
     * @param  string   [$bgcolor]      The back color for the wall row
253
     * @param  string   [$option]+...   The text styles for the wall row
254
     *
255
     * @return string          
256
     */
257
     public static function tableRow()
258
    {
259
        // get and parse arguments:
260
        //  - extract first argument (columns list)
261
        //  - keep following arguments (if exist) as styles
262
        $args       = func_get_args();
263
        $columns    = !empty($args) ? $args[0] : [];
264
        array_shift($args);
265
266
        // build the row string
267
        // start with separator
268
        $str = self::$verticalSeparator;
269
270
        // add columns        
271
        foreach ($columns as $column => $pad){
272
            $str .= self::$tableCellPadding. 
273
                    self::pad($column, $pad).
274
                    self::$tableCellPadding.
275
                    self::$verticalSeparator;
276
        }
277
278
        // format full row
279
        return self::getCliString($str, $args) ; 
280
    }
281
}