2026-09-07 16:53:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Domain\Models\ValueObjects\Brand;
|
|
|
|
|
|
2026-09-14 10:52:04 +02:00
|
|
|
final readonly class BrandCode
|
2026-09-07 16:53:09 +02:00
|
|
|
{
|
2026-09-14 10:52:04 +02:00
|
|
|
private const string REGEX = '/^[A-Z]{3}\d{2}$/';
|
|
|
|
|
|
2026-09-07 16:53:09 +02:00
|
|
|
/**
|
|
|
|
|
* @param string $value Unique identification code.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
2026-09-14 10:52:04 +02:00
|
|
|
public string $value,
|
2026-09-07 16:53:09 +02:00
|
|
|
) {
|
2026-09-14 10:52:04 +02:00
|
|
|
if (! preg_match(self::REGEX, $value)) {
|
|
|
|
|
throw new \DomainException(
|
|
|
|
|
"Invalid code '{$value}' does not match pattern " . self::REGEX,
|
2026-09-07 16:53:09 +02:00
|
|
|
);
|
|
|
|
|
}
|
2026-09-14 10:52:04 +02:00
|
|
|
|
2026-09-07 16:53:09 +02:00
|
|
|
if ($value[3] === $value[4]) {
|
2026-09-14 10:52:04 +02:00
|
|
|
throw new \DomainException(
|
|
|
|
|
"Invalid code '{$value}' has the same digit repeated",
|
2026-09-07 16:53:09 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|