Skip to content

Commit

Permalink
Added Uruguay ID (IC) validator (#95)
Browse files Browse the repository at this point in the history
* implement Uruguay ID validator

* improvements uruguay id validator

Co-authored-by: Rodolpho Lima <[email protected]>
  • Loading branch information
gheleri and Rodolpho Lima authored Oct 10, 2020
1 parent 4d3156e commit 9818ecf
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions COUNTRIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
1 change: 1 addition & 0 deletions src/Config/Countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
50 changes: 50 additions & 0 deletions src/Core/SouthAmerica/Uruguay/UruguayIdValidator.php
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;
}
}
75 changes: 75 additions & 0 deletions tests/Feature/SouthAmerica/UruguayTest.php
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');
}
}

0 comments on commit 9818ecf

Please sign in to comment.