From aa91ca224cf1a998e0308d9870cdf3cc19a55073 Mon Sep 17 00:00:00 2001 From: MrEko Date: Wed, 23 Oct 2019 16:45:36 -0300 Subject: [PATCH 1/3] Update composer.json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Arrumando versão do php para funcionar no eCivil --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index aa515c6..f227b5d 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } ], "require": { - "php": "^7.2" + "php": "^7.1" }, "require-dev": { "phpunit/phpunit": "^8.3", From b56a93b6d8db34e16551edbb736c36fae1b78942 Mon Sep 17 00:00:00 2001 From: MrEko Date: Fri, 10 Jan 2020 15:09:08 -0300 Subject: [PATCH 2/3] =?UTF-8?q?Corrigindo=20valida=C3=A7=C3=A3o=20da=20cnh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/validator-docs/Rules/Cnh.php | 33 ++++++++++++++++++++++---------- tests/TestValidator.php | 7 +++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/validator-docs/Rules/Cnh.php b/src/validator-docs/Rules/Cnh.php index 20688cc..92181f5 100644 --- a/src/validator-docs/Rules/Cnh.php +++ b/src/validator-docs/Rules/Cnh.php @@ -10,7 +10,8 @@ final class Cnh extends Sanitization { /** - * Trecho retirado do respect validation + * @author Evandro Kondrat + * Trecho reescrito com base no algoritmo passado pelo Detran-PR */ public function validateCnh($attribute, $value): bool { @@ -24,20 +25,32 @@ public function validateCnh($attribute, $value): bool return false; } - for ($c = $s1 = $s2 = 0, $p = 9; $c < 9; $c++, $p--) { - $s1 += (int) $value[$c] * $p; - $s2 += (int) $value[$c] * (10 - $p); + $parcial = substr($value, 0, 9); + + for ($i = 0 , $j = 2, $s = 0; $i < mb_strlen($parcial); $i++, $j++) { + $s += (int) $parcial[$i] * $j; } - $dv1 = $s1 % 11; - if ($value[9] != ($dv1 > 9) ? 0 : $dv1) { - return false; + $resto = $s % 11; + if ($resto <= 1) { + $dv1 = 0; + } else { + $dv1 = 11 - $resto; } - $dv2 = $s2 % 11 - ($dv1 > 9 ? 2 : 0); + $parcial = $dv1.$parcial; - $check = $dv2 < 0 ? $dv2 + 11 : ($dv2 > 9 ? 0 : $dv2); + for ($i = 0, $j = 2, $s = 0; $i < mb_strlen($parcial); $i++, $j++) { + $s += (int) $parcial[$i] * $j; + } + + $resto = $s % 11; + if ($resto <= 1) { + $dv2 = 0; + } else { + $dv2 = 11 - $resto; + } - return $value[10] == $check; + return $dv1.$dv2 == substr($value, -2); } } \ No newline at end of file diff --git a/tests/TestValidator.php b/tests/TestValidator.php index e1d6a27..18cc973 100644 --- a/tests/TestValidator.php +++ b/tests/TestValidator.php @@ -130,6 +130,13 @@ public function cnh() $this->assertTrue($correct->passes()); $this->assertTrue($incorrect->fails()); + + $correct = \Validator::make( + ['certo' => '04463004100'], + ['certo' => 'cnh'] + ); + + $this->assertTrue($correct->passes()); } /** @test **/ From c8c5a10cc550bcc09899b755615c65c7df6cdf44 Mon Sep 17 00:00:00 2001 From: MrEko Date: Fri, 10 Jan 2020 15:12:41 -0300 Subject: [PATCH 3/3] Up versao php --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9ae1fa2..700778a 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } ], "require": { - "php": "^7.1" + "php": "^7.2" }, "require-dev": { "phpunit/phpunit": "^8.3",