<?php
class DowOsValidator {
public static function valid($value) {
//delete white chars
$value = str_replace(' ', '', $value);
//if is too short or too long
if (strlen($value) <= 8 || strlen($value) > 9) {
return false;
}
$defValue = [
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19,
'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29,
'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35
];
$importance = [7, 3, 1, 9, 7, 3, 1, 7, 3];
try {
//change to upper
$value = strtoupper($value);
$identityCardSum = 0;
for ($i = 0; $i < 9; $i++) {
if (!isset($defValue[$value[$i]]) || ($i < 3 && $defValue[$value[$i]] < 10)) {
return false;
} elseif (($i > 2 && $defValue[$value[$i]] > 9)) {
return false;
}
$identityCardSum += ((int) $defValue[$value[$i]]) * $importance[$i];
}
if ($identityCardSum % 10 != 0) {
return false;
}
} catch (\Exception $ex) {
return false;
}
return true;
}
}