ldc-boutique-cnc-sable/domain/Models/ValueObjects/Code.php
2026-10-27 09:58:59 +01:00

39 lines
1 KiB
PHP

<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects;
use Shared\DDD\Equatable;
final readonly class Code implements \Stringable, Equatable
{
public const int LENGTH = 7;
public const string ALLOWED_CHARACTERS = 'ABCDEFGHJKMNPQRSTUWXYZabcdefghjkmnpqrstuwxyz123456789';
public function __construct(
public string $value
) {
if (trim($value) === '') {
throw new \DomainException('Code cannot be empty');
}
$length = strlen($value);
if ($length < 6 || $length > 10) {
throw new \DomainException('Code has to be between 6 and 10 chars length');
}
if (! preg_match('/^[' . self::ALLOWED_CHARACTERS . ']+$/', $value)) {
throw new \DomainException('Code contains invalid characters');
}
}
public function __toString(): string
{
return $this->value;
}
public function equals(mixed $other): bool
{
return $other instanceof self && $this->value === $other->value;
}
}