Formatter::date()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
c 0
b 0
f 0
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 3
    public static function bytes($bytes, $precision = 2)
22
    {
23 3
        $units = ['b', 'KiB', 'MiB', 'GiB', 'TiB'];
24
25 3
        $bytes = max($bytes, 0);
26 3
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
27 3
        $pow = min($pow, count($units) - 1);
28 3
        $bytes /= (1 << (10 * $pow));
29
30 3
        return round($bytes, $precision) . ' ' . $units[$pow];
31
    }
32
33
    /**
34
     * @param  mixed  $date
35
     * @param  string $format
36
     * @return string
37
     */
38 2
    public static function date($date, $format = null)
39
    {
40 2
        if (false === $date instanceof \DateTime) {
41 2
            if ($format !== null) {
42 2
                $date = \DateTime::createFromFormat($format, $date);
43
            } else {
44
                $date = new \DateTime($date);
45
            }
46
        }
47
48 2
        return $date->format(\DateTime::RFC2822);
49
    }
50
}
51