Completed
Pull Request — master (#38)
by Boris
04:23 queued 02:11
created

Formatter::bytes()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
rs 9.4285
nc 1
cc 2
eloc 7
nop 2
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
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 CacheTool\Util;
13
14
class Formatter
15
{
16
    /**
17
     * @param  integer  $bytes
18
     * @param  integer $precision
19
     * @return string
20
     */
21
    public static function bytes($bytes, $precision = 2)
22
    {
23
        $units = array('b', 'KiB', 'MiB', 'GiB', 'TiB');
24
25
        $bytes = max($bytes, 0);
26
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
27
        $pow = min($pow, count($units) - 1);
28
        $bytes /= (1 << (10 * $pow));
29
30
        return round($bytes, $precision) . ' ' . $units[$pow];
31
    }
32
33
    /**
34
     * @param  mixed  $date
35
     * @param  string $format
36
     * @return string
37
     */
38
    public static function date($date, $format = null)
39
    {
40
        if (false === $date instanceof \DateTime) {
41
            if ($format !== null) {
42
                $date = \DateTime::createFromFormat($format, $date);
43
            } else {
44
                $date = new \DateTime($date);
45
            }
46
        }
47
48
        return $date->format(\DateTime::RFC2822);
49
    }
50
}
51