2026-02-21 18:26:45 +03:00

60 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Domain\Shared\ValueObjects;
use DomainException;
use Ramsey\Uuid\Uuid as RamseyUuid;
use Ramsey\Uuid\UuidInterface;
final readonly class Uuid
{
private function __construct(
private UuidInterface $value
) {
}
public static function generate(): self
{
return new self(RamseyUuid::uuid4());
}
public static function fromString(string $value): self
{
if (!RamseyUuid::isValid($value)) {
throw new DomainException(sprintf('Недопустимая строка UUID: %s', $value));
}
return new self(RamseyUuid::fromString($value));
}
public function toString(): string
{
return $this->value->toString();
}
public function equals(self $other): bool
{
return $this->value->equals($other);
}
public function __toString(): string
{
return $this->toString();
}
public static function validate(string|array $uuid): bool
{
$uuids = is_array($uuid) ? $uuid : [$uuid];
foreach ($uuids as $u) {
if (!RamseyUuid::isValid($u)) {
return false;
}
}
return true;
}
}