From 77114ede1209a4a3f8797df441013d6547421d1d Mon Sep 17 00:00:00 2001 From: uesleymelo Date: Tue, 25 Mar 2025 14:17:29 -0300 Subject: [PATCH 1/2] =?UTF-8?q?adiciona=20valia=C3=A7=C3=A3o=20de=20pis=20?= =?UTF-8?q?pasep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Helpers/Validate.php | 18 ++++++++++++++++++ tests/Helpers/ValidateTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+) 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], + ]; + } } From 058b3e97a765b7c2f6b30eadf7cbec922fd0657f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geidson=20Ben=C3=ADcio=20Coelho?= Date: Wed, 26 Mar 2025 16:43:52 -0300 Subject: [PATCH 2/2] Update php.yml --- .github/workflows/php.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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