diff --git a/README.md b/README.md index bef3a61..8e5d934 100644 --- a/README.md +++ b/README.md @@ -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.
+__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: diff --git a/composer.json b/composer.json index 0ff5c3b..4a62a86 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/DgiiRncValidator.php b/src/DgiiRncValidator.php index 01977c6..cdd8f9a 100755 --- a/src/DgiiRncValidator.php +++ b/src/DgiiRncValidator.php @@ -5,6 +5,7 @@ namespace Seisigma\DgiiRncValidator; use Seisigma\DgiiRncValidator\Helpers\Status; +use Seisigma\DgiiRncValidator\Helpers\Types; use Seisigma\DgiiRncValidator\Helpers\Utils; use SoapClient; @@ -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 */ @@ -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(), ]; } } diff --git a/src/Helpers/Status.php b/src/Helpers/Status.php index b89c0b5..da91296 100644 --- a/src/Helpers/Status.php +++ b/src/Helpers/Status.php @@ -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' }; } } diff --git a/src/Helpers/Types.php b/src/Helpers/Types.php new file mode 100644 index 0000000..3be5df1 --- /dev/null +++ b/src/Helpers/Types.php @@ -0,0 +1,19 @@ + 'RNC', + self::CEDULA => 'Cédula', + self::PASSPORT => 'Pasaporte', + }; + } +} diff --git a/tests/DgiiRncValidatorTest.php b/tests/DgiiRncValidatorTest.php index 07f8d68..ca49de3 100644 --- a/tests/DgiiRncValidatorTest.php +++ b/tests/DgiiRncValidatorTest.php @@ -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))