-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Uruguay ID (IC) validator (#95)
* implement Uruguay ID validator * improvements uruguay id validator Co-authored-by: Rodolpho Lima <[email protected]>
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Reducktion\Socrates\Core\SouthAmerica\Uruguay; | ||
|
||
use Reducktion\Socrates\Contracts\IdValidator; | ||
use Reducktion\Socrates\Exceptions\InvalidLengthException; | ||
|
||
/** | ||
* Class UruguayIdValidator | ||
* | ||
* Algorithm adapted from: https://github.com/DiMaNacho/validarci | ||
* | ||
* @package Reducktion\Socrates\Core\SouthAmerica\Uruguay | ||
*/ | ||
class UruguayIdValidator implements IdValidator | ||
{ | ||
public function validate(string $id): bool | ||
{ | ||
$id = $this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Reducktion\Socrates\Tests\Feature\SouthAmerica; | ||
|
||
use Reducktion\Socrates\Laravel\Facades\Socrates; | ||
use Reducktion\Socrates\Exceptions\InvalidLengthException; | ||
use Reducktion\Socrates\Exceptions\UnsupportedOperationException; | ||
use Reducktion\Socrates\Tests\Feature\FeatureTest; | ||
|
||
class UruguayTest extends FeatureTest | ||
{ | ||
private $validIds; | ||
private $invalidIds; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->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'); | ||
} | ||
} |