47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domain\Models\ValueObjects;
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
use Ramsey\Uuid\UuidInterface;
|
|
use Shared\Exceptions\InvalidUuidException;
|
|
use Shared\Helpers\UuidHelper;
|
|
|
|
final readonly class OrderReference implements \Stringable
|
|
{
|
|
public function __construct(
|
|
public UuidInterface $value
|
|
) {
|
|
if (! UuidHelper::isV7($value)) {
|
|
throw new \DomainException('value must be a valid UUID v7');
|
|
}
|
|
}
|
|
|
|
public static function fromString(string $value): self
|
|
{
|
|
if (UuidHelper::isValid($value)) {
|
|
return new self(Uuid::fromString($value));
|
|
}
|
|
|
|
return self::fromBase58($value);
|
|
}
|
|
|
|
public static function fromBase58(string $base58): self
|
|
{
|
|
try {
|
|
$uuid = UuidHelper::fromBase58($base58);
|
|
} catch (InvalidUuidException) {
|
|
throw new \DomainException('Order reference must be a valid base-58 string');
|
|
}
|
|
|
|
return new self($uuid);
|
|
}
|
|
|
|
#[\Override]
|
|
public function __toString(): string
|
|
{
|
|
return UuidHelper::toBase58($this->value);
|
|
}
|
|
}
|