2011-09-04 00:20:02 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2011-09-20 03:13:10 +02:00
|
|
|
class Cpf extends AbstractRule
|
2011-09-06 04:34:00 +02:00
|
|
|
{
|
|
|
|
public function validate($input)
|
|
|
|
{
|
2011-10-20 16:52:07 +02:00
|
|
|
// Ported from JSFromHell
|
|
|
|
$c = preg_replace('/\D/', '', $input);
|
|
|
|
if (strlen($c) != 11) return false;
|
|
|
|
if (preg_match("/^{$c[0]}{11}$/", $c)) return false;
|
|
|
|
for ($s = 10, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
|
|
|
|
if ($c[9] != ((($n %= 11) < 2) ? 0 : 11 - $n)) return false;
|
|
|
|
for ($s = 11, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
|
|
|
|
if ($c[10] != ((($n %= 11) < 2) ? 0 : 11 - $n)) return false;
|
|
|
|
return true;
|
2011-09-04 00:20:02 +02:00
|
|
|
}
|
2011-09-23 00:20:24 +02:00
|
|
|
}
|