Skip to content

Commit 12836dd

Browse files
uesleygeidsonc
andauthored
adiciona valiação de pis pasep (#54)
* adiciona valiação de pis pasep * Atualiza dependências do github actions --------- Co-authored-by: Geidson Benício Coelho <[email protected]>
1 parent 40d0134 commit 12836dd

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- uses: actions/checkout@v2
2424

2525
- name: Cache composer dependencies
26-
uses: actions/cache@v2
26+
uses: actions/cache@v3
2727
env:
2828
cache-name: cache-composer
2929
with:
@@ -36,4 +36,4 @@ jobs:
3636
uses: php-actions/composer@v2
3737

3838
- name: Run php unit tests
39-
run: ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
39+
run: ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

src/Helpers/Validate.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,22 @@ private static function validate_cns_tmp($cns)
206206
return $resto == 0;
207207
}
208208

209+
public static function isValidPisPasep(string $value): bool
210+
{
211+
$number = preg_replace('/\D/', '', $value);
212+
213+
if (strlen($number) !== 11 || preg_match('/^(\d)\1{10}$/', $number)) {
214+
return false;
215+
}
216+
217+
$digits = str_split($number);
218+
$weights = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
219+
$sum = array_sum(array_map(fn($d, $w) => $d * $w, array_slice($digits, 0, 10), $weights));
220+
221+
$calculated_verification_digit = ($sum % 11) < 2 ? 0 : (11 - $sum % 11);
222+
223+
$given_verification_digit = (int) $digits[10];
224+
225+
return $given_verification_digit === $calculated_verification_digit;
226+
}
209227
}

tests/Helpers/ValidateTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,29 @@ public function cnsProvider()
167167
[null, false],
168168
];
169169
}
170+
171+
/**
172+
* @dataProvider pisPasepProvider
173+
* */
174+
public function testValidatePisPasep(string $value, bool $is_valid): void
175+
{
176+
$validation = Validate::isValidPisPasep($value);
177+
178+
$this->assertSame($is_valid, $validation);
179+
}
180+
181+
public static function pisPasepProvider(): array
182+
{
183+
return [
184+
['12345678900', true],
185+
['14843463732', true],
186+
['37275831654', true],
187+
['58757814249', true],
188+
['65255328642', true],
189+
['81816214189', true],
190+
['1234567890', false],
191+
['37275831655', false],
192+
['372758316555', false],
193+
];
194+
}
170195
}

0 commit comments

Comments
 (0)