forked from CodelyTV/php-ddd-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUuid.php
More file actions
44 lines (35 loc) · 936 Bytes
/
Uuid.php
File metadata and controls
44 lines (35 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
declare(strict_types=1);
namespace CodelyTv\Shared\Domain\ValueObject;
use InvalidArgumentException;
use Ramsey\Uuid\Uuid as RamseyUuid;
use Stringable;
class Uuid implements Stringable
{
public function __construct(protected string $value)
{
$this->ensureIsValidUuid($value);
}
public static function random(): self
{
return new static(RamseyUuid::uuid4()->toString());
}
public function value(): string
{
return $this->value;
}
public function equals(Uuid $other): bool
{
return $this->value() === $other->value();
}
public function __toString(): string
{
return $this->value();
}
private function ensureIsValidUuid(string $id): void
{
if (!RamseyUuid::isValid($id)) {
throw new InvalidArgumentException(sprintf('<%s> does not allow the value <%s>.', static::class, $id));
}
}
}