From 9818ecf9b1c37342222826abd8ed8b173774cc84 Mon Sep 17 00:00:00 2001 From: Rodolpho Lima Date: Sat, 10 Oct 2020 20:14:44 +0100 Subject: [PATCH] Added Uruguay ID (IC) validator (#95) * implement Uruguay ID validator * improvements uruguay id validator Co-authored-by: Rodolpho Lima --- COUNTRIES.md | 1 + src/Config/Countries.php | 1 + .../Uruguay/UruguayIdValidator.php | 50 +++++++++++++ tests/Feature/SouthAmerica/UruguayTest.php | 75 +++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 src/Core/SouthAmerica/Uruguay/UruguayIdValidator.php create mode 100644 tests/Feature/SouthAmerica/UruguayTest.php diff --git a/COUNTRIES.md b/COUNTRIES.md index c5347ac..de54ab6 100644 --- a/COUNTRIES.md +++ b/COUNTRIES.md @@ -41,3 +41,4 @@ | Turkey πŸ‡ΉπŸ‡· | TR | :heavy_check_mark: | :x: | | Ukraine πŸ‡ΊπŸ‡¦ | UA | :heavy_check_mark: | :heavy_check_mark: | | United Kingdom πŸ‡¬πŸ‡§ | GB | :heavy_check_mark: | :x: | +| Uruguay πŸ‡ΊπŸ‡Ύ | UY | :heavy_check_mark: | :x: | diff --git a/src/Config/Countries.php b/src/Config/Countries.php index ceb40a4..a9ecb28 100644 --- a/src/Config/Countries.php +++ b/src/Config/Countries.php @@ -310,6 +310,7 @@ abstract class Countries 'AR' => \Reducktion\Socrates\Core\SouthAmerica\Argentina\ArgentinaIdValidator::class, 'BR' => \Reducktion\Socrates\Core\SouthAmerica\Brazil\BrazilIdValidator::class, 'CL' => \Reducktion\Socrates\Core\SouthAmerica\Chile\ChileIdValidator::class, + 'UY' => \Reducktion\Socrates\Core\SouthAmerica\Uruguay\UruguayIdValidator::class, ]; public static $extractors = [ diff --git a/src/Core/SouthAmerica/Uruguay/UruguayIdValidator.php b/src/Core/SouthAmerica/Uruguay/UruguayIdValidator.php new file mode 100644 index 0000000..d5758c8 --- /dev/null +++ b/src/Core/SouthAmerica/Uruguay/UruguayIdValidator.php @@ -0,0 +1,50 @@ +sanitize($id); + $lastDigit = (int)$id[-1]; + $id = str_pad($id, 7, '0', STR_PAD_LEFT); + + $a = 0; + + $baseNumber = "2987634"; + for ($i = 0; $i < 7; $i++) { + $baseDigit = (int)$baseNumber[$i]; + $ciDigit = (int)$id[$i]; + + $a += ($baseDigit * $ciDigit) % 10; + } + + $validationDigit = $a % 10 == 0 ? 0 : 10 - $a % 10; + + return $lastDigit == $validationDigit; + } + + private function sanitize(string $id): string + { + $id = preg_replace('/[^0-9]/', '', $id); + + $idLength = strlen($id); + + if ($idLength != 8) { + throw new InvalidLengthException('Uruguay CI', '8', $idLength); + } + + return $id; + } +} diff --git a/tests/Feature/SouthAmerica/UruguayTest.php b/tests/Feature/SouthAmerica/UruguayTest.php new file mode 100644 index 0000000..1d58055 --- /dev/null +++ b/tests/Feature/SouthAmerica/UruguayTest.php @@ -0,0 +1,75 @@ +validIds = [ + '9.814.898-7', + '99609297', + '7.019.519.6', + '2.797.689 6', + '24895354', + '40008026', + '60125711', + '11111111', + '22222222', + '52574582', + '96072455', + '70505244', + ]; + + $this->invalidIds = [ + '46057422', + '90224632', + '52631437', + '62634608', + '23801966', + '27452675', + '32311266', + '94448560', + '59672227', + '28441574', + '11111112', + ]; + } + + public function test_extract_behaviour(): void + { + $this->expectException(UnsupportedOperationException::class); + + Socrates::getCitizenDataFromId('', 'UY'); + } + + public function test_validation_behaviour(): void + { + foreach ($this->validIds as $id) { + $this->assertTrue( + Socrates::validateId($id, 'UY'), + $id + ); + } + + foreach ($this->invalidIds as $id) { + $this->assertFalse( + Socrates::validateId($id, 'UY') + ); + } + + $this->expectException(InvalidLengthException::class); + + Socrates::validateId('1234567', 'UY'); + } +}