Commit 9fa74474 authored by Côme Chilliet's avatar Côme Chilliet

Merge branch '5991-orcid-last-character-may-be-x-and-not-a-number' into '1.4-dev'

Resolve "ORCID last character may be 'X' and not a number"

See merge request fusiondirectory/fd-plugins!611
parents 195ae92b 3cf05bd7
......@@ -141,9 +141,25 @@ class socialHandler_orcid extends socialHandler
function validate ($value)
{
if (!preg_match('/^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}$/', $value)) {
if (!preg_match('/^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]$/', $value)) {
throw new socialHandlerInvalidValueException(_('ORCID account IDs must look like XXXX-XXXX-XXXX-XXXX where X are digits'));
}
if (static::orcidCheckSum($value) != substr($value, -1)) {
throw new socialHandlerInvalidValueException(_('Incorrect ORCID value, the checksum does not match'));
}
return $value;
}
public static function orcidCheckSum (string $orcid): string
{
/* Remove hyphens, remove last digit, convert to array */
$baseDigits = str_split(str_replace('-', '', substr($orcid, 0, -1)));
$sum = 0;
foreach ($baseDigits as $baseDigit) {
$sum = ($sum + $baseDigit) * 2;
}
$remainder = $sum % 11;
$result = (12 - $remainder) % 11;
return (($result == 10) ? "X" : (string)$result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment