respect-validation/library/Respect/Validation/Rules/Cnpj.php

34 lines
703 B
PHP
Raw Normal View History

2011-10-18 20:25:47 +02:00
<?php
namespace Respect\Validation\Rules;
class Cnpj extends AbstractRule
2011-10-18 20:25:47 +02:00
{
public function validate($input)
2011-10-18 20:25:47 +02:00
{
//Code ported from jsfromhell.com
$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);
if (strlen($c) != 14) {
return false;
}
2011-10-20 00:57:25 +02:00
for ($i = 0, $n = 0; $i < 12; $n += $c[$i] * $b[++$i]);
if ($c[12] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}
2011-10-20 00:57:25 +02:00
for ($i = 0, $n = 0; $i <= 12; $n += $c[$i] * $b[$i++]);
if ($c[13] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}
2011-10-20 00:57:25 +02:00
return true;
2011-10-18 20:25:47 +02:00
}
2011-10-18 20:25:47 +02:00
}