Skip to content

Commit

Permalink
New Feature: RNC Type
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardov03 authored Feb 7, 2023
2 parents 9a27741 + 4ca7e60 commit 9b9d7c5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,25 @@ $validatedRnc = DgiiRncValidator::validateRNC("132620951");
var_dump($validatedRnc); // bool(true)

// 123456789 is an invalid RNC
$validatedRnc = DgiiRncValidator::check("123456789");
$validatedRnc = DgiiRncValidator::validateRNC("123456789");
var_dump($validatedRnc); // bool(false)
```

### rncType
Validate if a given string is a valid RNC.<br>
__How to use it:__
```php
require Seisigma\DgiiRncValidator\DgiiRncValidator;
...
// 132620951 is a valid RNC
$rncType = DgiiRncValidator::rncType("132620951");
var_dump($rncType); // string(RNC)

// 123456789 is an invalid RNC
$rncType = DgiiRncValidator::rncType("04800009577");
var_dump($rncType); // string(Cédula)
```

## Helper Functions
Just in case you need a few extra tools, here's a list of utility functions:

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
}
],
"require": {
"php": "8.*",
"php": ">=8.1",
"ext-soap": "*"
},
"require-dev": {
"pestphp/pest": "^1.20",
"laravel/pint": "^1.2",
"spatie/ray": "^1.28"
"pestphp/pest": "^1.22.4",
"laravel/pint": "^1.4.1",
"spatie/ray": "^1.36"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 11 additions & 1 deletion src/DgiiRncValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Seisigma\DgiiRncValidator;

use Seisigma\DgiiRncValidator\Helpers\Status;
use Seisigma\DgiiRncValidator\Helpers\Types;
use Seisigma\DgiiRncValidator\Helpers\Utils;
use SoapClient;

Expand All @@ -20,6 +21,15 @@ public static function validateRNC(string $string): bool
return (bool) count($matches);
}

public static function rncType(string $string): bool|string
{
if (self::validateRNC($string)) {
return (strlen($string) === 9) ? Types::RNC->toString() : Types::CEDULA->toString();
}

return false;
}

/**
* @throws \Exception
*/
Expand Down Expand Up @@ -55,7 +65,7 @@ public static function check(string $id): array|bool
'rnc' => $id,
'name' => $name,
'commercial_name' => $commercialName,
'status' => Status::from((int)$status)->toString(),
'status' => Status::from((int) $status)->toString(),
];
}
}
4 changes: 2 additions & 2 deletions src/Helpers/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ enum Status: int
public function toString(): string
{
return match ($this) {
Status::ACTIVE => 'Active',
Status::INACTIVE => 'Inactive'
self::ACTIVE => 'Active',
self::INACTIVE => 'Inactive'
};
}
}
19 changes: 19 additions & 0 deletions src/Helpers/Types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Seisigma\DgiiRncValidator\Helpers;

enum Types: int
{
case RNC = 1;
case CEDULA = 2;
case PASSPORT = 3;

public function toString(): string
{
return match ($this) {
self::RNC => 'RNC',
self::CEDULA => 'Cédula',
self::PASSPORT => 'Pasaporte',
};
}
}
5 changes: 5 additions & 0 deletions tests/DgiiRncValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
->and(DgiiRncValidator::validateRNC('12345678901'))->toBeTrue();
});

test('check rncType return the type name', function () {
expect(DgiiRncValidator::rncType('123456789'))->toBe('RNC')
->and(DgiiRncValidator::rncType('12345678901'))->toBe('Cédula');
});

it('can return the details of an RNC if true', function () {
$id = '132620951';
expect(DgiiRncValidator::check($id))
Expand Down

0 comments on commit 9b9d7c5

Please sign in to comment.