@@ 58-68 (lines=11) @@ | ||
55 | * @throws \RuntimeException |
|
56 | * @return int |
|
57 | */ |
|
58 | public static function toBytes($value) |
|
59 | { |
|
60 | if (!preg_match('#^[0-9]*[BKMGT]$#i', $value)) { |
|
61 | throw new RuntimeException('Invalid size value'); |
|
62 | } |
|
63 | $units = ['B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4, 'P' => 5]; |
|
64 | $unit = strtoupper(substr($value, -1)); |
|
65 | $number = intval(substr($value, 0, -1)); |
|
66 | ||
67 | return $number * pow(1024, $units[$unit]); |
|
68 | } |
|
69 | ||
70 | /** |
|
71 | * Return time in seconds for a given value. |
|
@@ 89-99 (lines=11) @@ | ||
86 | * @throws \RuntimeException |
|
87 | * @return integer |
|
88 | */ |
|
89 | public static function toTime($offset) |
|
90 | { |
|
91 | if (!preg_match('#^[1-9]+[0-9]*[SIHDWMY]$#i', $offset)) { |
|
92 | throw new RuntimeException(sprintf('Invalid value for offset: %s', $offset)); |
|
93 | } |
|
94 | $units = ['S' => 1, 'I' => 60, 'H' => 3600, 'D' => 86400, 'W' => 604800, 'M' => 2678400, 'Y' => 31536000]; |
|
95 | $unit = strtoupper(substr($offset, -1)); |
|
96 | $number = intval(substr($offset, 0, -1)); |
|
97 | ||
98 | return $number * $units[$unit]; |
|
99 | } |
|
100 | ||
101 | /** |
|
102 | * Pads all given strings to given length. |