Last active
January 2, 2023 09:03
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Thanks! 👍 Awesome!
Haha, merci @gbaudoin 🙌 !!
Thank you!!
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.
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
.
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
calculate function return 10 when the Control digit should be 0. follows quick fix:
public static function calculate($number) {
$report = 0;
foreach(str_split($number) as $key => $value) {
$report = self::$table[$report][(int)$value];
}
return (10 - $report) % 10;
}