Created
September 23, 2021 09:21
-
-
Save robertuniqid/efbd00d44af1b73f00f51171487870f1 to your computer and use it in GitHub Desktop.
Romania Validate CNP - PHP
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 | |
// The goal is to prevent human error, not cheating the system. | |
class Sample { | |
private static $_cnpValidationConstant = "279146358279"; | |
public static function isValidCNP( $cnp ) { | |
$cnp = intval( $cnp ); | |
if( strlen( '' . $cnp ) !== ( strlen( self::$_cnpValidationConstant ) + 1 ) || !is_numeric( $cnp ) ) | |
return false; | |
$security = $cnp % 10; | |
$checksum = 0; | |
$numberNavigation = floor( $cnp / 10 ); | |
for( $currentDigit = $numberNavigation % 10, $currentPosition = strlen( self::$_cnpValidationConstant ) - 1; | |
$currentPosition >= 0; | |
$numberNavigation /= 10, $currentDigit = $numberNavigation % 10, $currentPosition-- ) | |
$checksum += $currentDigit * self::$_cnpValidationConstant[ $currentPosition ]; | |
$checksum = $checksum % 11; | |
return $checksum === 10 ? ( $security === 1 ) : $security === $checksum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment