ldc-boutique-cnc-sable/domain/Models/ValueObjects/Code.php

40 lines
1 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects;
use Shared\DDD\Equatable;
final readonly class Code implements \Stringable, Equatable
{
2026-10-05 16:33:20 +02:00
public const int LENGTH = 7;
2026-06-14 21:32:07 +02:00
public const string ALLOWED_CHARACTERS = 'ABCDEFGHJKMNPQRSTUWXYZabcdefghjkmnpqrstuwxyz123456789';
2026-06-14 21:32:07 +02:00
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');
2026-06-14 21:32:07 +02:00
}
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;
}
}