Skip to content

Commit 5690fc2

Browse files
pl-githubtemp
authored andcommitted
feat: Provide UUID factory
1 parent 272aa36 commit 5690fc2

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Uuid/UuidTrait.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ final protected function setUpUuidTrait(): void
2828
$this->lastUuidValue = 0;
2929
}
3030

31-
final protected function nextUuid(): string
31+
final protected static function uuidFromInteger(int $number): string
3232
{
33-
$this->lastUuidValue ??= 0;
34-
35-
$uuid = str_pad(dechex(++$this->lastUuidValue), 32, '0', STR_PAD_LEFT);
33+
$uuid = str_pad(dechex($number), 32, '0', STR_PAD_LEFT);
3634
$uuid = substr_replace($uuid, '-', 8, 0);
3735
$uuid = substr_replace($uuid, '-', 13, 0);
3836
$uuid = substr_replace($uuid, '-', 18, 0);
@@ -41,6 +39,13 @@ final protected function nextUuid(): string
4139
return (string) Uuid::fromString($uuid);
4240
}
4341

42+
final protected function nextUuid(): string
43+
{
44+
$this->lastUuidValue ??= 0;
45+
46+
return self::uuidFromInteger(++$this->lastUuidValue);
47+
}
48+
4449
final protected static function assertIsUuid(mixed $actual, string $message = ''): void
4550
{
4651
self::assertIsString($actual, $message);

tests/Uuid/UuidTraitTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ final class UuidTraitTest extends TestCase
1717
{
1818
use UuidTrait;
1919

20+
public function testCreateUuidByInteger(): void
21+
{
22+
self::assertEquals('00000000-0000-0000-0000-000000000001', self::uuidFromInteger(1));
23+
self::assertEquals('00000000-0000-0000-0000-000000000002', self::uuidFromInteger(2));
24+
}
25+
2026
public function testNextUuid(): void
2127
{
2228
self::assertEquals('00000000-0000-0000-0000-000000000001', $this->nextUuid());

0 commit comments

Comments
 (0)