Passed
Pull Request — master (#12)
by Kris
05:47
created

ShellTablePrinter::tableRow()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 23
rs 9.9332
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
/* 
4
 *   __  __  _       _            _  _
5
 *  |  \/  |(_) ___ | |__    ___ | || |
6
 *  | |\/| || |/ __|| '_ \  / _ \| || |
7
 *  | |  | || |\__ \| | | ||  __/| || |
8
 *  |_|  |_||_||___/|_| |_| \___||_||_|
9
 *
10
 * This file is part of Kristuff\Mishell.
11
 * (c) Kristuff <[email protected]>
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 *
16
 * @version    1.1.0
17
 * @copyright  2017-2020 Kristuff
18
 */
19
20
namespace Kristuff\Mishell;
21
 
22
abstract class ShellTablePrinter extends \Kristuff\Mishell\ShellColoredPrinter
23
{
24
    /**
25
     * Align left constant
26
     *
27
     * @const    int
28
     */
29
    const ALIGN_LEFT = STR_PAD_RIGHT;
30
31
    /**
32
     * Align right constant
33
     *
34
     * @const    int
35
     */
36
    const ALIGN_RIGHT = STR_PAD_LEFT;
37
38
    /**
39
     * Align center constant
40
     *
41
     * @const    int
42
     */
43
    const ALIGN_CENTER = STR_PAD_BOTH;
44
45
    /**
46
     * The default horizontal sign. 
47
     *
48
     * @access protected
49
     * @var    string
50
     */
51
    protected static $defaultHorizontalSign = '-';
52
53
    /**
54
     * The default vertical sign. 
55
     *
56
     * @access protected
57
     * @var    string
58
     */
59
    protected static $defaultVerticalSign = '|';
60
61
     /**
62
     * The default vertical sign. 
63
     *
64
     * @access protected
65
     * @var    string
66
     */
67
    protected static $defaultVerticalInnerSign = '+';
68
69
    /**
70
     * The default table cell padding. 
71
     *
72
     * @access protected
73
     * @var    string
74
     */
75
    protected static $defaultCellPadding = ' ';
76
77
    /**
78
     * The cell padding. 
79
     *
80
     * @access public
81
     * @var    string
82
     */
83
    public static $tableCellPadding    = ' ';
84
85
    /**
86
     * The horizontal separator sign. 
87
     *
88
     * @access public
89
     * @var    string
90
     */
91
    public static $horizontalSeparator = '-';
92
93
    /**
94
     * The vertical separator sign. 
95
     *
96
     * @access public
97
     * @var    string
98
     */
99
    public static $verticalSeparator   = '|';
100
101
   /**
102
     * The vertical separator sign. 
103
     *
104
     * @access public
105
     * @var    string
106
     */
107
    public static $verticalInnerSeparator   = '+';
108
           
109
    /**
110
     * Resets the default options
111
     *
112
     * @access public
113
     * @static method
114
     *
115
     * @return void         
116
     */
117
    public static function resetDefaults()
118
    {
119
        self::$verticalSeparator        = self::$defaultVerticalSign;
120
        self::$horizontalSeparator      = self::$defaultHorizontalSign;
121
        self::$tableCellPadding         = self::$defaultCellPadding;
122
        self::$verticalInnerSeparator   = self::$defaultVerticalInnerSign;
123
    }
124
125
    /**
126
     * Gets a formated table row separator
127
     *
128
     * @access public
129
     * @static method
130
     * @param  string   [$color]        The text color for the wall row
131
     * @param  string   [$bgcolor]      The back color for the wall row
132
     * @param  string   [$option]+...   The text styles for the wall row
133
     *
134
     * @return string         
135
     */
136
    public static function tableRowSeparator()
137
    {
138
        $args = func_get_args();
139
        $columnsPads = !empty($args) ? $args[0] : [];
140
        array_shift($args);
141
142
        $str = self::$verticalSeparator ;
143
        $cellPaddingLenght = strlen(self::$tableCellPadding) *2;
144
        foreach ($columnsPads as $key => $pad){
145
            $str .= str_repeat(self::$horizontalSeparator , $pad + $cellPaddingLenght) .self::$verticalInnerSeparator ;
146
        }
147
148
        return self::getCliString(self::$verticalInnerSeparator === '' ? $str : substr($str, 0, strlen($str) -1). self::$verticalSeparator, $args);
149
    }
150
151
     /**
152
     * Gets a formated table row separator
153
     *
154
     * @access public
155
     * @static method
156
     * @param  string   [$color]        The text color for the wall row
157
     * @param  string   [$bgcolor]      The back color for the wall row
158
     * @param  string   [$option]+...   The text styles for the wall row
159
     *
160
     * @return string         
161
     */
162
    public static function tableSeparator()
163
    {
164
        $args = func_get_args();
165
        $columnsPads = !empty($args) ? $args[0] : [];
166
        array_shift($args);
167
168
        $str = self::$verticalSeparator ;
169
        $cellPaddingLenght = strlen(self::$tableCellPadding) *2;
170
        foreach ($columnsPads as $key => $pad){
171
            $str .= str_repeat(self::$horizontalSeparator , $pad + $cellPaddingLenght) .self::$horizontalSeparator;
172
        }
173
        return self::getCliString(substr($str, 0, strlen($str) -1). self::$verticalSeparator, $args);
174
    }
175
176
    /**
177
     * Gets a formated table blank row 
178
     *
179
     * @access public
180
     * @static method
181
     * @param  string   [$color]        The text color for the wall row
182
     * @param  string   [$bgcolor]      The back color for the wall row
183
     * @param  string   [$option]+...   The text styles for the wall row
184
     *
185
     * @return string         
186
     */
187
    public static function tableRowEmpty()
188
    {
189
        // get and parse arguments:
190
        //  - extract first argument (columns list)
191
        //  - keep following arguments (if exist) as styles
192
        $args = func_get_args();
193
        $columnsPads = !empty($args) ? $args[0] : [];
194
        array_shift($args);
195
196
        $str = self::$verticalSeparator ;
197
        foreach ($columnsPads as $pad){
198
            $str .= self::$tableCellPadding. 
199
                    str_pad(' ', $pad).
200
                    self::$tableCellPadding. 
201
                    self::$verticalSeparator ;
202
        }
203
        return self::getCliString($str, $args);
204
    }
205
206
    /**
207
     * Return a table row start.  
208
     *
209
     * @access public
210
     * @static method
211
     *
212
     * @return void         
213
     */
214
    public static function tableRowStart()
215
    {
216
        return self::$verticalSeparator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::verticalSeparator returns the type string which is incompatible with the documented return type void.
Loading history...
217
    }
218
219
    /**
220
     * Return a table cell string.
221
     *
222
     * @access public
223
     * @static method
224
     * @param  string   $column         The column text. 
225
     * @param  int      $lenght         The column length. Default is 0 (no pad)
226
     * @param  int      $align          The column alignement (Console::ALIGN_LEFT, Console::ALIGN_RIGHT or Console::ALIGN_CENTER). Default is Console::ALIGN_LEFT.
227
     *
228
     * @return string          
229
     */
230
    public static function tableRowCell($column, $length = 0, $align = self::ALIGN_LEFT)
231
    {
232
        return  self::$tableCellPadding. 
233
                self::pad($column, $length, ' ', $align).
0 ignored issues
show
Bug Best Practice introduced by
The method Kristuff\Mishell\ShellColoredPrinter::pad() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

233
                self::/** @scrutinizer ignore-call */ 
234
                      pad($column, $length, ' ', $align).
Loading history...
234
                self::$tableCellPadding. 
235
                self::$verticalSeparator;
236
    }
237
238
    /**
239
     * Return a table row string.
240
     *
241
     * @access public
242
     * @static method
243
     * @param  array    $columns        The columns arrays. 
244
     * @param  string   [$color]        The text color for the wall row
245
     * @param  string   [$bgcolor]      The back color for the wall row
246
     * @param  string   [$option]+...   The text styles for the wall row
247
     *
248
     * @return string          
249
     */
250
     public static function tableRow()
251
    {
252
        // get and parse arguments:
253
        //  - extract first argument (columns list)
254
        //  - keep following arguments (if exist) as styles
255
        $args       = func_get_args();
256
        $columns    = !empty($args) ? $args[0] : [];
257
        array_shift($args);
258
259
        // build the row string
260
        // start with separator
261
        $str = self::$verticalSeparator;
262
263
        // add columns        
264
        foreach ($columns as $column => $pad){
265
            $str .= self::$tableCellPadding. 
266
                    self::pad($column, $pad).
0 ignored issues
show
Bug Best Practice introduced by
The method Kristuff\Mishell\ShellColoredPrinter::pad() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

266
                    self::/** @scrutinizer ignore-call */ 
267
                          pad($column, $pad).
Loading history...
267
                    self::$tableCellPadding.
268
                    self::$verticalSeparator;
269
        }
270
271
        // format full row
272
        return self::getCliString($str, $args) ; 
273
    }
274
}