Skip to content

Commit

Permalink
Merge pull request #26 from elminson/elminson/fixing_empty_string
Browse files Browse the repository at this point in the history
Add validation for empty or non-numeric RNC input string
  • Loading branch information
ricardov03 authored May 16, 2024
2 parents 86bad0a + e9bfdf1 commit 144c12b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/DgiiRncValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class DgiiRncValidator
public static function validateRNC(string $string): bool
{
$cleanedId = Utils::getNumbers($string);

if (!$cleanedId) {
return false;
}

preg_match('/^(\d{9}|\d{11})$/', $cleanedId, $matches);

return (bool) count($matches);
Expand Down
9 changes: 9 additions & 0 deletions tests/DgiiRncValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@
->and(DgiiRncValidator::check('123456789'))
->toBeFalse();
});

test('check if the given string without numbers is a valid RNC', function () {
$id = 'abc cdd';
expect(DgiiRncValidator::validateRNC($id))->toBeFalse();
$id = ' ';
expect(DgiiRncValidator::validateRNC($id))->toBeFalse();
$id = '';
expect(DgiiRncValidator::validateRNC($id))->toBeFalse();
});

0 comments on commit 144c12b

Please sign in to comment.