2
2
3
3
namespace Wilsenhc \RifValidation \Rules ;
4
4
5
- use Illuminate \Contracts \Validation \Rule ;
5
+ use Closure ;
6
+ use Illuminate \Contracts \Validation \ValidationRule ;
6
7
use Illuminate \Support \Str ;
8
+ use Wilsenhc \RifValidation \RifValidator ;
7
9
8
- class Rif implements Rule
10
+ class Rif implements ValidationRule
9
11
{
10
12
/** @var string */
11
13
protected $ attribute ;
12
14
13
- /** @var array */
14
- protected $ nationality_array ;
15
-
16
- /** @var array */
17
- protected $ multipliers ;
15
+ /** @var \Wilsenhc\RifValidation\RifValidator */
16
+ protected RifValidator $ validator ;
18
17
19
18
public function __construct ()
20
19
{
21
- $ this ->nationality_array = [
22
- 'V ' => '1 ' ,
23
- 'E ' => '2 ' ,
24
- 'J ' => '3 ' ,
25
- 'P ' => '4 ' ,
26
- 'G ' => '5 ' ,
27
- 'C ' => '3 ' ,
28
- ];
29
-
30
- $ this ->multipliers = [4 , 3 , 2 , 7 ,6 , 5 , 4 , 3 , 2 ];
20
+ $ this ->validator = new RifValidator ();
31
21
}
32
22
33
23
/**
34
- * Determine if the RIF validation rule passes .
24
+ * Run the validation rule.
35
25
*
36
26
* @param string $attribute
37
27
* @param mixed $value
38
- * @return bool
28
+ * @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
29
+ * @return void
39
30
*/
40
- public function passes ( $ attribute , $ value ): bool
31
+ public function validate ( string $ attribute , mixed $ value, Closure $ fail ): void
41
32
{
42
33
$ this ->attribute = $ attribute ;
43
34
44
- $ value = Str::upper ($ value );
45
-
46
- // Verificar si el RIF tiene el formato valido
47
- if (!preg_match ('/^[VEPJGC]-?[\d]{8}-?[\d]$/i ' , $ value ))
48
- {
49
- return false ;
35
+ if ($ this ->validator ->isValid ($ value )) {
36
+ return ;
50
37
}
51
38
52
- $ full_rif = strtoupper ($ value );
53
- $ full_rif = str_replace ('- ' , '' , $ value );
54
-
55
- $ contributor = substr ($ full_rif , 0 , -1 );
56
- $ validationNumber = substr ($ full_rif , -1 , 1 );
57
-
58
- return $ this ->validationNumber ($ contributor ) == $ validationNumber ;
59
- }
60
-
61
- /**
62
- * Get the validation error message.
63
- *
64
- * @return string
65
- */
66
- public function message (): string
67
- {
68
- return __ ('validateRif::messages.rif ' , [
69
- 'attribute ' => $ this ->attribute ,
70
- ]);
71
- }
72
-
73
- /**
74
- * Calcate the Validation Number
75
- *
76
- * @param string $rif
77
- * @return string
78
- */
79
- private function validationNumber ($ rif ): string
80
- {
81
- // FIRST STEP:
82
- // Replace the letter for its numeric value.
83
- $ rif [0 ] = $ this ->nationality_array [$ rif [0 ]];
84
-
85
- $ split_rif = str_split ($ rif );
86
-
87
- // SECOND STEP
88
- // Multiply each value by its *constant* multiplier from the multipiers
89
- // array.
90
- $ sum = 0 ;
91
- foreach ($ split_rif as $ index => $ value )
92
- {
93
- $ sum += intval ($ value ) * $ this ->multipliers [$ index ];
94
- }
95
-
96
- // THIRD STEP
97
- $ remainder = intval ($ sum % 11 );
98
-
99
- // FOURTH STEP
100
- $ difference = 11 - $ remainder ;
101
-
102
- // FINAL STEP
103
- return ($ difference > 9 ) ? '0 ' : strval ($ difference );
39
+ $ fail (Str::replace (':attribute ' , $ this ->attribute , __ ('validateRif::messages.rif ' )));
104
40
}
105
- }
41
+ }
0 commit comments