-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.CPFValidator.php
61 lines (58 loc) · 1.27 KB
/
class.CPFValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* CPF Validator
* Classe usada para validação de CPF.
* MIT License. Copyright (c) 2018 Paulo Rodriguez
*
* @author Paulo Rodrigues (xlaming)
* @link https://paulao.me/cpf/
* @version 1.0 stable
*/
class CPFValidator
{
/**
* Invalid CPFs
* @var array
*/
protected $invalidCPF = [00000000000, 11111111111, 22222222222, 33333333333, 44444444444, 55555555555, 66666666666, 77777777777, 88888888888, 99999999999];
/**
* Validating the CPF
* @param string $cpf
* @return bool
*/
public function validate($cpf)
{
if (empty($cpf))
{
return false;
}
/* remove everything except digits */
$cpf = preg_replace('/\D/', '', $cpf);
$cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);
if (strlen($cpf) != 11) // check if it is 11 digits
{
return false;
}
else if (in_array($cpf, $this->invalidCPF)) // check if it is in the list of invalid CPFs
{
return false;
}
else
{
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) // check if the sum is invalid
{
return false;
}
}
return true; // seems that everythings ok
}
}
}
?>