<?php
 namespace App\Modules\Citizenship\FR; use App\Core\Citizenship\AbstractCitizenship; class FR extends AbstractCitizenship { public function getVerificationFields(): array { return [ 'individual_id' => [ 'type' => 'text', 'label' => 'Numéro de Sécurité Sociale (NIR)', 'placeholder' => '1 84 12 76 451 089 46', 'group' => 'individual', ], 'company_id' => [ 'type' => 'text', 'label' => 'SIRET', 'placeholder' => '12345678901234', 'group' => 'company', ], ]; } public function validateField(string $fieldKey, string $value, array $allData = []): array { return match ($fieldKey) { 'individual_id' => $this->validateNir($value), 'company_id' => $this->validateSiret($value), default => $this->successResult(), }; } protected function getModuleLabel(): string { return "FR — France Vérification d'Identité"; } protected function getModuleDescription(): string { return 'NIR and SIRET validation for France'; } private function validateNir(string $value): array { $clean = strtoupper(preg_replace('/\s+/', '', trim($value))); if (! preg_match('/^([12])(\d{4})(\d{2}|2[AB])(\d{6})(\d{2})$/', $clean, $m)) { return $this->failResult('NIR invalide : format attendu 15 caractères (chiffres, Corse 2A/2B admis).'); } $dept = $m[3]; $key = (int) $m[5]; $deptNumeric = match ($dept) { '2A' => '19', '2B' => '18', default => $dept, }; $number = $m[1] . $m[2] . $deptNumeric . $m[4]; $expectedKey = 97 - ((int) $number % 97); if ($key < 1 || $key > 97 || $expectedKey !== $key) { return $this->failResult('NIR invalide : clé de contrôle incorrecte.'); } return $this->successResult('NIR valide.'); } private function validateSiret(string $value): array { $clean = preg_replace('/\s+/', '', trim($value)); if (! preg_match('/^\d{14}$/', $clean)) { return $this->failResult('Le SIRET doit contenir 14 chiffres.'); } if (! $this->luhnValid($clean)) { return $this->failResult('SIRET invalide : la clé de Luhn est incorrecte.'); } return $this->successResult('SIRET valide.'); } private function luhnValid(string $digits): bool { $sum = 0; $len = strlen($digits); for ($i = 0; $i < $len; $i++) { $d = (int) $digits[$len - 1 - $i]; if ($i % 2 === 1) { $d *= 2; if ($d > 9) { $d -= 9; } } $sum += $d; } return $sum % 10 === 0; } } 