-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathICOValidator.php
More file actions
53 lines (42 loc) · 1.11 KB
/
ICOValidator.php
File metadata and controls
53 lines (42 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Czechphp\ICOValidator;
use function preg_match;
use function preg_replace;
use function str_split;
/**
* Used sources
*
* @link https://cs.wikipedia.org/wiki/Identifika%C4%8Dn%C3%AD_%C4%8D%C3%ADslo_osoby
* @link https://phpfashion.com/jak-overit-platne-ic-a-rodne-cislo
*/
final class ICOValidator
{
public const ERROR_NONE = 0;
public const ERROR_FORMAT = 1;
public const ERROR_MODULO = 2;
public function validate(string $value): int
{
// clean input
$value = preg_replace('/\s+/', '', $value);
if (preg_match('/^\d{8}$/', $value) !== 1) {
return self::ERROR_FORMAT;
}
$chars = str_split($value);
$sum = 0;
for ($i = 0, $w = 8; $i < 7; $i++, $w--) {
$sum += $chars[$i] * $w;
}
$modulo = $sum % 11;
if ($modulo === 0) {
$c = 1;
} elseif ($modulo === 1) {
$c = 0;
} else {
$c = 11 - $modulo;
}
if ((int) $chars[7] !== $c) {
return self::ERROR_MODULO;
}
return self::ERROR_NONE;
}
}