Completed
Push — master ( 6b7717...ad9af1 )
by Alexandre
11s
created

formatting-functions.php ➔ format_duration()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 6
nop 1
dl 0
loc 24
ccs 0
cts 20
cp 0
crap 42
rs 8.5125
c 0
b 0
f 0
1
<?php
2
/**
3
* @author Todd Burry <[email protected]>
4
* @copyright 2009-2014 Vanilla Forums Inc.
5
* @license MIT
6
*/
7
8
/**
9
 * Format a callback function as a string.
10
 *
11
 * @param callable $callback The callback to format.
12
 * @return string Returns a string representation of the callback.
13
 * @return string Returns the callback as a string.
14
 */
15
function format_callback(callable $callback) {
16 1
    if (is_string($callback)) {
17 1
        return $callback.'()';
18 1
    } elseif (is_array($callback)) {
19 1
        if (is_object($callback[0])) {
20 1
            return get_class($callback[0]).'->'.$callback[1].'()';
21
        } else {
22 1
            return $callback[0].'::'.$callback[1].'()';
23
        }
24
    } elseif ($callback instanceof Closure) {
25
        return 'closure()';
26
    }
27
    return '';
28
}
29
30
/**
31
 * Format a span of time that comes from a timer.
32
 *
33
 * @param float $seconds The number of seconds that elapsed.
34
 * @return string
35
 * @see microtime()
36
 */
37
function format_duration($seconds) {
38
    if ($seconds < 1.0e-3) {
39
        $n = number_format($seconds * 1.0e6, 0);
40
        $sx = 'μs';
41
    } elseif ($seconds < 1) {
42
        $n = number_format($seconds * 1000, 0);
43
        $sx = 'ms';
44
    } elseif ($seconds < 60) {
45
        $n = number_format($seconds, 1);
46
        $sx = 's';
47
    } elseif ($seconds < 3600) {
48
        $n = number_format($seconds / 60, 1);
49
        $sx = 'm';
50
    } elseif ($seconds < 86400) {
51
        $n = number_format($seconds / 3600, 1);
52
        $sx = 'h';
53
    } else {
54
        $n = number_format($seconds / 86400, 1);
55
        $sx = 'd';
56
    }
57
58
    $result = rtrim($n, '0.').$sx;
59
    return $result;
60
}
61
62
/**
63
 * Format a number of bytes with the largest unit.
64
 *
65
 * @param int $bytes The number of bytes.
66
 * @param int $precision The number of decimal places in the formatted number.
67
 * @return string the formatted filesize.
68
 */
69
function format_filesize($bytes, $precision = 1) {
70
    $units = array('B', 'K', 'M', 'G', 'T');
71
72
    $bytes = max((int)$bytes, 0);
73
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
74
    $pow = min($pow, count($units) - 1);
75
76
    $bytes /= pow(1024, $pow);
77
78
    $result = round($bytes, $precision).$units[$pow];
79
    return $result;
80
}
81
82
/**
83
 * Unformat a file size that was formatted using {@link format_filesize()}.
84
 *
85
 * @param $str The formatted file suze to unformat.
86
 * @return int Returns the file size in bytes.
87
 */
88
function unformat_filesize($str) {
89
    $units = array('B' => 1, 'K' => 1 << 10, 'M' => 1 << 20, 'G' => 1 << 30, 'T' => 1 << 40);
90
91
    if (preg_match('/([0-9.]+)\s*([A-Z]*)/i', $str, $matches)) {
92
        $number = floatval($matches[1]);
93
        $unit = strtoupper(substr($matches[2], 0, 1));
94
        $mult = val($unit, $units, 1);
95
96
        $result = round($number * $mult, 0);
97
        return $result;
98
    } else {
99
        return null;
100
    }
101
}
102
103
$transliterations = array('–' => '-', '—' => '-', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'Ae', 'Å' => 'A', 'Ā' => 'A', 'Ą' => 'A', 'Ă' => 'A', 'Æ' => 'Ae', 'Ç' => 'C', 'Ć' => 'C', 'Č' => 'C', 'Ĉ' => 'C', 'Ċ' => 'C', 'Ď' => 'D', 'Đ' => 'D', 'Ð' => 'D', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ē' => 'E', 'Ě' => 'E', 'Ĕ' => 'E', 'Ė' => 'E', 'Ĝ' => 'G', 'Ğ' => 'G', 'Ġ' => 'G', 'Ģ' => 'G', 'Ĥ' => 'H', 'Ħ' => 'H', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ī' => 'I', 'Ĩ' => 'I', 'Ĭ' => 'I', 'Į' => 'I', 'İ' => 'I', 'IJ' => 'IJ', 'Ĵ' => 'J', 'Ķ' => 'K', 'Ł' => 'K', 'Ľ' => 'K', 'Ĺ' => 'K', 'Ļ' => 'K', 'Ŀ' => 'K', 'Ñ' => 'N', 'Ń' => 'N', 'Ň' => 'N', 'Ņ' => 'N', 'Ŋ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'Oe', 'Ō' => 'O', 'Ő' => 'O', 'Ŏ' => 'O', 'Œ' => 'OE', 'Ŕ' => 'R', 'Ŗ' => 'R', 'Ś' => 'S', 'Š' => 'S', 'Ş' => 'S', 'Ŝ' => 'S', 'Ť' => 'T', 'Ţ' => 'T', 'Ŧ' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'Ue', 'Ū' => 'U', 'Ů' => 'U', 'Ű' => 'U', 'Ŭ' => 'U', 'Ũ' => 'U', 'Ų' => 'U', 'Ŵ' => 'W', 'Ý' => 'Y', 'Ŷ' => 'Y', 'Ÿ' => 'Y', 'Ź' => 'Z', 'Ž' => 'Z', 'Ż' => 'Z', 'Þ' => 'T', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'ae', 'å' => 'a', 'ā' => 'a', 'ą' => 'a', 'ă' => 'a', 'æ' => 'ae', 'ç' => 'c', 'ć' => 'c', 'č' => 'c', 'ĉ' => 'c', 'ċ' => 'c', 'ď' => 'd', 'đ' => 'd', 'ð' => 'd', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ē' => 'e', 'ę' => 'e', 'ě' => 'e', 'ĕ' => 'e', 'ė' => 'e', 'ƒ' => 'f', 'ĝ' => 'g', 'ğ' => 'g', 'ġ' => 'g', 'ģ' => 'g', 'ĥ' => 'h', 'ħ' => 'h', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ī' => 'i', 'ĩ' => 'i', 'ĭ' => 'i', 'į' => 'i', 'ı' => 'i', 'ij' => 'ij', 'ĵ' => 'j', 'ķ' => 'k', 'ĸ' => 'k', 'ł' => 'l', 'ľ' => 'l', 'ĺ' => 'l', 'ļ' => 'l', 'ŀ' => 'l', 'ñ' => 'n', 'ń' => 'n', 'ň' => 'n', 'ņ' => 'n', 'ʼn' => 'n', 'ŋ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'oe', 'ø' => 'o', 'ō' => 'o', 'ő' => 'o', 'ŏ' => 'o', 'œ' => 'oe', 'ŕ' => 'r', 'ř' => 'r', 'ŗ' => 'r', 'š' => 's', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'ue', 'ū' => 'u', 'ů' => 'u', 'ű' => 'u', 'ŭ' => 'u', 'ũ' => 'u', 'ų' => 'u', 'ŵ' => 'w', 'ý' => 'y', 'ÿ' => 'y', 'ŷ' => 'y', 'ž' => 'z', 'ż' => 'z', 'ź' => 'z', 'þ' => 't', 'ß' => 'ss', 'ſ' => 'ss', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'YO', 'Ж' => 'ZH', 'З' => 'Z', 'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'ș' => 's', 'ț' => 't', 'Ț' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'CH', 'Ш' => 'SH', 'Щ' => 'SCH', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'YU', 'Я' => 'YA', 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu', 'я' => 'ya');
104
105
/**
106
 * Generate a url friendly slug from a string.
107
 *
108
 * @param string $str A string to be formatted.
109
 * @return string
110
 * @global array $transliterations An array of translations from other scripts into url friendly characters.
111
 */
112
function format_slug($str) {
113
    global $transliterations;
114
115
    $str = trim($str);
116
    $str = strip_tags(html_entity_decode($str, ENT_COMPAT, 'UTF-8')); // remove html tags
117
    $str = strtr($str, $transliterations); // transliterate known characters
118
    $str = preg_replace('`([^\PP.\-_])`u', '', $str); // get rid of punctuation
119
    $str = preg_replace('`([^\PS+])`u', '', $str); // get rid of symbols
120
    $str = preg_replace('`[\s\-/+.]+`u', '-', $str); // replace certain characters with dashes
121
    $str = rawurlencode(strtolower($str));
122
    $str = trim($str, '.-');
123
    return $str;
124
}