Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Filter
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 formatHumanReadableTime
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 formatHumanReadableSize
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * PrivateBin
4 *
5 * a zero-knowledge paste bin
6 *
7 * @link      https://github.com/PrivateBin/PrivateBin
8 * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
9 * @license   https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
10 * @version   1.7.0
11 */
12
13namespace PrivateBin;
14
15use Exception;
16
17/**
18 * Filter
19 *
20 * Provides data filtering functions.
21 */
22class Filter
23{
24    /**
25     * format a given time string into a human readable label (localized)
26     *
27     * accepts times in the format "[integer][time unit]"
28     *
29     * @access public
30     * @static
31     * @param  string $time
32     * @throws Exception
33     * @return string
34     */
35    public static function formatHumanReadableTime($time)
36    {
37        if (preg_match('/^(\d+) *(\w+)$/', $time, $matches) !== 1) {
38            throw new Exception("Error parsing time format '$time'", 30);
39        }
40        switch ($matches[2]) {
41            case 'sec':
42                $unit = 'second';
43                break;
44            case 'min':
45                $unit = 'minute';
46                break;
47            default:
48                $unit = rtrim($matches[2], 's');
49        }
50        return I18n::_(array('%d ' . $unit, '%d ' . $unit . 's'), (int) $matches[1]);
51    }
52
53    /**
54     * format a given number of bytes in IEC 80000-13:2008 notation (localized)
55     *
56     * @access public
57     * @static
58     * @param  int $size
59     * @return string
60     */
61    public static function formatHumanReadableSize($size)
62    {
63        $iec = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
64        $i   = 0;
65        while (($size / 1024) >= 1) {
66            $size = $size / 1024;
67            ++$i;
68        }
69        return number_format($size, $i ? 2 : 0, '.', ' ') . ' ' . I18n::_($iec[$i]);
70    }
71}