ldc-mesbr-app/src/Domain/Models/ValueObjects/Brand/BrandCode.php

52 lines
1.2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects\Brand;
use DomainException;
use Shared\DDD\Equatable;
final readonly class BrandCode implements Equatable
{
/**
* @param string $value Unique identification code.
*/
public function __construct(
private string $value,
) {
if (! preg_match('/^[A-Z]{3}\d{2}$/', $value)) {
throw new DomainException(
"Invalid code '{$value}' does not match pattern /^[A-Z]{3}\d{2}$/"
);
}
if ($value[3] === $value[4]) {
throw new DomainException(
"Invalid code '{$value}' has the same digit repeated"
);
}
}
/**
* Get the value of the unique identification code.
*/
public function value(): string
{
return $this->value;
}
/**
* Compare the unique identification code of the brand.
*
* @param mixed $other Unique identification code to compare with.
*/
public function equals(mixed $other): bool
{
if (! $other instanceof self) {
return false;
}
return $this->value === $other->value;
}
}