Skip to content

Commit a6c303a

Browse files
committed
Merge branch 'dev' into main
2 parents 536fd5b + 411ffaf commit a6c303a

File tree

9 files changed

+311
-1
lines changed

9 files changed

+311
-1
lines changed

.github/workflows/run-tests.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: run-tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
fail-fast: true
10+
matrix:
11+
os: [ubuntu-latest]
12+
php: [8.0, 7.3]
13+
laravel: [8.*]
14+
dependency-version: [prefer-lowest, prefer-stable]
15+
include:
16+
- laravel: 8.*
17+
testbench: 6.*
18+
19+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v2
24+
25+
- name: Setup PHP
26+
uses: shivammathur/setup-php@v2
27+
with:
28+
php-version: ${{ matrix.php }}
29+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
30+
coverage: none
31+
32+
- name: Setup problem matchers
33+
run: |
34+
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
35+
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
36+
37+
- name: Install dependencies
38+
run: |
39+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
40+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
41+
42+
- name: Execute tests
43+
run: vendor/bin/phpunit

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ composer.lock
33
docs
44
vendor
55
coverage
6-
.php_cs.cache
6+
.php_cs.cache
7+
.phpunit.result.cache

composer.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "wilsenhc/laravel-rif-validation",
3+
"description": "Regla de Validación para conocer si un RIF (Registro Único de Información Fiscal) está bien formado",
4+
"keywords": [
5+
"wilsenhc",
6+
"seniat",
7+
"rif",
8+
"laravel-rif-validation",
9+
"laravel-validar-rif"
10+
],
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Wilsen Hernández",
15+
"email": "[email protected]",
16+
"role": "Developer"
17+
}
18+
],
19+
"require": {
20+
"php": "^7.3|^8.0",
21+
"illuminate/support": "^8.0",
22+
"illuminate/contracts": "^8.0"
23+
},
24+
"require-dev": {
25+
"orchestra/testbench": "^6.0",
26+
"phpunit/phpunit": "^9.4"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"Wilsenhc\\RifValidation\\": "src"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Wilsenhc\\RifValidation\\Tests\\": "tests"
36+
}
37+
},
38+
"scripts": {
39+
"test": "vendor/bin/phpunit"
40+
},
41+
"extra": {
42+
"laravel" : {
43+
"providers" : [
44+
"Wilsenhc\\RifValidation\\RifValidationServiceProvider"
45+
]
46+
}
47+
}
48+
}

phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" cacheResult="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Wilsenhc Test Suite">
10+
<directory>tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

resources/lang/en/messages.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'rif' => 'RIF is invalid.',
5+
];

resources/lang/es/messages.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'rif' => 'El RIF es inválido.',
5+
];

src/RifValidationServiceProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Wilsenhc\RifValidation;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class RifValidationServiceProvider extends ServiceProvider
8+
{
9+
public function boot()
10+
{
11+
$this->publishes([
12+
__DIR__.'/../resources/lang' => resource_path('lang/vendor/validateRif'),
13+
]);
14+
15+
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'validateRif');
16+
}
17+
}

src/Rules/Rif.php

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

tests/Rules/RifTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Wilsenhc\RifValidation\Tests\Rules;
4+
5+
use Orchestra\Testbench\TestCase;
6+
7+
use Wilsenhc\RifValidation\Rules\Rif;
8+
use Wilsenhc\RifValidation\RifValidationServiceProvider;
9+
10+
class RifTest extends TestCase
11+
{
12+
/**
13+
* @var \Wilsenhc\RifValidation\Rules\Rif $rule
14+
*/
15+
protected $rule;
16+
17+
/**
18+
* Setup the test environment.
19+
*/
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
$this->rule = new Rif();
25+
}
26+
27+
protected function getPackageProviders($app)
28+
{
29+
return [
30+
RifValidationServiceProvider::class,
31+
];
32+
}
33+
34+
/** @test */
35+
public function it_will_return_true_if_rif_is_valid()
36+
{
37+
// Valid RIF of "Universidad de Carabobo"
38+
$rif = 'G-20000041-4';
39+
40+
$this->assertTrue($this->rule->passes('rif', $rif));
41+
42+
// Valid RIF of "Banesco Banco Universal"
43+
$rif = 'J-07013380-5';
44+
45+
$this->assertTrue($this->rule->passes('rif', $rif));
46+
47+
// Valid RIF of "Nicolas Maduro Moros"
48+
$rif = 'V-05892464-0';
49+
50+
$this->assertTrue($this->rule->passes('rif', $rif));
51+
}
52+
53+
/** @test */
54+
public function it_will_return_false_if_format_is_invalid()
55+
{
56+
// Starts with a RIF Type that doesn't exist
57+
$rif = 'Q-00000000-0';
58+
59+
$this->assertFalse($this->rule->passes('rif', $rif));
60+
61+
// Extra number
62+
$rif = 'G-200000041-4';
63+
64+
$this->assertFalse($this->rule->passes('rif', $rif));
65+
66+
// Missing numbers
67+
$rif = 'V-5892464';
68+
69+
$this->assertFalse($this->rule->passes('rif', $rif));
70+
71+
// Letter where there should be only numbers
72+
$rif = 'G-200F00041-F';
73+
74+
$this->assertFalse($this->rule->passes('rif', $rif));
75+
}
76+
}

0 commit comments

Comments
 (0)