- 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
46 lines
1.2 KiB
PHP
46 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use Domain\Models\ValueObjects\Code;
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
use Kwaadpepper\Serial\SerialCaster;
|
|
use Kwaadpepper\Serial\SerialCasterBuilder;
|
|
|
|
class CodeGenerationService
|
|
{
|
|
private SerialCaster $serialCaster;
|
|
|
|
public function __construct(
|
|
SerialCasterBuilder $serialCasterBuilder
|
|
) {
|
|
$this->serialCaster = $serialCasterBuilder
|
|
->withChars(Code::ALLOWED_CHARACTERS)
|
|
->withLength(Code::LENGTH)
|
|
->build();
|
|
}
|
|
|
|
public function generateFrom(Matricule $matricule): Code
|
|
{
|
|
return $this->generateCodeFromMatricule($matricule);
|
|
}
|
|
|
|
private function generateCodeFromMatricule(Matricule $value): Code
|
|
{
|
|
$integerValue = $this->matriculeToInteger($value);
|
|
$codeValue = $this->serialCaster->encode($integerValue);
|
|
|
|
return new Code($codeValue);
|
|
}
|
|
|
|
private function matriculeToInteger(Matricule $matricule): int
|
|
{
|
|
if (! ctype_digit($matricule->value)) {
|
|
throw new \InvalidArgumentException('Matricule must be numeric');
|
|
}
|
|
|
|
return (int) $matricule->value;
|
|
}
|
|
}
|