parent
3f0cec856e
commit
dbfa82ef81
@ -0,0 +1,37 @@ |
||||
<?php |
||||
|
||||
declare(strict_types = 1); |
||||
|
||||
/** |
||||
* @author Robert Strutts <Bob_586@Yahoo.com> |
||||
* @copyright (c) 2025, Robert Strutts |
||||
* @license MIT |
||||
*/ |
||||
namespace CodeHydrater\uuids; |
||||
|
||||
const BASE62 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||||
|
||||
class base62 { |
||||
|
||||
public static function encode_id(int $num): string { |
||||
if ($num === 0) return BASE62[0]; |
||||
$base = strlen(BASE62); |
||||
$encoded = ''; |
||||
while ($num > 0) { |
||||
$encoded = BASE62[$num % $base] . $encoded; |
||||
$num = intdiv($num, $base); |
||||
} |
||||
return $encoded; |
||||
} |
||||
|
||||
public static function decode_id(string $str): int { |
||||
$base = strlen(BASE62); |
||||
$len = strlen($str); |
||||
$num = 0; |
||||
for ($i = 0; $i < $len; $i++) { |
||||
$num = $num * $base + strpos(BASE62, $str[$i]); |
||||
} |
||||
return $num; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue