Completed
Branch V3 (ec4077)
by PastisD
11:29 queued 09:28
created

Extension::size_format()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 4
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Georges.L (Geolim4)  <[email protected]>
11
 * @author PastisD https://github.com/PastisD
12
 * @author Alexander (asm89) <[email protected]>
13
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
14
 *
15
 */
16
17
namespace Phpfastcache\Bundle\Twig\HumanReadableExtension;
18
19
/**
20
 * Class HumanReadableExtension
21
 * @package Phpfastcache\Bundle\Twig
22
 */
23
class Extension extends \Twig_Extension
24
{
25
    /**
26
     * @return array
27
     */
28
    public function getFilters()
29
    {
30
        return array(
31
            new \Twig_SimpleFilter('sizeFormat', [$this, 'size_format']),
32
        );
33
    }
34
    /**
35
     * @param int $bytes Bytes/Octets
36
     * @param int $decimals Number for decimals to return
37
     * @param bool $octetFormat Use Octet notation instead of Bytes
38
     * @param string $separator The unit separator
39
     *
40
     * @return string
41
     */
42
    public function size_format($bytes, $decimals = 2, $octetFormat = false, $separator = '')
43
    {
44
        $bytes = (int) $bytes;
45
        $sz     = 'BKMGTP';
46
        $factor = floor(( strlen($bytes) - 1 ) / 3);
47
48
        return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)).$separator.@$sz[ $factor ].( $factor ? ($octetFormat ? 'O' : 'B') : '' );
49
    }
50
51
    /**
52
     * Extension name
53
     *
54
     * @return string
55
     */
56
    public function getName()
57
    {
58
        return 'human_readable_extension';
59
    }
60
}