Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Uuid | |
100.00% |
9 / 9 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| from | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| equals | |
100.00% |
1 / 1 |
|
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 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Phexium\Domain\Id; |
| 11 | |
| 12 | use InvalidArgumentException; |
| 13 | use LogicException; |
| 14 | use Override; |
| 15 | use Ramsey\Uuid\Uuid as UuidImplementation; |
| 16 | use Ramsey\Uuid\UuidInterface; |
| 17 | use Stringable; |
| 18 | |
| 19 | abstract 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 | } |