Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Uuid
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 from
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 equals
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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\Domain\Id;
11
12use InvalidArgumentException;
13use LogicException;
14use Override;
15use Ramsey\Uuid\Uuid as UuidImplementation;
16use Ramsey\Uuid\UuidInterface;
17use Stringable;
18
19abstract readonly class Uuid implements Stringable, IdInterface
20{
21    private function __construct(private UuidInterface $uuid) {}
22
23    #[Override]
24    public function __toString(): string
25    {
26        return $this->uuid->toString();
27    }
28
29    #[Override]
30    public static function from(int|string $id): static
31    {
32        if (is_int($id)) {
33            throw new LogicException('UUID cannot be created from an integer. Use string UUIDs instead.');
34        }
35
36        if (!UuidImplementation::isValid($id)) {
37            throw new InvalidArgumentException(sprintf('Invalid UUID: %s', $id));
38        }
39
40        return new static(UuidImplementation::fromString($id));
41    }
42
43    #[Override]
44    public function getValue(): string
45    {
46        return $this->uuid->toString();
47    }
48
49    #[Override]
50    public function equals(IdInterface $other): bool
51    {
52        return $this->uuid->toString() === $other->getValue();
53    }
54}