for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Install GitHub App
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <[email protected]>
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
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 <[email protected]>
* @author Henrique Moody <[email protected]>
* @author Kinn Coelho Julião <[email protected]>
* @author William Espindola <[email protected]>
final class Cnh extends AbstractRule
{
* {@inheritdoc}
public function isValid($input): bool
if (!is_scalar($input)) {
return false;
}
// Canonicalize input
$input = preg_replace('{\D}', '', (string) $input);
// Validate length and invalid numbers
if ((11 != mb_strlen($input)) || (0 === (int) $input)) {
// 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);
if ($input[9] != (($dv1 = $s1 % 11) > 9) ? 0 : $dv1) {
if ($input[10] != (((($dv2 = ($s2 % 11) - (($dv1 > 9) ? 2 : 0)) < 0)
? $dv2 + 11 : $dv2) > 9) ? 0 : $dv2) {
return true;