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; } }