1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BrenoRoosevelt\Validation\Rules\Brazilian; |
5
|
|
|
|
6
|
|
|
use BrenoRoosevelt\Validation\AbstractRule; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Throwable; |
9
|
|
|
|
10
|
|
|
class DigitoVerificador extends AbstractRule |
11
|
|
|
{ |
12
|
|
|
const MOD11 = 0; |
13
|
|
|
const MOD10 = 1; |
14
|
|
|
|
15
|
|
|
public function __construct(private int $algorithm = self::MOD11, ?string $message = 'Dígito verificador inválido') |
16
|
|
|
{ |
17
|
|
|
parent::__construct($message); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function isValid($input, array $context = []): bool |
21
|
|
|
{ |
22
|
|
|
try { |
23
|
|
|
$stringInput = (string) $input; |
24
|
|
|
} catch (Throwable $exception) { |
|
|
|
|
25
|
|
|
return false; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (preg_match('/^\d$/', $stringInput) !== 1) { |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$number = substr($stringInput, 0, -1); |
33
|
|
|
$digit = (int) substr($stringInput, -1); |
34
|
|
|
return |
35
|
|
|
$digit === match ($this->algorithm) { |
36
|
|
|
self::MOD11 => self::mod11($number), |
37
|
|
|
self::MOD10 => self::mod10($number), |
38
|
|
|
default => throw new InvalidArgumentException('Algoritmo de cálculo de dígito inválido') |
39
|
|
|
}; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function mod11($input, $modReturn = []): int |
43
|
|
|
{ |
44
|
|
|
$numbers = array_reverse(str_split((string) $input)); |
|
|
|
|
45
|
|
|
$factor = [2, 3, 4, 5, 6, 7, 8, 9]; |
46
|
|
|
$size = count($factor); |
47
|
|
|
$i = $sum = 0; |
48
|
|
|
foreach ($numbers as $number) { |
49
|
|
|
$sum += $number * $factor[$i++ % $size]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$mod11 = ($sum * 10 ) % 11; |
53
|
|
|
return $modReturn[$mod11] ?? 11 - $mod11; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function mod10($input): int |
57
|
|
|
{ |
58
|
|
|
$numbers = array_reverse(str_split((string) $input)); |
|
|
|
|
59
|
|
|
$factor = [2, 1]; |
60
|
|
|
$size = count($factor); |
61
|
|
|
$i = $sum = 0; |
62
|
|
|
foreach ($numbers as $number) { |
63
|
|
|
$num = $number * $factor[$i++ % $size]; |
64
|
|
|
$sum += array_sum(str_split((string) $num)); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return 10 - ($sum % 10); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function checkCpfCnpjDigits(string $document): bool |
71
|
|
|
{ |
72
|
|
|
$document = preg_replace('/\D/', '', $document); |
73
|
|
|
$number = substr($document, 0, -2); |
74
|
|
|
$digits = substr($document, -2); |
75
|
|
|
|
76
|
|
|
$modReturn = [0 => 0, 1 => 0]; |
77
|
|
|
$digit1 = DigitoVerificador::mod11($number, $modReturn); |
78
|
|
|
$digit2 = DigitoVerificador::mod11($number . $digit1, $modReturn); |
79
|
|
|
|
80
|
|
|
return $digits === ($digit1 . $digit2); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.