1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orumad\SpanishValidator; |
4
|
|
|
|
5
|
|
|
class Validator |
6
|
|
|
{ |
7
|
|
|
public function isValidTaxNumber($value) |
8
|
|
|
{ |
9
|
|
|
return |
10
|
|
|
$this->isValidNif($value) or |
11
|
|
|
$this->isValidNie($value) or |
12
|
|
|
$this->isValidCif($value); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function isValidPersonalId($value) |
16
|
|
|
{ |
17
|
|
|
return |
18
|
|
|
$this->isValidNif($value) or |
19
|
|
|
$this->isValidNie($value); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function isValidNif($value) |
23
|
|
|
{ |
24
|
|
|
$regEx = '/^[0-9]{8}[A-Z]$/i'; |
25
|
|
|
|
26
|
|
|
$letters = 'TRWAGMYFPDXBNJZSQVHLCKE'; |
27
|
|
|
|
28
|
|
|
if (preg_match($regEx, $value)) { |
29
|
|
|
return $letters[(substr($value, 0, 8) % 23)] == $value[8]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function isValidNie($value) |
36
|
|
|
{ |
37
|
|
|
$regEx = '/^[KLMXYZ][0-9]{7}[A-Z]$/i'; |
38
|
|
|
$letters = 'TRWAGMYFPDXBNJZSQVHLCKE'; |
39
|
|
|
|
40
|
|
|
if (preg_match($regEx, $value)) { |
41
|
|
|
$replaced = str_replace(['X', 'Y', 'Z'], [0, 1, 2], $value); |
42
|
|
|
|
43
|
|
|
return $letters[(substr($replaced, 0, 8) % 23)] == $value[8]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function isValidCif($value) |
50
|
|
|
{ |
51
|
|
|
$regEx1 = '/^[ABEH][0-9]{8}$/i'; |
52
|
|
|
$regEx2 = '/^[KPQS][0-9]{7}[A-J]$/i'; |
53
|
|
|
$regEx3 = '/^[CDFGJLMNRUVW][0-9]{7}[0-9A-J]$/i'; |
54
|
|
|
|
55
|
|
|
if (preg_match($regEx1, $value) || |
56
|
|
|
preg_match($regEx2, $value) || |
57
|
|
|
preg_match($regEx3, $value)) { |
58
|
|
|
$digit = $value[strlen($value) - 1]; |
59
|
|
|
$sum1 = 0; |
60
|
|
|
$sum2 = 0; |
61
|
|
|
|
62
|
|
|
for ($i = 1; $i < 8; $i++) { |
63
|
|
|
if ($i % 2 == 0) { |
64
|
|
|
$sum1 += intval($value[$i]); |
65
|
|
|
} else { |
66
|
|
|
$tot = (intval($value[$i]) * 2); |
67
|
|
|
$par = 0; |
68
|
|
|
|
69
|
|
|
for ($j = 0; $j < strlen($tot); $j++) { |
70
|
|
|
$par += substr($tot, $j, 1); |
71
|
|
|
} |
72
|
|
|
$sum2 += $par; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$sum3 = (intval($sum1 + $sum2)).''; |
77
|
|
|
$sum4 = (10 - intval($sum3[strlen($sum3) - 1])) % 10; |
78
|
|
|
|
79
|
|
|
$letters = 'JABCDEFGHI'; |
80
|
|
|
|
81
|
|
|
if ($digit >= '0' && $digit <= '9') { |
82
|
|
|
return $digit == $sum4; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return strtoupper($digit) == $letters[$sum4]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function isValidNSS($value) |
92
|
|
|
{ |
93
|
|
|
$regEx = '/^[0-9]{12}$/i'; |
94
|
|
|
|
95
|
|
|
if (preg_match($regEx, $value)) { |
96
|
|
|
$num1 = substr($value, 0, 2); |
97
|
|
|
$num2 = substr($value, 2, 8); |
98
|
|
|
$num3 = substr($value, 10, 2); |
99
|
|
|
$num4 = null; |
|
|
|
|
100
|
|
|
|
101
|
|
|
if ($num1 && $num2 && $num3) { |
102
|
|
|
if ($num2 < 10000000) { |
103
|
|
|
$num4 = $num2 + $num1 * 10000000; |
104
|
|
|
} else { |
105
|
|
|
$num4 = $num1.$num2; |
106
|
|
|
} |
107
|
|
|
$validacion = $num4 % 97; |
108
|
|
|
|
109
|
|
|
if ($validacion == $num3) { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function isValidIban($value) |
119
|
|
|
{ |
120
|
|
|
$iban = new \IBAN(); |
121
|
|
|
|
122
|
|
|
return $iban->Verify($value); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function isValidPostalCode($value) |
126
|
|
|
{ |
127
|
|
|
$regEx = '/^(?:0[1-9]|[1-4]\d|5[0-2])\d{3}$/i'; |
128
|
|
|
|
129
|
|
|
return preg_match($regEx, $value) === 1; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function isValidPhone($value) |
133
|
|
|
{ |
134
|
|
|
$regEx = '/^[9|8|6|7][0-9]{8}$/i'; |
135
|
|
|
|
136
|
|
|
return preg_match($regEx, $value) === 1; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.