Passed
Push — master ( 2b9668...6875d9 )
by Sebastian
02:20
created

ConvertHelper_ByteConverter::toTerabytes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * File containing the {@see ConvertHelper_ByteConverter} class.
4
 *
5
 * @package AppUtils
6
 * @subpackage ConvertHelper
7
 * @see ConvertHelper_ByteConverter
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * This class is made to convert an amount of bytes into a storage 
16
 * size notation like "50 MB".
17
 *
18
 * It supports both Base 10 and Base 2 size calculation.
19
 *
20
 * @package AppUtils
21
 * @subpackage ConvertHelper
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class ConvertHelper_ByteConverter
25
{
26
   /**
27
    * @var int
28
    */
29
    protected $bytes;
30
    
31
    public function __construct(int $bytes)
32
    {
33
        $this->bytes = $bytes;
34
        
35
        // correct negative values
36
        if($this->bytes < 0) 
37
        {
38
            $this->bytes = 0;
39
        }
40
    }
41
    
42
   /**
43
    * Detects the size matching the byte value for the specified base.
44
    * 
45
    * @param int $base
46
    * @return ConvertHelper_StorageSizeEnum_Size
47
    */
48
    protected function detectSize(int $base) : ConvertHelper_StorageSizeEnum_Size
49
    {
50
        $sizes = $this->getSizesSorted($base);   
51
52
        if($this->bytes >= $sizes[0]->getBytes()) 
53
        {
54
            $total = count($sizes);
55
            
56
            for($i=0; $i < $total; $i++)
57
            {
58
                $size = $sizes[$i];
59
                
60
                if(!isset($sizes[($i+1)])) {
61
                    return $size;
62
                }
63
                
64
                if($this->bytes >= $size->getBytes() && $this->bytes < $sizes[($i+1)]->getBytes()) {
65
                    return $size;
66
                }
67
            }
68
        }
69
        
70
        return ConvertHelper_StorageSizeEnum::getSizeByName('b');
71
    }
72
    
73
   /**
74
    * Retrieves all storage sizes for the specified base, 
75
    * sorted from smallest byte size to highest.
76
    * 
77
    * @param int $base
78
    * @return \AppUtils\ConvertHelper_StorageSizeEnum_Size[]
79
    */
80
    protected function getSizesSorted(int $base)
81
    {
82
        $sizes = ConvertHelper_StorageSizeEnum::getSizesByBase($base);
83
        
84
        usort($sizes, function(ConvertHelper_StorageSizeEnum_Size $a, ConvertHelper_StorageSizeEnum_Size $b)
85
        {
86
            return $a->getBytes() - $b->getBytes();
87
        });
88
        
89
        return $sizes;
90
    }
91
    
92
   /**
93
    * Converts the byte value to a human readable string, e.g. "5 KB", "140 MB".
94
    * 
95
    * @param int $precision The amount of decimals (rounded up)
96
    * @param int $base The base to calculate bytes with.
97
    * @return string
98
    * 
99
    * @see ConvertHelper_StorageSizeEnum::BASE_10
100
    * @see ConvertHelper_StorageSizeEnum::BASE_2
101
    */
102
    public function toString(int $precision, int $base=ConvertHelper_StorageSizeEnum::BASE_10) : string
103
    {
104
        $size = $this->detectSize($base);
105
        
106
        return round($this->bytes / $size->getBytes(), $precision) . ' ' . $size->getSuffix();
107
    }
108
    
109
   /**
110
    * Converts the byte value to the amount of the corresponding units (KB, MB...).
111
    * 
112
    * @param int $precision The amount of decimals (rounded up)
113
    * @param string $sizeName The lowercase storage size name (e.g. "kb", "kib")
114
    * @return float
115
    */
116
    public function toNumber(int $precision, string $sizeName) : float
117
    {
118
        $size = ConvertHelper_StorageSizeEnum::getSizeByName($sizeName);
119
        
120
        return round($this->bytes / $size->getBytes(), $precision);
121
    }
122
    
123
   /**
124
    * Converts the bytes to Kilobytes.
125
    * 
126
    * @param int $precision Amount of decimals (rounded up)
127
    * @return float
128
    */
129
    public function toKilobytes(int $precision=1) : float
130
    {
131
        return $this->toNumber($precision, 'kb');
132
    }
133
    
134
   /**
135
    * Converts the bytes to Megabytes.
136
    *
137
    * @param int $precision Amount of decimals (rounded up)
138
    * @return float
139
    */
140
    public function toMegabytes(int $precision=1) : float
141
    {
142
        return $this->toNumber($precision, 'mb');
143
    }
144
145
   /**
146
    * Converts the bytes to Gigabytes.
147
    *
148
    * @param int $precision Amount of decimals (rounded up)
149
    * @return float
150
    */
151
    public function toGigabytes(int $precision=1) : float
152
    {
153
        return $this->toNumber($precision, 'gb');
154
    }
155
156
   /**
157
    * Converts the bytes to Terabytes.
158
    *
159
    * @param int $precision Amount of decimals (rounded up)
160
    * @return float
161
    */
162
    public function toTerabytes(int $precision=1) : float
163
    {
164
        return $this->toNumber($precision, 'tb');
165
    }
166
    
167
   /**
168
    * Converts the bytes to Petabytes.
169
    *
170
    * @param int $precision Amount of decimals (rounded up)
171
    * @return float
172
    */
173
    public function toPetabytes(int $precision=1) : float
174
    {
175
        return $this->toNumber($precision, 'pb');
176
    }
177
178
    /**
179
     * Converts the bytes to Kibibytes (Base 2).
180
     *
181
     * @param int $precision Amount of decimals (rounded up)
182
     * @return float
183
     */
184
    public function toKibibytes(int $precision=1) : float
185
    {
186
        return $this->toNumber($precision, 'kib');
187
    }
188
    
189
    /**
190
     * Converts the bytes to Mebibytes (Base 2).
191
     *
192
     * @param int $precision Amount of decimals (rounded up)
193
     * @return float
194
     */
195
    public function toMebibytes(int $precision=1) : float
196
    {
197
        return $this->toNumber($precision, 'mib');
198
    }
199
    
200
    /**
201
     * Converts the bytes to Gibibytes (Base 2).
202
     *
203
     * @param int $precision Amount of decimals (rounded up)
204
     * @return float
205
     */
206
    public function toGibibytes(int $precision=1) : float
207
    {
208
        return $this->toNumber($precision, 'gib');
209
    }
210
    
211
    /**
212
     * Converts the bytes to Tebibytes (Base 2).
213
     *
214
     * @param int $precision Amount of decimals (rounded up)
215
     * @return float
216
     */
217
    public function toTebibytes(int $precision=1) : float
218
    {
219
        return $this->toNumber($precision, 'tib');
220
    }
221
    
222
    /**
223
     * Converts the bytes to Pebibytes (Base 2).
224
     *
225
     * @param int $precision Amount of decimals (rounded up)
226
     * @return float
227
     */
228
    public function toPebibytes(int $precision=1) : float
229
    {
230
        return $this->toNumber($precision, 'pib');
231
    }
232
}
233