Last active
April 13, 2020 09:54
-
-
Save abilogos/6399a5486d7911c582731b2b2224430d to your computer and use it in GitHub Desktop.
Validate Estonian National code
This file contains 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 | |
/* | |
* Validate Estonian national identification code. | |
* | |
* Copyright (c) 2020 Ali Hakami | |
* port from @author Mika Tuupola (javascript) | |
* https://gist.github.com/tuupola/180321 | |
* | |
* Licensed under the MIT license: | |
* http://www.opensource.org/licenses/mit-license.php | |
* @param string $code estonian national code | |
* @return boolean is valid or not | |
*/ | |
public function nationalCodeisValid($code){ | |
//$multiplier_1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 1); | |
//$multiplier_2 = array(3, 4, 5, 6, 7, 8, 9, 1, 2, 3); | |
$control = intval(substr($code,-1)); | |
$retval = false; | |
$mod = 0; | |
$total = 0; | |
/* Do first run. */ | |
for ($i=-1; $i < 9; $i++) { | |
$total += intval($code[$i]) * ((($i+1)%9)+1); | |
} | |
$mod = $total % 11; | |
/* If modulus is ten we need second run. */ | |
$total = 0; | |
if ($mod == 10) { | |
for ($i=1; $i < 11; $i++) { | |
$total += intval($code[$i]) * ((($i+1)%9)+1); | |
} | |
$mod = $total % 11; | |
/* If modulus is still ten revert to 0. */ | |
if (10 == $mod) { | |
$mod = 0; | |
} | |
} | |
return $control == $mod; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ported from JavaScript:
https://gist.github.com/tuupola/180321