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