Passed
Push — master ( 42885b...6beff1 )
by Alec
04:27
created

brackets()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 17
c 1
b 0
f 1
nc 6
nop 2
dl 0
loc 22
ccs 16
cts 16
cp 1
crap 6
rs 9.0777
1
<?php
2
/**
3
 * User: alec
4
 * Date: 12.10.18
5
 * Time: 15:24
6
 */
7
8
define('BRACKETS_SQUARE', 10); // []
9
define('BRACKETS_CURLY', 20); // {}
10
define('BRACKETS_PARENTHESES', 30); // ()
11
define('BRACKETS_ANGLE', 40); //  ⟨⟩
12
13
define('BRACKETS_SUPPORTED',
14
    [
15
        BRACKETS_SQUARE,
16
        BRACKETS_CURLY,
17
        BRACKETS_PARENTHESES,
18
        BRACKETS_ANGLE,
19
    ]
20
);
21
22
23
if (!function_exists('tag')) {
24
    /**
25
     * @param string $text
26
     * @param string|null $tag
27
     * @return string
28
     */
29
    function tag(string $text, string $tag = null): string
30
    {
31
        return
32 1
            $tag ? "<{$tag}>$text</{$tag}>" : $text;
33
    }
34
}
35
36
if (!function_exists('brackets')) {
37
    /**
38
     * @param string $text
39
     * @param int|null $brackets
40
     * @return string
41
     */
42
    function brackets(string $text, int $brackets = BRACKETS_SQUARE): string
43
    {
44 3
        if (!\in_array($brackets, BRACKETS_SUPPORTED, true)) {
45 1
            throw new \InvalidArgumentException(
46 1
                'Parameter 2 should be BRACKETS_SQUARE | BRACKETS_CURLY | BRACKETS_PARENTHESES | BRACKETS_ANGLE'
47
            );
48
        }
49
        switch ($brackets) {
50 2
            case BRACKETS_CURLY:
51 1
                $text = "{{$text}}";
52 1
                break;
53 2
            case BRACKETS_SQUARE:
54 2
                $text = "[{$text}]";
55 2
                break;
56 1
            case BRACKETS_PARENTHESES:
57 1
                $text = "({$text})";
58 1
                break;
59 1
            case BRACKETS_ANGLE:
60 1
                $text = "⟨{$text}⟩";
61 1
                break;
62
        }
63 2
        return $text;
64
    }
65
}
66
67
if (!function_exists('str_decorate')) {
68
    /**
69
     * @param string $text
70
     * @param null|string $open
71
     * @param null|string $close
72
     * @return string
73
     */
74
    function str_decorate(string $text, ?string $open = null, ?string $close = null): string
75
    {
76 1
        if (null !== $open && null === $close) {
77 1
            $close = $open;
78
        }
79 1
        return "{$open}{$text}{$close}";
80
    }
81
}
82
83
if (!function_exists('format_bytes')) {
84
    define(
85
        'BYTES_UNITS',
86
        [
87
            'B' => 0,
88
            'KB' => 1,
89
            'MB' => 2,
90
            'GB' => 3,
91
            'TB' => 4,
92
            'PB' => 5,
93
            'EB' => 6,
94
            'ZB' => 7,
95
            'YB' => 8
96
        ]);
97
98
    function format_bytes(int $bytes, ?string $unit = null, int $decimals = 2): string
99
    {
100 37
        $negative = is_negative($bytes);
101 37
        $bytes = (int)\abs($bytes);
102 37
        $value = 0;
103 37
        $unit = \strtoupper($unit ?? '');
104 37
        if ($bytes > 0) {
105
            // Generate automatic prefix by bytes
106
            // If wrong prefix given
107 36
            if (!\array_key_exists($unit, BYTES_UNITS)) {
108 12
                $pow = (int)\floor(\log($bytes) / \log(1024));
109 12
                $unit = (string)\array_search($pow, BYTES_UNITS, true);
110
            }
111
            // Calculate byte value by prefix
112 36
            $value = ($bytes / 1024 ** \floor(BYTES_UNITS[$unit]));
113
        } else {
114 1
            $unit = 'B';
115
        }
116 37
        if ($unit === 'B') {
117 6
            $decimals = 0;
118
        }
119 37
        $decimals = (int)bounds($decimals, 0, 24);
120
121
        // Format output
122
        return
123 37
            sprintf('%s%.' . $decimals . 'f' . $unit, $negative ? '-' : '', $value);
124
    }
125
}
126