Files
rkm-conectasus-ci-sandbox/api/app/Rules/NisPisPasep.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

46 lines
883 B
PHP

<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class NisPisPasep implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$pis = str_pad(preg_replace('[^0-9]', '', $value), 11, '0', STR_PAD_LEFT);
if (strlen($pis) != 11 || (int)$pis == 0)
return false;
for ($d = 0, $p = 3, $c = 0; $c < 10; $c++) {
$d += $pis[$c] * $p;
$p = ($p < 3) ? 9 : --$p;
}
$d = ((10 * $d) % 11) % 10;
return $pis[$c] == $d;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'PIS/PASEP|NIS inválido.';
}
}