From 487a5b2922488a3016f2fb13d10bfa91109e602c Mon Sep 17 00:00:00 2001 From: Jonathan Swaelens <jonathan.swaelens@fusiondirectory.org> Date: Wed, 10 Apr 2024 19:13:07 +0200 Subject: [PATCH] :sparkles: feat(test): Add orcid test method Add orcid test method Signed-off-by: Jonathan Swaelens <jonathan.swaelens@fusiondirectory.org> --- include/class_tests.inc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/class_tests.inc b/include/class_tests.inc index a62659773..aefb4c58e 100644 --- a/include/class_tests.inc +++ b/include/class_tests.inc @@ -365,4 +365,20 @@ class tests return ($ad >= $from && $ad <= $to); } + + /* \brief Check if the value is a valid orcid id */ + public static function is_orcid ($orcid) + { + /* 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 + (int)$baseDigit) * 2; + } + $remainder = $sum % 11; + $result = (12 - $remainder) % 11; + $orcidCheckSum = (($result == 10) ? "X" : (string)$result); + + return ($orcidCheckSum != substr($orcid, -1)); + } } -- GitLab