Completed
Push — master ( 0e7286...1af01d )
by WEBEWEB
01:30
created

FileSizeRenderer::renderSize()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.3554
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the core-bundle 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\Bundle\CoreBundle\Renderer;
13
14
/**
15
 * File size renderer.
16
 *
17
 * @author webeweb <https://github.com/webeweb/>
18
 * @package WBW\Bundle\CoreBundle\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
     * @return string Returns the rendered size.
48
     */
49
    public static function renderSize($size) {
50
51
        if (null === $size || $size < 0) {
52
            return "";
53
        }
54
55
        $format = "%.2f";
56
57
        if (self::SIZE_DIVIDER_GIO <= $size) {
58
            return sprintf("${format} Gio", $size / self::SIZE_DIVIDER_GIO);
59
        }
60
61
        if (self::SIZE_DIVIDER_MIO <= $size) {
62
            return sprintf("${format} Mio", $size / self::SIZE_DIVIDER_MIO);
63
        }
64
65
        return sprintf("${format} Kio", $size / self::SIZE_DIVIDER_KIO);
66
    }
67
}
68