|
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\Helper; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Helper is the base class for all helper classes. |
|
27
|
|
|
*/ |
|
28
|
|
|
class Helper |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* The width is how many characters positions the string will use. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string|null $value |
|
34
|
|
|
* |
|
35
|
|
|
* @return int |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function width(?string $value): int |
|
38
|
|
|
{ |
|
39
|
|
|
$value ?? ''; |
|
40
|
|
|
|
|
41
|
|
|
if (false === $encoding = mb_detect_encoding($value, null, true)) { |
|
|
|
|
|
|
42
|
|
|
return strlen($value); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return mb_strwidth($value, $encoding); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The length is related to how many bytes the string will use. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string|null $value |
|
52
|
|
|
* |
|
53
|
|
|
* @return int |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function length(?string $value): int |
|
56
|
|
|
{ |
|
57
|
|
|
$value ?? ''; |
|
58
|
|
|
|
|
59
|
|
|
if (false === $encoding = mb_detect_encoding($value, null, true)) { |
|
|
|
|
|
|
60
|
|
|
return strlen($value); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return mb_strlen($value, $encoding); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns the subset of a string, using mb_substr if it is available. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string|null $value |
|
70
|
|
|
* @param int $from |
|
71
|
|
|
* @param int|null $length |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function substr(?string $value, int $from, int $length = null): string |
|
76
|
|
|
{ |
|
77
|
|
|
$value ?? ''; |
|
78
|
|
|
|
|
79
|
|
|
if (false === $encoding = mb_detect_encoding($value, null, true)) { |
|
|
|
|
|
|
80
|
|
|
return substr($value, $from, $length); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return mb_substr($value, $from, $length, $encoding); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the format for time of an item result to response time in console. |
|
88
|
|
|
* |
|
89
|
|
|
* @param int|float $seconds |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function formatTime(int|float $secs) |
|
94
|
|
|
{ |
|
95
|
|
|
static $timeFormats = [ |
|
96
|
|
|
[0, '< 1 sec'], |
|
97
|
|
|
[1, '1 sec'], |
|
98
|
|
|
[2, 'secs', 1], |
|
99
|
|
|
[60, '1 min'], |
|
100
|
|
|
[120, 'mins', 60], |
|
101
|
|
|
[3600, '1 hr'], |
|
102
|
|
|
[7200, 'hrs', 3600], |
|
103
|
|
|
[86400, '1 day'], |
|
104
|
|
|
[172800, 'days', 86400], |
|
105
|
|
|
]; |
|
106
|
|
|
|
|
107
|
|
|
foreach ($timeFormats as $index => $format) { |
|
108
|
|
|
if ($secs >= $format[0]) { |
|
109
|
|
|
if ((isset($timeFormats[$index + 1]) && |
|
110
|
|
|
$secs < $timeFormats[$index + 1][0]) || |
|
111
|
|
|
$index == \count($timeFormats) - 1 |
|
112
|
|
|
) { |
|
113
|
|
|
if (2 == \count($format)) { |
|
114
|
|
|
return $format[1]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return floor($secs / $format[2]).' '.$format[1]; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |