|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Lenevor Framework |
|
5
|
|
|
* |
|
6
|
|
|
* LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
|
9
|
|
|
* with this package in the file license.md. |
|
10
|
|
|
* It is also available through the world-wide-web at this URL: |
|
11
|
|
|
* https://lenevor.com/license |
|
12
|
|
|
* If you did not receive a copy of the license and are unable to |
|
13
|
|
|
* obtain it through the world-wide-web, please send an email |
|
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Lenevor |
|
17
|
|
|
* @subpackage Base |
|
18
|
|
|
* @link https://lenevor.com |
|
19
|
|
|
* @copyright Copyright (c) 2019 - 2023 Alexander Campo <[email protected]> |
|
20
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace Syscodes\Components\Console\Util; |
|
24
|
|
|
|
|
25
|
|
|
use Syscodes\Components\Support\Arr; |
|
26
|
|
|
use Syscodes\Components\Support\Str; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* This class allows to format data useful for console. |
|
30
|
|
|
*/ |
|
31
|
|
|
final class FormatUtil |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Slice array. |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $data |
|
37
|
|
|
* @param array $options |
|
38
|
|
|
* |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function spliceKeyValue(array $data, array $options = []): string |
|
42
|
|
|
{ |
|
43
|
|
|
$text = ''; |
|
44
|
|
|
|
|
45
|
|
|
$options = array_merge([ |
|
46
|
|
|
'leftChar' => '', // e.g ' ', ' * ' |
|
47
|
|
|
'sepChar' => ' ', // e.g ' | ' OUT: key | value |
|
48
|
|
|
'keyStyle' => '', // e.g 'info','comment' |
|
49
|
|
|
'valStyle' => '', // e.g 'info','comment' |
|
50
|
|
|
'keyMinWidth' => 8, |
|
51
|
|
|
'keyMaxWidth' => 0, |
|
52
|
|
|
'keyPadPos' => 'right', |
|
53
|
|
|
'ucFirst' => true, // upper first char for value |
|
54
|
|
|
], $options); |
|
55
|
|
|
|
|
56
|
|
|
$keyStyle = trim($options['keyStyle']); |
|
57
|
|
|
$keyPadPos = (string) $options['keyPadPos']; |
|
58
|
|
|
|
|
59
|
|
|
if ($options['keyMaxWidth'] < 1) { |
|
60
|
|
|
$options['keyMaxWidth'] = Arr::getMaxWidth($data); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// compare |
|
64
|
|
|
if ((int) $options['keyMinWidth'] > $options['keyMaxWidth']) { |
|
65
|
|
|
$options['keyMaxWidth'] = $options['keyMinWidth']; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
foreach ($data as $key => $value) { |
|
69
|
|
|
$hasKey = ! is_int($key); |
|
70
|
|
|
$text .= $options['leftChar']; |
|
71
|
|
|
|
|
72
|
|
|
if ($hasKey) { |
|
73
|
|
|
$key = Str::padBoth((string) $key, $options['keyMaxWidth'], ' ', $keyPadPos); |
|
|
|
|
|
|
74
|
|
|
$text .= static::wrap($key, $keyStyle).$options['sepChar']; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// if value is array, translate array to string |
|
78
|
|
|
if (is_array($value)) { |
|
79
|
|
|
$temp = '['; |
|
80
|
|
|
|
|
81
|
|
|
foreach ($value as $k => $val) { |
|
82
|
|
|
if (is_bool($val)) { |
|
83
|
|
|
$val = $val ? '(True)' : '(False)'; |
|
84
|
|
|
} else { |
|
85
|
|
|
$val = is_scalar($val) ? (string) $val : $val; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$temp .= ( ! is_numeric($k) ? "$k: " : '') . "$val, "; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$value = rtrim($temp, ' ,') . ']'; |
|
92
|
|
|
} elseif (is_bool($value)) { |
|
93
|
|
|
$value = $value ? '(True)' : '(False)'; |
|
94
|
|
|
} else { |
|
95
|
|
|
$value = (string) $value; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$value = $hasKey && $options['ucFirst'] ? ucfirst($value) : $value; |
|
99
|
|
|
$text .= static::wrap($value, $options['valStyle'])."\n"; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $text; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Wrap a color style tag. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $text |
|
109
|
|
|
* @param string $tag |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function wrap(string $text, string $tag): string |
|
114
|
|
|
{ |
|
115
|
|
|
if ( ! $text || ! $tag) { |
|
116
|
|
|
return $text; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return "<$tag>$text</$tag>"; |
|
120
|
|
|
} |
|
121
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.