Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- uses: actions/checkout@v2

- name: Cache composer dependencies
uses: actions/cache@v2
uses: actions/cache@v3
env:
cache-name: cache-composer
with:
Expand All @@ -36,4 +36,4 @@ jobs:
uses: php-actions/composer@v2

- name: Run php unit tests
run: ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
run: ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
18 changes: 18 additions & 0 deletions src/Helpers/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,22 @@ private static function validate_cns_tmp($cns)
return $resto == 0;
}

public static function isValidPisPasep(string $value): bool
{
$number = preg_replace('/\D/', '', $value);

if (strlen($number) !== 11 || preg_match('/^(\d)\1{10}$/', $number)) {
return false;
}

$digits = str_split($number);
$weights = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
$sum = array_sum(array_map(fn($d, $w) => $d * $w, array_slice($digits, 0, 10), $weights));

$calculated_verification_digit = ($sum % 11) < 2 ? 0 : (11 - $sum % 11);

$given_verification_digit = (int) $digits[10];

return $given_verification_digit === $calculated_verification_digit;
}
}
25 changes: 25 additions & 0 deletions tests/Helpers/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,29 @@ public function cnsProvider()
[null, false],
];
}

/**
* @dataProvider pisPasepProvider
* */
public function testValidatePisPasep(string $value, bool $is_valid): void
{
$validation = Validate::isValidPisPasep($value);

$this->assertSame($is_valid, $validation);
}

public static function pisPasepProvider(): array
{
return [
['12345678900', true],
['14843463732', true],
['37275831654', true],
['58757814249', true],
['65255328642', true],
['81816214189', true],
['1234567890', false],
['37275831655', false],
['372758316555', false],
];
}
}