Created
April 28, 2017 12:20
-
-
Save hewerthomn/cc60a9db2d96ef0ffaecd951821fe410 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Calculate the mod11 | |
* @param string $baseVal | |
* @param string $separator | |
* @return string | |
*/ | |
public function mod11($baseVal = "", $separator = '-') | |
{ | |
$result = ""; | |
$weight = [ 2, 3, 4, 5, 6, 7, | |
2, 3, 4, 5, 6, 7, | |
2, 3, 4, 5, 6, 7, | |
2, 3, 4, 5, 6, 7 ]; | |
/* For convenience, reverse the string and work left to right. */ | |
$reversedBseVal = strrev($baseVal); | |
for ($i=0, $sum=0; $i < strlen($reversedBseVal); $i++) { | |
/* Calculate product and accumulate. */ | |
$sum += substr($reversedBseVal, $i, 1) * $weight[$i]; | |
} | |
/* Determine check digit, and concatenate to base value. */ | |
$remainder = $sum % 11; | |
switch ($remainder) { | |
case 0: | |
case 1: | |
$result = "{$baseVal}{$separator}0"; | |
break; | |
default: | |
$checkDigit = 11 - $remainder; | |
$result = "{$baseVal}{$separator}{$checkDigit}"; | |
break; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment