Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Filter | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
| formatHumanReadableTime | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| formatHumanReadableSize | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php declare(strict_types=1); |
| 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 | */ |
| 11 | |
| 12 | namespace PrivateBin; |
| 13 | |
| 14 | use Exception; |
| 15 | |
| 16 | /** |
| 17 | * Filter |
| 18 | * |
| 19 | * Provides data filtering functions. |
| 20 | */ |
| 21 | class Filter |
| 22 | { |
| 23 | /** |
| 24 | * format a given time string into a human readable label (localized) |
| 25 | * |
| 26 | * accepts times in the format "[integer][time unit]" |
| 27 | * |
| 28 | * @access public |
| 29 | * @static |
| 30 | * @param string $time |
| 31 | * @throws Exception |
| 32 | * @return string |
| 33 | */ |
| 34 | public static function formatHumanReadableTime($time) |
| 35 | { |
| 36 | if (preg_match('/^(\d+) *(\w+)$/', $time, $matches) !== 1) { |
| 37 | throw new Exception("Error parsing time format '$time'", 30); |
| 38 | } |
| 39 | switch ($matches[2]) { |
| 40 | case 'sec': |
| 41 | $unit = 'second'; |
| 42 | break; |
| 43 | case 'min': |
| 44 | $unit = 'minute'; |
| 45 | break; |
| 46 | default: |
| 47 | $unit = rtrim($matches[2], 's'); |
| 48 | } |
| 49 | return I18n::_(array('%d ' . $unit, '%d ' . $unit . 's'), (int) $matches[1]); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * format a given number of bytes in IEC 80000-13:2008 notation (localized) |
| 54 | * |
| 55 | * @access public |
| 56 | * @static |
| 57 | * @param int $size |
| 58 | * @return string |
| 59 | */ |
| 60 | public static function formatHumanReadableSize($size) |
| 61 | { |
| 62 | $iec = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); |
| 63 | $i = 0; |
| 64 | while (($size / 1000) >= 1) { |
| 65 | $size = $size / 1000; |
| 66 | ++$i; |
| 67 | } |
| 68 | return number_format($size, $i ? 2 : 0, '.', ' ') . ' ' . I18n::_($iec[$i]); |
| 69 | } |
| 70 | } |