mardi 23 janvier 2018

Checksum function in JAVA to PhP Laravel 5 Validation

I'm trying to make a validation in Laravel for ORCID (link: https://support.orcid.org/knowledgebase/articles/116780-structure-of-the-orcid-identifier ) that has a Checksum function ( ISO 7064 11,2 )

this is the function in Java (i think)

/** 
  * Generates check digit as per ISO 7064 11,2. 
  * 
  */ 
public static String generateCheckDigit(String baseDigits) { 
    int total = 0; 
    for (int i = 0; i < baseDigits.length(); i++) { 
        int digit = Character.getNumericValue(baseDigits.charAt(i)); 
        total = (total + digit) * 2; 
    } 
    int remainder = total % 11; 
    int result = (12 - remainder) % 11; 
    return result == 10 ? "X" : String.valueOf(result); 
}

and I translated it in php like so:

    $total = 0;
    $baseDigits = $request->orcid;
    for($i=0;$i<strlen($baseDigits);$i++){
        $digit = IntlChar::getNumericValue($baseDigits[$i]);
        $total = ($total+$digit)*2;
    }
    $remainder = $total%11;
    $result=(12-$remainder)%11;
    if($result==10)
        $request->orcid="X";
    else
        $request->orcid=$result;
    $this->validate($request, [
        'orcid' => array(
            'nullable',
            'regex:/[0-9X]/u',
            'digits:1'
        )
    ]);

From what I understand the function takes in the ORCID (xxxx-xxxx-xxxx-xxxx) and makes a checksum mod 11. The result has to be 1 character long, which goes from 0-9 and if it's 10 then the result is 'X'. Is the translation right? Is my regex validation right?

The ORCID is user input. ORCID Examples:
0000-0002-1825-0097
0000-0001-5109-3700
0000-0002-1694-233X



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire