diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 8030697..d27cbed 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -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: @@ -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 \ No newline at end of file + run: ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover diff --git a/src/Helpers/Validate.php b/src/Helpers/Validate.php index 87e812f..a9593dd 100644 --- a/src/Helpers/Validate.php +++ b/src/Helpers/Validate.php @@ -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; + } } diff --git a/tests/Helpers/ValidateTest.php b/tests/Helpers/ValidateTest.php index 593a387..310b5eb 100644 --- a/tests/Helpers/ValidateTest.php +++ b/tests/Helpers/ValidateTest.php @@ -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], + ]; + } }