Completed
Push — master ( 1c956a...362a13 )
by WEBEWEB
01:17
created

FileSizeRenderer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 49
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A renderSize() 0 18 5
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Renderer;
13
14
/**
15
 * File size renderer.
16
 *
17
 * @author webeweb <https://github.com/webeweb>
18
 * @package WBW\Library\Core\Renderer
19
 */
20
class FileSizeRenderer {
21
22
    /**
23
     * Size divider "Gio".
24
     *
25
     * @var int
26
     */
27
    const SIZE_DIVIDER_GIO = 1073741824;
28
29
    /**
30
     * Size divider "Kio".
31
     *
32
     * @var int
33
     */
34
    const SIZE_DIVIDER_KIO = 1024;
35
36
    /**
37
     * Size divider "Mio".
38
     *
39
     * @var int
40
     */
41
    const SIZE_DIVIDER_MIO = 1048576;
42
43
    /**
44
     * Render a size.
45
     *
46
     * @param int $size The size.
47
     * @param int $decimals The decimals.
48
     * @return string Returns the rendered size.
49
     */
50
    public static function renderSize($size, $decimals = 2) {
51
52
        if (null === $size || $size < 0) {
53
            return "";
54
        }
55
56
        $format = "%.${decimals}f";
57
58
        if (self::SIZE_DIVIDER_GIO <= $size) {
59
            return sprintf("${format} Gio", $size / self::SIZE_DIVIDER_GIO);
60
        }
61
62
        if (self::SIZE_DIVIDER_MIO <= $size) {
63
            return sprintf("${format} Mio", $size / self::SIZE_DIVIDER_MIO);
64
        }
65
66
        return sprintf("${format} Kio", $size / self::SIZE_DIVIDER_KIO);
67
    }
68
}
69