Completed
Pull Request — master (#4)
by James Ekow Abaka
03:06 queued 36s
created

FilesizeHelper::commas()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ntentan\honam\engines\php\helpers;
4
5
use ntentan\honam\engines\php\Helper;
6
7
class FilesizeHelper extends Helper
8
{
9
    private $size;
10
    private $commas;
11
    private $decimals;
12
    
13 1
    public function help($size, $decimals = 2, $commas = true)
14
    {
15 1
        $this->size = $size;
16 1
        $this->decimals($decimals);
17 1
        $this->commas($commas);
18
        
19 1
        return $this;
20
    }
21
    
22 1
    public function commas($commas)
23
    {
24 1
        $this->commas = $commas ? ',' : '';
25 1
        return $this;
26
    }
27
    
28 1
    public function decimals($decimals)
29
    {
30 1
        $this->decimals = $decimals;
31 1
        return $this;
32
    }
33
    
34 1
    public function __toString() 
35
    {
36 1
        $output = null;
37
        $scales = array(
38 1
            array(0, 1024, 'Byte', 'Byte', false),
39
            array(1024, 1048576, 'Kilobyte', 'KB', true),
40
            array(1048576, 1073741824, 'Megabyte', 'MB', true),
41
            array(1073741824, 1099511627776, 'Gigabyte', 'GB', true),
42
            array(1099511627776 ,1125899906842620, 'Terabyte', 'TB', true),
43
            array(1125899906842620, 1152921504606850000, 'Petabyte', 'PB', true)
44
        );
45
        
46 1
        $devisor = 1;
47 1
        foreach($scales as $scale)
48
        {
49 1
            if($this->size >= $scale[0] && $this->size < $scale[1])
50
            {
51 1
                $size = $this->size / $devisor;
52 1
                $output = number_format($size, $scale[4] ? $this->decimals : 0, '.', $this->commas)
53 1
                    . " {$scale[2]}" 
54 1
                    . ($size <> 1 ? 's' : '');
55 1
                break;
56
            }
57 1
            $devisor = $scale[1];
58
        }        
59
        
60 1
        return $output;
61
    }
62
}
63