Skip to content

Commit 94a5066

Browse files
committed
feat: upgrade to support laravel 11 and 12
1 parent de6ddda commit 94a5066

File tree

3 files changed

+27
-107
lines changed

3 files changed

+27
-107
lines changed

.github/workflows/run-tests.yml

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,14 @@ jobs:
99
fail-fast: true
1010
matrix:
1111
os: [ubuntu-latest]
12-
php: [8.0, 8.1, 8.2]
13-
laravel: [8.*, 9.*, 10.*]
12+
php: [8.2, 8.3, 8.4]
13+
laravel: [11.*, 12.*]
1414
dependency-version: [prefer-lowest, prefer-stable]
1515
include:
16-
- laravel: 8.*
17-
testbench: 6.*
18-
- laravel: 9.*
19-
testbench: 7.*
20-
- laravel: 10.*
21-
testbench: 8.*
22-
exclude:
23-
- php: 8.0
24-
laravel: 10.*
25-
- php: 8.1
26-
laravel: 6.*
27-
- php: 8.1
28-
laravel: 7.*
29-
- php: 8.1
30-
laravel: 8.*
31-
dependency-version: prefer-lowest
32-
- php: 8.2
33-
laravel: 9.*
34-
dependency-version: prefer-lowest
35-
- php: 8.2
36-
laravel: 8.*
16+
- laravel: 11.*
17+
testbench: 9.*
18+
- laravel: 12.*
19+
testbench: 10.*
3720

3821
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
3922

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
}
1818
],
1919
"require": {
20-
"php": "^8.0",
21-
"illuminate/support": "^8.0|^9.0|^10.0",
22-
"illuminate/contracts": "^8.0|^9.0|^10.0"
20+
"php": "^8.2",
21+
"illuminate/support": "^11.0|^12.0",
22+
"illuminate/contracts": "^11.0|^12.0",
23+
"wilsenhc/rif-validation": "dev-main"
2324
},
2425
"require-dev": {
25-
"orchestra/testbench": "^6.0|^7.0|^8.0",
26-
"phpunit/phpunit": "^9.4"
26+
"orchestra/testbench": "^9.0|^10.0",
27+
"phpunit/phpunit": "^11.0|^12.0"
2728
},
2829
"autoload": {
2930
"psr-4": {

src/Rules/Rif.php

Lines changed: 15 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,104 +2,40 @@
22

33
namespace Wilsenhc\RifValidation\Rules;
44

5-
use Illuminate\Contracts\Validation\Rule;
5+
use Closure;
6+
use Illuminate\Contracts\Validation\ValidationRule;
67
use Illuminate\Support\Str;
8+
use Wilsenhc\RifValidation\RifValidator;
79

8-
class Rif implements Rule
10+
class Rif implements ValidationRule
911
{
1012
/** @var string */
1113
protected $attribute;
1214

13-
/** @var array */
14-
protected $nationality_array;
15-
16-
/** @var array */
17-
protected $multipliers;
15+
/** @var \Wilsenhc\RifValidation\RifValidator */
16+
protected RifValidator $validator;
1817

1918
public function __construct()
2019
{
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();
3121
}
3222

3323
/**
34-
* Determine if the RIF validation rule passes.
24+
* Run the validation rule.
3525
*
3626
* @param string $attribute
3727
* @param mixed $value
38-
* @return bool
28+
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
29+
* @return void
3930
*/
40-
public function passes($attribute, $value): bool
31+
public function validate(string $attribute, mixed $value, Closure $fail): void
4132
{
4233
$this->attribute = $attribute;
4334

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;
5037
}
5138

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')));
10440
}
105-
}
41+
}

0 commit comments

Comments
 (0)