Completed
Push — master ( f9acd9...e71144 )
by Alec
06:12
created

format_time_auto()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.1967

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 0
loc 22
ccs 10
cts 13
cp 0.7692
crap 4.1967
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: alec
4
 * Date: 12.10.18
5
 * Time: 15:24
6
 */
7
8
namespace AlecRabbit;
9
10
use function AlecRabbit\Helpers\bounds;
11
use function AlecRabbit\Helpers\is_negative;
12
use const AlecRabbit\Constants\BRACKETS_ANGLE;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\BRACKETS_ANGLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
13
use const AlecRabbit\Constants\BRACKETS_CURLY;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\BRACKETS_CURLY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
14
use const AlecRabbit\Constants\BRACKETS_PARENTHESES;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\BRACKETS_PARENTHESES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
use const AlecRabbit\Constants\BRACKETS_SQUARE;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\BRACKETS_SQUARE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
16
use const AlecRabbit\Constants\BRACKETS_SUPPORTED;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\BRACKETS_SUPPORTED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
use const AlecRabbit\Constants\DEFAULT_PRECISION;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\DEFAULT_PRECISION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
18
use const AlecRabbit\Constants\String\BYTES_UNITS;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\String\BYTES_UNITS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
use const AlecRabbit\Constants\String\TIME_COEFFICIENTS;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\String\TIME_COEFFICIENTS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
20
use const AlecRabbit\Constants\String\TIME_UNITS;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\String\TIME_UNITS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
use const AlecRabbit\Constants\UNIT_HOURS;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\UNIT_HOURS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
22
use const AlecRabbit\Constants\UNIT_MILLISECONDS;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\UNIT_MILLISECONDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
23
use const AlecRabbit\Constants\UNIT_MINUTES;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\UNIT_MINUTES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
24
use const AlecRabbit\Constants\UNIT_SECONDS;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\UNIT_SECONDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
25
use const AlecRabbit\Constants\UNITS;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\UNITS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
26
27
/**
28
 * @param string $text
29
 * @param string|null $tag
30
 * @return string
31
 */
32
function tag(string $text, string $tag = null): string
33
{
34
    return
35 1
        $tag ? "<{$tag}>$text</{$tag}>" : $text;
36
}
37
38
/**
39
 * @param string $text
40
 * @param int $brackets
41
 * @return string
42
 */
43
function brackets(string $text, int $brackets = BRACKETS_SQUARE): string
44
{
45 3
    if (!\in_array($brackets, BRACKETS_SUPPORTED, true)) {
46 1
        throw new \InvalidArgumentException(
47 1
            'Parameter 2 should be BRACKETS_SQUARE | BRACKETS_CURLY | BRACKETS_PARENTHESES | BRACKETS_ANGLE'
48
        );
49
    }
50
    switch ($brackets) {
51 2
        case BRACKETS_CURLY:
52 1
            $text = "{{$text}}";
53 1
            break;
54 2
        case BRACKETS_SQUARE:
55 2
            $text = "[{$text}]";
56 2
            break;
57 1
        case BRACKETS_PARENTHESES:
58 1
            $text = "({$text})";
59 1
            break;
60 1
        case BRACKETS_ANGLE:
61 1
            $text = "⟨{$text}⟩";
62 1
            break;
63
    }
64 2
    return $text;
65
}
66
67
/**
68
 * @param string $text
69
 * @param null|string $open
70
 * @param null|string $close
71
 * @return string
72
 */
73
function str_decorate(string $text, ?string $open = null, ?string $close = null): string
74
{
75 1
    if (null !== $open && null === $close) {
76 1
        $close = $open;
77
    }
78 1
    return "{$open}{$text}{$close}";
79
}
80
81
82
function format_bytes(int $bytes, ?string $unit = null, int $decimals = 2): string
83
{
84 37
    $negative = is_negative($bytes);
85
    /** @noinspection CallableParameterUseCaseInTypeContextInspection */
86 37
    $bytes = \abs($bytes);
87 37
    $value = 0;
88 37
    $unit = \strtoupper($unit ?? '');
89 37
    if ($bytes > 0) {
90
        // Generate automatic prefix by bytes
91
        // If wrong prefix given
92 36
        if (!\array_key_exists($unit, BYTES_UNITS)) {
93 12
            $pow = (int)\floor(\log($bytes) / \log(1024));
94 12
            $unit = (string)\array_search($pow, BYTES_UNITS, true);
95
        }
96
        // Calculate byte value by prefix
97 36
        $value = ($bytes / 1024 ** \floor(BYTES_UNITS[$unit]));
98
    } else {
99 1
        $unit = 'B';
100
    }
101 37
    if ($unit === 'B') {
102 6
        $decimals = 0;
103
    }
104 37
    $decimals = (int)bounds($decimals, 0, 24);
105
106
    // Format output
107
    return
108 37
        sprintf('%s%.' . $decimals . 'f' . $unit, $negative ? '-' : '', $value);
109
}
110
111
function format_time(?float $value, ?int $units = null, int $precision = DEFAULT_PRECISION): string
112
{
113 12
    $units = $units ?? UNIT_MILLISECONDS;
114 12
    $precision = (int)bounds($precision, 0, 6);
115 12
    $value = $value ?? 0.0;
116
    return
117 12
        sprintf(
118 12
            '%s%s',
119 12
            round($value * TIME_COEFFICIENTS[$units], $precision),
120 12
            TIME_UNITS[$units]
121
        );
122
}
123
124
function format_time_auto(float $value): string
125
{
126 8
    if ($value > 10000) {
127
        return format_time($value, UNIT_HOURS, 3);
128
    }
129 8
    if ($value > 1000) {
130
        return format_time($value, UNIT_MINUTES, 2);
131
    }
132 8
    if ($value > 100) {
133
        return format_time($value, UNIT_SECONDS, 0);
134
    }
135
    $pow =
136 8
        (int)bounds(
137 8
            \abs(
138 8
                \floor(
139 8
                    \log($value) / \log(1000)
140
                )
141
            ),
142 8
            0,
143 8
            3
144
        );
145 8
    return \round($value * 1000 ** $pow, 1) . TIME_UNITS[UNITS[$pow]];
146
}
147