ldc-boutique-cnc-sable/domain/Models/ValueObjects/Code.php
Jérémy Munsch e0c4b34ed8
feat(employees): implement deterministic 6-character code generation using SerialCaster
- Add kwaadpepper/serial-int-caster and SerialCasterServiceProvider
- Update CodeGenerationService to encode Matricule deterministically with length 6
- Update Code value object to support lengths between 6 and 10 with 53 non-ambiguous characters
- Add database migration to sanitize legacy ambiguous codes
- Recreate and update unit and integration tests for CodeGenerationService and Code value object
2026-10-27 09:58:46 +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 = 6;
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;
}
}