respect-validation/library/Rules/Cnh.php
Alexandre Gomes Gaigalas ab3732f91f Use SPDX IDs for licensing
SPDX IDs are shorter than licensing notes previously used, and
adhere better to FOSS standards. It is also machine-readable.
2023-02-19 00:19:10 -03:00

60 lines
1.4 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use function is_scalar;
use function mb_strlen;
use function preg_replace;
/**
* Validates a Brazilian driver's license.
*
* @author Gabriel Pedro <gpedro@users.noreply.github.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Kinn Coelho Julião <kinncj@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class Cnh extends AbstractRule
{
/**
* {@inheritDoc}
*/
public function validate($input): bool
{
if (!is_scalar($input)) {
return false;
}
// Canonicalize input
$input = (string) preg_replace('{\D}', '', (string) $input);
// Validate length and invalid numbers
if (mb_strlen($input) != 11 || ((int) $input === 0)) {
return false;
}
// Validate check digits using a modulus 11 algorithm
for ($c = $s1 = $s2 = 0, $p = 9; $c < 9; $c++, $p--) {
$s1 += (int) $input[$c] * $p;
$s2 += (int) $input[$c] * (10 - $p);
}
$dv1 = $s1 % 11;
if ($input[9] != ($dv1 > 9) ? 0 : $dv1) {
return false;
}
$dv2 = $s2 % 11 - ($dv1 > 9 ? 2 : 0);
$check = $dv2 < 0 ? $dv2 + 11 : ($dv2 > 9 ? 0 : $dv2);
return $input[10] == $check;
}
}