MCPcopy Create free account
hub / github.com/error311/FileRise / parseSize

Method parseSize

src/FileRise/Domain/AdminModel.php:24–88  ·  view source on GitHub ↗

* Parse a shorthand size value (e.g. "5G", "500M", "123K", "50MB", "10KiB") into bytes. * Accepts bare numbers (bytes) and common suffixes: K, KB, KiB, M, MB, MiB, G, GB, GiB, etc. * * @param string $val * @return int Bytes (rounded) */

(string $val)

Source from the content-addressed store, hash-verified

22 * @return int Bytes (rounded)
23 */
24 private static function parseSize(string $val): int
25 {
26 $val = trim($val);
27 if ($val === '') {
28 return 0;
29 }
30
31 // Match: number + optional unit/suffix (K, KB, KiB, M, MB, MiB, G, GB, GiB, ...)
32 if (preg_match('/^\s*(\d+(?:\.\d+)?)\s*([kmgtpezy]?i?b?)?\s*$/i', $val, $m)) {
33 $num = (float)$m[1];
34 $unit = strtolower($m[2] ?? '');
35
36 switch ($unit) {
37 case 'k':
38 case 'kb':
39 case 'kib':
40 $num *= 1024;
41 break;
42 case 'm':
43 case 'mb':
44 case 'mib':
45 $num *= 1024 ** 2;
46 break;
47 case 'g':
48 case 'gb':
49 case 'gib':
50 $num *= 1024 ** 3;
51 break;
52 case 't':
53 case 'tb':
54 case 'tib':
55 $num *= 1024 ** 4;
56 break;
57 case 'p':
58 case 'pb':
59 case 'pib':
60 $num *= 1024 ** 5;
61 break;
62 case 'e':
63 case 'eb':
64 case 'eib':
65 $num *= 1024 ** 6;
66 break;
67 case 'z':
68 case 'zb':
69 case 'zib':
70 $num *= 1024 ** 7;
71 break;
72 case 'y':
73 case 'yb':
74 case 'yib':
75 $num *= 1024 ** 8;
76 break;
77 // case 'b' or empty => bytes; do nothing
78 default:
79 // If unit is just 'b' or empty, treat as bytes.
80 // For unknown units fall back to bytes.
81 break;

Callers 2

updateConfigMethod · 0.95
getConfigMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected