feat: shortened and filename-safe request ids
This commit is contained in:
1 parent
1cc7a025a1
commit
33609ab534
6 files changed
+70
-4
No files matched your search
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
class RequestIdGenerator
|
||||
{
|
||||
private const TIMESTAMP_LENGTH = 10;
|
||||
private const RANDOM_LENGTH = 8;
|
||||
|
||||
public function generateBaseId(): string
|
||||
{
|
||||
$timestampMs = (int) floor(microtime(true) * 1000);
|
||||
$timestampPart = str_pad(base_convert((string) $timestampMs, 10, 36), self::TIMESTAMP_LENGTH, '0', STR_PAD_LEFT);
|
||||
|
||||
$maxRandomValue = (36 ** self::RANDOM_LENGTH) - 1;
|
||||
$randomPart = str_pad(base_convert((string) random_int(0, $maxRandomValue), 10, 36), self::RANDOM_LENGTH, '0', STR_PAD_LEFT);
|
||||
|
||||
return sprintf('r-%s-%s', $timestampPart, $randomPart);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user