Passed
Push — develop ( 0e0fcc...00fcbf )
by Alec
03:46 queued 01:09
created

str_wrap()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

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