46 lines
883 B
PHP
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.';
|
|
}
|
|
}
|