1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MPijierro\IdentityPhp; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Check that a string is valid for official document types in Spain |
9
|
|
|
* |
10
|
|
|
* Class Identity |
11
|
|
|
* |
12
|
|
|
* @package MPijierro\IdentityPhp |
13
|
|
|
*/ |
14
|
|
|
final class Identity |
15
|
|
|
{ |
16
|
|
|
public function isValidIBAN(string $aIbanNumber): bool |
17
|
|
|
{ |
18
|
|
|
$iban = new \IBAN(); |
19
|
|
|
|
20
|
|
|
return $iban->Verify($aIbanNumber); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Check that CIF string is valid |
25
|
|
|
* |
26
|
|
|
* @param string $cif |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
|
|
public function isValidCIF(string $cif): bool |
30
|
|
|
{ |
31
|
|
|
$cifRegEx1 = '/^[ABEH][0-9]{8}$/i'; |
32
|
|
|
$cifRegEx2 = '/^[KPQS][0-9]{7}[A-J]$/i'; |
33
|
|
|
$cifRegEx3 = '/^[CDFGJLMNRUVW][0-9]{7}[0-9A-J]$/i'; |
34
|
|
|
|
35
|
|
|
if (preg_match($cifRegEx1, $cif) || preg_match($cifRegEx2, $cif) || preg_match($cifRegEx3, $cif)) { |
36
|
|
|
$control = $cif[strlen($cif) - 1]; |
37
|
|
|
$suma_A = 0; |
38
|
|
|
$suma_B = 0; |
39
|
|
|
|
40
|
|
|
for ($i = 1; $i < 8; $i++) { |
41
|
|
|
if ($i % 2 == 0) { |
42
|
|
|
$suma_A += (int)$cif[$i]; |
43
|
|
|
} else { |
44
|
|
|
|
45
|
|
|
$t = (string)((int)$cif[$i] * 2); |
46
|
|
|
$p = 0; |
47
|
|
|
|
48
|
|
|
for ($j = 0; $j < strlen($t); $j++) { |
49
|
|
|
$p += substr($t, $j, 1); |
50
|
|
|
} |
51
|
|
|
$suma_B += $p; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$suma_C = (int)($suma_A + $suma_B) . ""; |
56
|
|
|
$suma_D = (10 - (int)$suma_C[strlen($suma_C) - 1]) % 10; |
57
|
|
|
|
58
|
|
|
$letras = "JABCDEFGHI"; |
59
|
|
|
|
60
|
|
|
if ($control >= "0" && $control <= "9") { |
61
|
|
|
return ($control == $suma_D); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return (strtoupper($control) == $letras[$suma_D]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check that NIF string is valid |
72
|
|
|
* |
73
|
|
|
* @param string $nif |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function isValidNIF(string $nif): bool |
77
|
|
|
{ |
78
|
|
|
$nifRegEx = '/^[0-9]{8}[A-Z]$/i'; |
79
|
|
|
|
80
|
|
|
$letras = "TRWAGMYFPDXBNJZSQVHLCKE"; |
81
|
|
|
|
82
|
|
|
if (preg_match($nifRegEx, $nif)) { |
83
|
|
|
return ($letras[(substr($nif, 0, 8) % 23)] == $nif[8]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* * Check that NIE string is valid |
91
|
|
|
* |
92
|
|
|
* @param string $nif |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function isValidNIE(string $nif): bool |
96
|
|
|
{ |
97
|
|
|
$nieRegEx = '/^[KLMXYZ][0-9]{7}[A-Z]$/i'; |
98
|
|
|
$letras = "TRWAGMYFPDXBNJZSQVHLCKE"; |
99
|
|
|
|
100
|
|
|
if (preg_match($nieRegEx, $nif)) { |
101
|
|
|
|
102
|
|
|
$r = str_replace(['X', 'Y', 'Z'], [0, 1, 2], $nif); |
103
|
|
|
|
104
|
|
|
return ($letras[(substr($r, 0, 8) % 23)] == $nif[8]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return false; |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |