Skip to content

Instantly share code, notes, and snippets.

@omarkdev
Created December 16, 2016 15:10
Show Gist options
  • Save omarkdev/35de33206ee6c67c6f218444171f1e5a to your computer and use it in GitHub Desktop.
Save omarkdev/35de33206ee6c67c6f218444171f1e5a to your computer and use it in GitHub Desktop.
<?php
Validator::extend('cpf', function($attribute, $value, $params, $validator){
$cpf = $value;
$cpf = preg_replace('/[^0-9]/', '', $cpf);
$cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);
if (strlen($cpf) != 11)
return false;
$cpfsCheck = [
'00000000000',
'11111111111',
'22222222222',
'33333333333',
'44444444444',
'55555555555',
'66666666666',
'77777777777',
'88888888888',
'99999999999'
];
if(in_array($value, $cpfsCheck))
return false;
for ($t = 9; $t < 11; $t++) {
for ($d = 0, $c = 0; $c < $t; $c++) {
$d += $cpf{$c} * (($t + 1) - $c);
}
$d = ((10 * $d) % 11) % 10;
if ($cpf{$c} != $d) {
return false;
}
}
return true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment