Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UlidGenerator
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 generate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 from
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10namespace Phexium\Plugin\IdGenerator\Adapter;
11
12use LogicException;
13use Override;
14use Phexium\Domain\Id\Ulid;
15use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface;
16use Ramsey\Uuid\Uuid;
17
18final readonly class UlidGenerator implements IdGeneratorInterface
19{
20    #[Override]
21    public function generate(): Ulid
22    {
23        $uuid = Uuid::uuid7();
24
25        return Ulid::from($uuid->toString());
26    }
27
28    #[Override]
29    public function from(int|string $id): Ulid
30    {
31        if (is_int($id)) {
32            throw new LogicException('Ulid generator cannot create ULIDs from integers. Use string ULIDs instead.');
33        }
34
35        return Ulid::from($id);
36    }
37}