2011-10-18 20:25:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2013-01-14 22:55:03 +01:00
|
|
|
class Cnpj extends AbstractRule
|
2011-10-18 20:25:47 +02:00
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
|
|
|
|
public function validate($input)
|
2011-10-18 20:25:47 +02:00
|
|
|
{
|
2012-04-08 02:37:37 +02:00
|
|
|
//Code ported from jsfromhell.com
|
2011-10-20 18:41:36 +02:00
|
|
|
$c = preg_replace('/\D/', '', $input);
|
2011-10-20 00:57:25 +02:00
|
|
|
$b = array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);
|
2013-01-14 22:55:03 +01:00
|
|
|
|
|
|
|
if (strlen($c) != 14) {
|
2012-04-08 02:37:37 +02:00
|
|
|
return false;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2011-10-20 00:57:25 +02:00
|
|
|
for ($i = 0, $n = 0; $i < 12; $n += $c[$i] * $b[++$i]);
|
2013-01-14 22:55:03 +01:00
|
|
|
|
|
|
|
if ($c[12] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
|
2012-04-08 02:37:37 +02:00
|
|
|
return false;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2011-10-20 00:57:25 +02:00
|
|
|
for ($i = 0, $n = 0; $i <= 12; $n += $c[$i] * $b[$i++]);
|
2013-01-14 22:55:03 +01:00
|
|
|
|
|
|
|
if ($c[13] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
|
2012-04-08 02:37:37 +02:00
|
|
|
return false;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2011-10-20 00:57:25 +02:00
|
|
|
return true;
|
2011-10-18 20:25:47 +02:00
|
|
|
}
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2011-10-18 20:25:47 +02:00
|
|
|
}
|