respect-validation/library/Rules/Cnpj.php

88 lines
2 KiB
PHP
Raw Normal View History

2011-10-18 20:25:47 +02:00
<?php
2015-06-08 16:47:14 +02:00
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
declare(strict_types=1);
2011-10-18 20:25:47 +02:00
namespace Respect\Validation\Rules;
use function array_map;
use function array_sum;
use function count;
use function is_scalar;
use function preg_replace;
use function str_split;
/**
* Validates if the input is a Brazilian National Registry of Legal Entities (CNPJ) number.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jayson Reis <santosdosreis@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
* @author Renato Moura <renato@naturalweb.com.br>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class Cnpj extends AbstractRule
2011-10-18 20:25:47 +02:00
{
/**
* {@inheritDoc}
*/
public function validate($input): bool
2011-10-18 20:25:47 +02:00
{
2016-06-22 16:50:34 +02:00
if (!is_scalar($input)) {
return false;
}
// Code ported from jsfromhell.com
$bases = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
$digits = $this->getDigits((string) $input);
if (array_sum($digits) < 1) {
2016-06-22 16:50:34 +02:00
return false;
}
if (count($digits) !== 14) {
return false;
}
2013-01-22 20:25:34 +01:00
$n = 0;
for ($i = 0; $i < 12; ++$i) {
$n += $digits[$i] * $bases[$i + 1];
}
if ($digits[12] != (($n %= 11) < 2 ? 0 : 11 - $n)) {
return false;
}
$n = 0;
for ($i = 0; $i <= 12; ++$i) {
$n += $digits[$i] * $bases[$i];
}
$check = ($n %= 11) < 2 ? 0 : 11 - $n;
return $digits[13] == $check;
2011-10-18 20:25:47 +02:00
}
/**
* @return int[]
*/
private function getDigits(string $input): array
{
return array_map(
'intval',
str_split(
(string) preg_replace('/\D/', '', $input)
)
);
}
2011-10-18 20:25:47 +02:00
}