Passed
Push — master ( 17b22a...586c10 )
by Alec
01:20 queued 11s
created

format_time()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 5
dl 0
loc 19
ccs 9
cts 9
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit;
6
7
use function AlecRabbit\Helpers\bounds;
8
use const AlecRabbit\Helpers\Constants\UNITS_LIST;
9
use function AlecRabbit\Helpers\is_negative;
10
use const AlecRabbit\Helpers\Constants\BRACKETS_ANGLE;
11
use const AlecRabbit\Helpers\Constants\BRACKETS_CURLY;
12
use const AlecRabbit\Helpers\Constants\BRACKETS_PARENTHESES;
13
use const AlecRabbit\Helpers\Constants\BRACKETS_SQUARE;
14
use const AlecRabbit\Helpers\Constants\BRACKETS_SUPPORTED;
15
use const AlecRabbit\Helpers\Constants\UNIT_HOURS;
16
use const AlecRabbit\Helpers\Constants\UNIT_MILLISECONDS;
17
use const AlecRabbit\Helpers\Constants\UNIT_MINUTES;
18
use const AlecRabbit\Helpers\Constants\UNIT_SECONDS;
19
use const AlecRabbit\Helpers\Strings\Constants\BYTES_UNITS;
20
use const AlecRabbit\Helpers\Strings\Constants\TIME_COEFFICIENTS;
21
use const AlecRabbit\Helpers\Strings\Constants\TIME_UNITS;
22
23
/**
24
 * @param string $text
25
 * @param string|null $tag
26
 * @return string
27
 */
28
function tag(string $text, string $tag = null): string
29
{
30
    return
31 4
        $tag ? "<{$tag}>$text</{$tag}>" : $text;
32
}
33
34
/**
35
 * @param string $text
36
 * @param int $brackets
37
 * @return string
38
 */
39
function brackets(string $text, int $brackets = BRACKETS_SQUARE): string
40
{
41 7
    if (!\in_array($brackets, BRACKETS_SUPPORTED, true)) {
42 2
        throw new \InvalidArgumentException(
43 2
            'Parameter 2 should be BRACKETS_SQUARE | BRACKETS_CURLY | BRACKETS_PARENTHESES | BRACKETS_ANGLE'
44
        );
45
    }
46
    switch ($brackets) {
47 5
        case BRACKETS_CURLY:
48 1
            $text = "{{$text}}";
49 1
            break;
50 4
        case BRACKETS_SQUARE:
51 2
            $text = "[{$text}]";
52 2
            break;
53 2
        case BRACKETS_PARENTHESES:
54 1
            $text = "({$text})";
55 1
            break;
56 1
        case BRACKETS_ANGLE:
57 1
            $text = "⟨{$text}⟩";
58 1
            break;
59
    }
60 5
    return $text;
61
}
62
63
/**
64
 * @param string $text
65
 * @param null|string $open
66
 * @param null|string $close
67
 * @return string
68
 */
69
function str_wrap(string $text, ?string $open = null, ?string $close = null): string
70
{
71 11
    if (null !== $open && null === $close) {
72 6
        $close = $open;
73
    }
74 11
    return "{$open}{$text}{$close}";
75
}
76
77
/**
78
 * @param int $bytes
79
 * @param null|string $unit
80
 * @param null|int $decimals
81
 * @param string $decimalPoint
82
 * @param string $thousandsSeparator
83
 * @return string
84
 */
85
function format_bytes(
86
    int $bytes,
87
    ?string $unit = null,
88
    ?int $decimals = null,
89
    string $decimalPoint = '.',
90
    string $thousandsSeparator = ''
91
): string {
92 40
    $decimals = $decimals ?? 2;
93 40
    $negative = is_negative($bytes);
94 40
    $bytes = (int)\abs($bytes);
95 40
    $value = 0;
96 40
    $unit = \strtoupper($unit ?? '');
97 40
    if ($bytes > 0) {
98
        // Generate automatic prefix by bytes
99
        // If wrong prefix given
100 39
        if (!\array_key_exists($unit, BYTES_UNITS)) {
101 11
            $pow = (int)\floor(\log($bytes) / \log(1024));
102 11
            $unit = (string)\array_search($pow, BYTES_UNITS, true);
103
        }
104
        // Calculate byte value by prefix
105 39
        $value = ($bytes / 1024 ** \floor(BYTES_UNITS[$unit]));
106
    } else {
107 1
        $unit = 'B';
108
    }
109 40
    if ($unit === 'B') {
110 7
        $decimals = 0;
111
    }
112 40
    $decimals = (int)bounds($decimals, 0, 24);
113
114
    // Format output
115
    return
116 40
        ($negative ? '-' : '') . number_format($value, $decimals, $decimalPoint, $thousandsSeparator) . $unit;
117
}
118
119
/**
120
 * @param null|float $value
121
 * @param null|int $units
122
 * @param null|int $decimals
123
 * @param string $decimalPoint
124
 * @param string $thousandsSeparator
125
 * @return string
126
 */
127
function format_time(
128
    ?float $value,
129
    ?int $units = null,
130
    ?int $decimals = null,
131
    string $decimalPoint = '.',
132
    string $thousandsSeparator = ''
133
): string {
134 19
    $units = $units ?? UNIT_MILLISECONDS;
135 19
    $decimals = (int)bounds($decimals ?? 1, 0, 6);
136 19
    $value = $value ?? 0.0;
137
138
    return
139 19
        number_format(
140 19
            round($value * TIME_COEFFICIENTS[$units], $decimals),
141 19
            $decimals,
142 19
            $decimalPoint,
143 19
            $thousandsSeparator
144
        ) .
145 19
        TIME_UNITS[$units];
146
}
147
148
/**
149
 * @param float $value
150
 * @param string $decimalPoint
151
 * @param string $thousandsSeparator
152
 * @return string
153
 */
154
function format_time_auto(
155
    float $value,
156
    string $decimalPoint = '.',
157
    string $thousandsSeparator = ''
158
): string {
159 19
    if ($value > 10000) {
160 1
        return format_time($value, UNIT_HOURS, 3, $decimalPoint, $thousandsSeparator);
161
    }
162 18
    if ($value > 1000) {
163 1
        return format_time($value, UNIT_MINUTES, 2, $decimalPoint, $thousandsSeparator);
164
    }
165 17
    if ($value > 100) {
166 1
        return format_time($value, UNIT_SECONDS, 0, $decimalPoint, $thousandsSeparator);
167
    }
168
    $pow =
169 16
        (int)bounds(
170 16
            \abs(
171 16
                \floor(
172 16
                    \log($value) / \log(1000)
173
                )
174
            ),
175 16
            0,
176 16
            3
177
        );
178
    return
179 16
        \number_format(
180 16
            \round($value * 1000 ** $pow, 1),
181 16
            1,
182 16
            $decimalPoint,
183 16
            $thousandsSeparator
184
        ) .
185 16
        TIME_UNITS[UNITS_LIST[$pow]];
186
}
187
188
/**
189
 * @param int $value
190
 * @param string $decimalPoint
191
 * @param string $thousandsSeparator
192
 * @return string
193
 */
194
function format_time_ns(
195
    int $value,
196
    string $decimalPoint = '.',
197
    string $thousandsSeparator = ''
198
): string {
199 8
    return format_time_auto($value / 1000000000, $decimalPoint, $thousandsSeparator);
200
}
201