Skip to content

Instantly share code, notes, and snippets.

@gbaudoin
Last active January 2, 2023 09:03
Show Gist options
  • Save gbaudoin/9066c1a3d6ab68351635 to your computer and use it in GitHub Desktop.
Save gbaudoin/9066c1a3d6ab68351635 to your computer and use it in GitHub Desktop.
Calcul du chiffe-clé en PHP, modulo 10 récursif, BVR PostFinance Suisse
<?php
/**
* Calcul du chiffre-clé en PHP, modulo 10, récursif BVR Suisse
* Berechnung der Prüfziffer nach Modulo 10, rekursiv BESR Schweiz
* Calculating the Check Digits Using Module 10, Recursively
*
* @author gbaudoin
* @license GPL
* @see https://www.postfinance.ch/content/dam/pf/de/doc/consult/manual/dldata/efin_recdescr_man_fr.pdf
* @see https://gist.github.com/christianmeichtry/9348451
*//
class Bvr {
private static $table = [
[0,9,4,6,8,2,7,1,3,5],
[9,4,6,8,2,7,1,3,5,0],
[4,6,8,2,7,1,3,5,0,9],
[6,8,2,7,1,3,5,0,9,4],
[8,2,7,1,3,5,0,9,4,6],
[2,7,1,3,5,0,9,4,6,8],
[7,1,3,5,0,9,4,6,8,2],
[1,3,5,0,9,4,6,8,2,7],
[3,5,0,9,4,6,8,2,7,1],
[5,0,9,4,6,8,2,7,1,3]
];
public static function calculate($number) {
$report = 0;
foreach(str_split($number) as $key => $value) {
$report = self::$table[$report][(int)$value];
}
return (10 - $report) % 10;
}
public static function validate($number, $digit) {
return $digit == self::calculate($number);
}
}
@bholliger
Copy link

Thanks! 👍 Awesome!

@markcameron
Copy link

Haha, merci @gbaudoin 🙌 !!

@athavan94
Copy link

Thank you!!

@FredBoard
Copy link

Bonjour Guillaume,
Je n'arrive pas a générer 80 10690 00000 00000 00000 4593 en => 80 10690 00000 00000 00000 45932
Vous pourriez donner un exemple d’utilisation de votre class Bvr.php ?
Merci et bravo pour votre travail.

@markcameron
Copy link

Normalement Il faut juste faire Bvr::calculate('80106900000000000000004593') et cela devrait retourner 2 (cela le fait pour moi). Par contre il faut bien envoyé le numéro de référence sans les espaces entre les sections, sinon la fonction retourne 4.

@FredBoard
Copy link

FredBoard commented Apr 15, 2022

Super cela fonctionne !
il me manquait la syntaxe correcte : $chk_mod=Bvr::calculate('80106900000000000000004593');
Merci, good job !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment