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