Skip to content

Commit

Permalink
System: add Str::uuid7()
Browse files Browse the repository at this point in the history
  • Loading branch information
esyede committed Sep 25, 2024
1 parent 93a2b12 commit 47ffeee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions system/str.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,29 @@ public static function uuid()
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(static::bytes(16)), 4));
}

/**
* Buat string UUID (versi 7).
* (Universally Unique Identifier versi 7).
*
* @return string
*/
public static function uuid7()
{
$ts = intval(microtime(true) * 1000);
return sprintf(
'%02x%02x%02x%02x-%02x%02x-%04x-%04x-%012x',
($ts >> 40) & 0xFF,
($ts >> 32) & 0xFF,
($ts >> 24) & 0xFF,
($ts >> 16) & 0xFF,
($ts >> 8) & 0xFF,
$ts & 0xFF,
static::integers(0, 0x0FFF) | 0x7000,
static::integers(0, 0x3FFF) | 0x8000,
static::integers(0, 0xFFFFFFFFFFFF),
);
}

/**
* Buat string ULID.
* (Universally Unique Lexicographically Sortable Identifier).
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/str.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ public function testUuid()
$this->assertTrue((bool) preg_match('/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/Di', $uuid));
}



public function testUlid()
{
$this->assertTrue((bool) preg_match('/[0-9][A-Z]/', Str::ulid()));
Expand Down

0 comments on commit 47ffeee

Please sign in to comment.