mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 23:35:45 +01:00
The `{{name}}` placeholder could represent different things depending on
the state of the Result, and referring to it as `{{name}}` seems
arbitrary. This commit changes it to `{{subject}}`, which is much more
generic and it describes well what that placeholder can mean.
77 lines
1.7 KiB
PHP
77 lines
1.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use Attribute;
|
|
use Respect\Validation\Message\Template;
|
|
use Respect\Validation\Rules\Core\Simple;
|
|
|
|
use function array_map;
|
|
use function array_sum;
|
|
use function count;
|
|
use function is_scalar;
|
|
use function preg_replace;
|
|
use function str_split;
|
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
|
|
#[Template(
|
|
'{{subject}} must be a valid CNPJ number',
|
|
'{{subject}} must not be a valid CNPJ number',
|
|
)]
|
|
final class Cnpj extends Simple
|
|
{
|
|
public function isValid(mixed $input): bool
|
|
{
|
|
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) {
|
|
return false;
|
|
}
|
|
|
|
if (count($digits) !== 14) {
|
|
return false;
|
|
}
|
|
|
|
$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;
|
|
}
|
|
|
|
/** @return int[] */
|
|
private function getDigits(string $input): array
|
|
{
|
|
return array_map(
|
|
'intval',
|
|
str_split(
|
|
(string) preg_replace('/\D/', '', $input),
|
|
),
|
|
);
|
|
}
|
|
}
|