|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Respect/Validation. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Validates Spain's fiscal identification number (NIF). |
|
18
|
|
|
* |
|
19
|
|
|
* @author Julián Gutiérrez <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @see https://es.wikipedia.org/wiki/N%C3%BAmero_de_identificaci%C3%B3n_fiscal |
|
22
|
|
|
*/ |
|
23
|
|
|
final class Nif extends AbstractRule |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
50 |
|
public function isValid($input): bool |
|
29
|
|
|
{ |
|
30
|
50 |
|
if (!is_string($input)) { |
|
31
|
7 |
|
return false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
43 |
|
if (preg_match('/^(\d{8})([A-Z])$/', $input, $matches)) { |
|
35
|
14 |
|
return $this->validateDni($matches[1], $matches[2]); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
29 |
|
if (preg_match('/^([KLMXYZ])(\d{7})([A-Z])$/', $input, $matches)) { |
|
39
|
10 |
|
return $this->validateNie($matches[1], $matches[2], $matches[3]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
19 |
|
if (preg_match('/^([A-HJNP-SUVW])(\d{7})([0-9A-Z])$/', $input, $matches)) { |
|
43
|
13 |
|
return $this->validateCif($matches[2], $matches[3]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
6 |
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param int $number |
|
51
|
|
|
* @param int $control |
|
52
|
|
|
*/ |
|
53
|
24 |
|
private function validateDni($number, $control) |
|
54
|
|
|
{ |
|
55
|
24 |
|
return mb_substr('TRWAGMYFPDXBNJZSQVHLCKE', ($number % 23), 1) === $control; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $prefix |
|
60
|
|
|
* @param int $number |
|
61
|
|
|
* @param int $control |
|
62
|
|
|
*/ |
|
63
|
10 |
|
private function validateNie($prefix, $number, $control) |
|
64
|
|
|
{ |
|
65
|
10 |
|
if ('Y' === $prefix) { |
|
66
|
7 |
|
return $this->validateDni('1'.$number, $control); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
if ('Z' === $prefix) { |
|
70
|
1 |
|
return $this->validateDni('2'.$number, $control); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
return $this->validateDni($number, $control); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param int $number |
|
78
|
|
|
* @param string $control |
|
79
|
|
|
*/ |
|
80
|
13 |
|
private function validateCif(string $number, $control) |
|
81
|
|
|
{ |
|
82
|
13 |
|
$code = 0; |
|
83
|
13 |
|
$position = 1; |
|
84
|
13 |
|
foreach (str_split($number) as $digit) { |
|
85
|
13 |
|
$increaser = $digit; |
|
86
|
13 |
|
if (0 !== $position % 2) { |
|
87
|
13 |
|
$increaser = array_sum(str_split((string) ($digit * 2))); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
13 |
|
$code += $increaser; |
|
91
|
13 |
|
++$position; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
13 |
|
$digits = str_split((string) $code); |
|
95
|
13 |
|
$lastDigit = (int) array_pop($digits); |
|
96
|
13 |
|
$key = 0 === $lastDigit ? 0 : (10 - $lastDigit); |
|
97
|
|
|
|
|
98
|
13 |
|
if (is_numeric($control)) { |
|
99
|
4 |
|
return (int) $key === (int) $control; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
9 |
|
return mb_substr('JABCDEFGHI', ($key % 10), 1) === $control; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|