Completed
Push — master ( 7e7da0...18ecc7 )
by Georges
01:55
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 [
31
          new \Twig_SimpleFilter('sizeFormat', [$this, 'size_format']),
32
        ];
33
    }
34
35
    /**
36
     * @param int $bytes Bytes/Octets
37
     * @param int $decimals Number for decimals to return
38
     * @param bool $octetFormat Use Octet notation instead of Bytes
39
     * @param string $separator The unit separator
40
     *
41
     * @return string
42
     */
43
    public function size_format($bytes, $decimals = 2, $octetFormat = false, $separator = '')
44
    {
45
        $bytes = (int)$bytes;
46
        $sz = 'BKMGTP';
47
        $factor = floor((\strlen($bytes) - 1) / 3);
48
49
        return sprintf("%.{$decimals}f", $bytes / (1024 ** $factor)) . $separator . @$sz[ $factor ] . ($factor ? ($octetFormat ? 'O' : 'B') : '');
50
    }
51
52
    /**
53
     * Extension name
54
     *
55
     * @return string
56
     */
57
    public function getName()
58
    {
59
        return 'human_readable_extension';
60
    }
61
}