Skip to content

Commit a7ed2ca

Browse files
committed
Add ULID generator
1 parent fe89df5 commit a7ed2ca

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gember\IdentityGeneratorSymfony\Ulid;
6+
7+
use Gember\EventSourcing\Util\Generator\Identity\IdentityGenerator;
8+
use Override;
9+
use Symfony\Component\Uid\Factory\UlidFactory;
10+
11+
final readonly class SymfonyUlidIdentityGenerator implements IdentityGenerator
12+
{
13+
public function __construct(
14+
private UlidFactory $ulidFactory,
15+
) {}
16+
17+
#[Override]
18+
public function generate(): string
19+
{
20+
return (string) $this->ulidFactory->create();
21+
}
22+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gember\IdentityGeneratorSymfony\Test\Ulid;
6+
7+
use Gember\IdentityGeneratorSymfony\Ulid\SymfonyUlidIdentityGenerator;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use PHPUnit\Framework\TestCase;
10+
use Override;
11+
use Symfony\Component\Uid\Factory\UlidFactory;
12+
13+
/**
14+
* @internal
15+
*/
16+
final class SymfonyUlidIdentityGeneratorTest extends TestCase
17+
{
18+
private SymfonyUlidIdentityGenerator $identityGenerator;
19+
20+
#[Override]
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->identityGenerator = new SymfonyUlidIdentityGenerator(
26+
new UlidFactory(),
27+
);
28+
}
29+
30+
#[Test]
31+
public function itShouldGenerateUlid(): void
32+
{
33+
$ulid = $this->identityGenerator->generate();
34+
35+
self::assertMatchesRegularExpression(
36+
'#[0-7][0-9A-HJKMNP-TV-Z]{25}#',
37+
$ulid,
38+
);
39+
}
40+
}

0 commit comments

Comments
 (0)