40 lines
1.1 KiB
PHP
40 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domain\Models\ValueObjects;
|
|
|
|
use Symfony\Component\Uid\Uuid;
|
|
use Symfony\Component\Uid\UuidV7;
|
|
|
|
final readonly class OrderReference implements \Stringable
|
|
{
|
|
public function __construct(
|
|
public string $value
|
|
) {
|
|
$value = trim($value);
|
|
|
|
if (empty($value)) {
|
|
throw new \DomainException('Order reference cannot be empty');
|
|
}
|
|
|
|
if (! preg_match('/^[1-9A-HJ-NP-Za-km-z]{21,22}$/', $value)) {
|
|
throw new \DomainException('Order reference must be a valid base-58 string of 21 or 22 characters');
|
|
}
|
|
|
|
try {
|
|
$uuid = Uuid::fromBase58($value);
|
|
if (get_class($uuid) !== UuidV7::class) {
|
|
throw new \DomainException('Order reference must be generated from a UUID v7');
|
|
}
|
|
} catch (\InvalidArgumentException $e) {
|
|
throw new \DomainException('Order reference must be a valid base-58 string');
|
|
}
|
|
}
|
|
|
|
#[\Override]
|
|
public function __toString(): string
|
|
{
|
|
return $this->value;
|
|
}
|
|
}
|