Skip to content

Commit c8c0ce9

Browse files
committed
solved: Complexity, update: tests
1 parent b3ece73 commit c8c0ce9

File tree

11 files changed

+449
-215
lines changed

11 files changed

+449
-215
lines changed

CHANGELOG.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<!-- changelog-linker -->
2+
3+
<!-- dumped content start -->
4+
5+
## Unreleased
6+
7+
- [#48] added support for changelogs, Thanks to [@geekcom]
8+
- [#45] fixing "Unparenthesized is deprecated" error, Thanks to [@kidh0]
9+
- [#17] fix: erro em phpunit.xml, melhorias nos testes, namespace para testes, Thanks to [@geekcom]
10+
- [#25] Melhorias de leitura nos códigos, Thanks to [@geekcom]
11+
- [#2] Alteração da função validateCnpj para impedir que o Cnpj 00.000.000/0…, Thanks to [@andrergcosta]
12+
- [#3] Criada as funções validateFormatoCpfCnpj e validateCpfCnpj, Thanks to [@andrergcosta]
13+
- [#4] Inclusão do exemplo para validação de CPF/CNPJ no arquivo readme, Thanks to [@andrergcosta]
14+
- [#5] Validar título eleitor, Thanks to [@andrergcosta]
15+
- [#8] Faltando parâmetro $attributes em ValidatorProvider., Thanks to [@setefocos]
16+
- [#11] nova badge e melhoria no readme.me, Thanks to [@geekcom]
17+
- [#12] scrutinizer CI, Thanks to [@geekcom]
18+
- [#19] Criação da função para verificar PIS/PASEP/NIS/NIT, Thanks to [@lordantonelli]
19+
- [#20] Master from develop, Thanks to [@geekcom]
20+
- [#21] Merge pull request [#20] from geekcom/master, Thanks to [@geekcom]
21+
- [#23] update docs, minor corrections, Thanks to [@geekcom]
22+
- [#24] update docs, Thanks to [@geekcom]
23+
- [#30] update scrutinizer CI check, Thanks to [@geekcom]
24+
- [#27] atualizacao para Laravel 6, Thanks to [@geekcom]
25+
- [#47] Merge pull request [#46] from geekcom/master, Thanks to [@geekcom]
26+
- [#31] Atualização dos requisitos e testes, Thanks to [@victorhsanjos]
27+
- [#32] master into develop, Thanks to [@geekcom]
28+
- [#33] Merge pull request [#32] from geekcom/master, Thanks to [@geekcom]
29+
- [#35] auto-discover composer, Thanks to [@geekcom]
30+
- [#37] Adicionando validação do Cartão Nacional de Saúde, Thanks to [@MrEko]
31+
- [#38] Merge, Thanks to [@geekcom]
32+
- [#39] Merge pull request [#38] from geekcom/master, Thanks to [@geekcom]
33+
- [#40] melhorias de documentacao e sintaxe, v3.0.0, Thanks to [@geekcom]
34+
- [#41] Validação de Certidão de Nascimento, Casamento ou Óbito, Thanks to [@MrEko]
35+
- [#42] merge master into develop, Thanks to [@geekcom]
36+
- [#43] Merge pull request [#42] from geekcom/master, Thanks to [@geekcom]
37+
- [#44] melhoria na validacao de CNH,strict_types PHP ativado, Thanks to [@geekcom]
38+
- [#46] merge master inte develop, Thanks to [@geekcom]
39+
- [#1] Update README.md, Thanks to [@yvescabral]
40+
41+
<!-- dumped content end -->
42+
43+
[#48]: https://github.com/geekcom/validator-docs/pull/48
44+
[#47]: https://github.com/geekcom/validator-docs/pull/47
45+
[#46]: https://github.com/geekcom/validator-docs/pull/46
46+
[#45]: https://github.com/geekcom/validator-docs/pull/45
47+
[#44]: https://github.com/geekcom/validator-docs/pull/44
48+
[#43]: https://github.com/geekcom/validator-docs/pull/43
49+
[#42]: https://github.com/geekcom/validator-docs/pull/42
50+
[#41]: https://github.com/geekcom/validator-docs/pull/41
51+
[#40]: https://github.com/geekcom/validator-docs/pull/40
52+
[#39]: https://github.com/geekcom/validator-docs/pull/39
53+
[#38]: https://github.com/geekcom/validator-docs/pull/38
54+
[#37]: https://github.com/geekcom/validator-docs/pull/37
55+
[#35]: https://github.com/geekcom/validator-docs/pull/35
56+
[#33]: https://github.com/geekcom/validator-docs/pull/33
57+
[#32]: https://github.com/geekcom/validator-docs/pull/32
58+
[#31]: https://github.com/geekcom/validator-docs/pull/31
59+
[#30]: https://github.com/geekcom/validator-docs/pull/30
60+
[#27]: https://github.com/geekcom/validator-docs/pull/27
61+
[#25]: https://github.com/geekcom/validator-docs/pull/25
62+
[#24]: https://github.com/geekcom/validator-docs/pull/24
63+
[#23]: https://github.com/geekcom/validator-docs/pull/23
64+
[#21]: https://github.com/geekcom/validator-docs/pull/21
65+
[#20]: https://github.com/geekcom/validator-docs/pull/20
66+
[#19]: https://github.com/geekcom/validator-docs/pull/19
67+
[#17]: https://github.com/geekcom/validator-docs/pull/17
68+
[#12]: https://github.com/geekcom/validator-docs/pull/12
69+
[#11]: https://github.com/geekcom/validator-docs/pull/11
70+
[#8]: https://github.com/geekcom/validator-docs/pull/8
71+
[#5]: https://github.com/geekcom/validator-docs/pull/5
72+
[#4]: https://github.com/geekcom/validator-docs/pull/4
73+
[#3]: https://github.com/geekcom/validator-docs/pull/3
74+
[#2]: https://github.com/geekcom/validator-docs/pull/2
75+
[#1]: https://github.com/geekcom/validator-docs/pull/1
76+
[@yvescabral]: https://github.com/yvescabral
77+
[@victorhsanjos]: https://github.com/victorhsanjos
78+
[@setefocos]: https://github.com/setefocos
79+
[@lordantonelli]: https://github.com/lordantonelli
80+
[@kidh0]: https://github.com/kidh0
81+
[@geekcom]: https://github.com/geekcom
82+
[@andrergcosta]: https://github.com/andrergcosta
83+
[@MrEko]: https://github.com/MrEko

src/validator-docs/Rules/Certidao.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace geekcom\ValidatorDocs\Rules;
6+
7+
use function mb_strlen;
8+
use function preg_match;
9+
use function substr;
10+
11+
final class Certidao extends Sanitization
12+
{
13+
/*
14+
* CERTIDÃO DE NASCIMENTO/CASAMENTO/ÓBITO
15+
* Fonte: http://ghiorzi.org/DVnew.htm#zc
16+
*
17+
* Nota: se o resto for "10", o DV será "1"
18+
*/
19+
public function validateCertidao($attribute, $value): bool
20+
{
21+
$certidao = $this->sanitize($value);
22+
23+
if (!preg_match("/[0-9]{32}/", $certidao)) {
24+
return false;
25+
}
26+
27+
$num = substr($certidao, 0, -2);
28+
$dv = substr($certidao, -2);
29+
30+
$dv1 = $this->somaPonderadaCertidao($num) % 11;
31+
$dv1 = $dv1 > 9 ? 1 : $dv1;
32+
$dv2 = $this->somaPonderadaCertidao($num.$dv1) % 11;
33+
$dv2 = $dv2 > 9 ? 1 : $dv2;
34+
35+
// Compara o dv recebido com os dois numeros calculados
36+
if ($dv === $dv1.$dv2) {
37+
return true;
38+
}
39+
40+
return false;
41+
}
42+
43+
private function somaPonderadaCertidao($value): int
44+
{
45+
$soma = 0;
46+
47+
$multiplicador = 32 - mb_strlen($value);
48+
49+
for ($i = 0; $i < mb_strlen($value); $i++) {
50+
51+
$soma += $value[$i] * $multiplicador;
52+
53+
$multiplicador += 1;
54+
$multiplicador = $multiplicador > 10 ? 0 : $multiplicador;
55+
}
56+
57+
return $soma;
58+
}
59+
}

src/validator-docs/Rules/Cnh.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace geekcom\ValidatorDocs\Rules;
6+
7+
use function mb_strlen;
8+
use function is_scalar;
9+
10+
final class Cnh extends Sanitization
11+
{
12+
/**
13+
* Trecho retirado do respect validation
14+
*/
15+
public function validateCnh($attribute, $value): bool
16+
{
17+
$value = $this->sanitize($value);
18+
19+
if (!is_scalar($value)) {
20+
return false;
21+
}
22+
23+
if (mb_strlen($value) != 11 || ((int) $value === 0)) {
24+
return false;
25+
}
26+
27+
for ($c = $s1 = $s2 = 0, $p = 9; $c < 9; $c++, $p--) {
28+
$s1 += (int) $value[$c] * $p;
29+
$s2 += (int) $value[$c] * (10 - $p);
30+
}
31+
32+
$dv1 = $s1 % 11;
33+
if ($value[9] != ($dv1 > 9) ? 0 : $dv1) {
34+
return false;
35+
}
36+
37+
$dv2 = $s2 % 11 - ($dv1 > 9 ? 2 : 0);
38+
39+
$check = $dv2 < 0 ? $dv2 + 11 : ($dv2 > 9 ? 0 : $dv2);
40+
41+
return $value[10] == $check;
42+
}
43+
}

src/validator-docs/Rules/Cnpj.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace geekcom\ValidatorDocs\Rules;
6+
7+
use function mb_strlen;
8+
use function preg_match;
9+
10+
final class Cnpj extends Sanitization
11+
{
12+
public function validateCnpj($attribute, $value): bool
13+
{
14+
$c = $this->sanitize($value);
15+
16+
if (mb_strlen($c) != 14 || preg_match("/^{$c[0]}{14}$/", $c)) {
17+
return false;
18+
}
19+
20+
$b = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
21+
22+
for ($i = 0, $n = 0; $i < 12; $n += $c[$i] * $b[++$i]);
23+
24+
if ($c[12] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
25+
return false;
26+
}
27+
28+
for ($i = 0, $n = 0; $i <= 12; $n += $c[$i] * $b[$i++]);
29+
30+
if ($c[13] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
31+
return false;
32+
}
33+
34+
return true;
35+
}
36+
}

src/validator-docs/Rules/Cns.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace geekcom\ValidatorDocs\Rules;
6+
7+
use function preg_match;
8+
use function mb_strlen;
9+
10+
final class Cns extends Sanitization
11+
{
12+
public function validateCns($attribute, $value): bool
13+
{
14+
$cns = $this->sanitize($value);
15+
16+
// CNSs definitivos começam em 1 ou 2 / CNSs provisórios em 7, 8 ou 9
17+
if (preg_match("/[1-2][0-9]{10}00[0-1][0-9]/", $cns) || preg_match("/[7-9][0-9]{14}/", $cns)) {
18+
return $this->somaPonderadaCns($cns) % 11 == 0;
19+
}
20+
21+
return false;
22+
}
23+
24+
private function somaPonderadaCns($value): int
25+
{
26+
$soma = 0;
27+
28+
for ($i = 0; $i < mb_strlen($value); $i++) {
29+
$soma += $value[$i] * (15 - $i);
30+
}
31+
32+
return $soma;
33+
}
34+
}

src/validator-docs/Rules/Cpf.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace geekcom\ValidatorDocs\Rules;
6+
7+
use function preg_match;
8+
use function mb_strlen;
9+
10+
final class Cpf extends Sanitization
11+
{
12+
public function validateCpf($attribute, $value): bool
13+
{
14+
$c = $this->sanitize($value);
15+
16+
if (mb_strlen($c) != 11 || preg_match("/^{$c[0]}{11}$/", $c)) {
17+
return false;
18+
}
19+
20+
for ($s = 10, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
21+
22+
if ($c[9] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
23+
return false;
24+
}
25+
26+
for ($s = 11, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
27+
28+
if ($c[10] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
29+
return false;
30+
}
31+
32+
return true;
33+
}
34+
}

src/validator-docs/Rules/Nis.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace geekcom\ValidatorDocs\Rules;
6+
7+
use function mb_strlen;
8+
use function sprintf;
9+
use function preg_match;
10+
11+
final class Nis extends Sanitization
12+
{
13+
public function validateNis($attribute, $value): bool
14+
{
15+
$nis = sprintf('%011s', $this->sanitize($value));
16+
17+
if (mb_strlen($nis) != 11 || preg_match("/^{$nis[0]}{11}$/", $nis)) {
18+
return false;
19+
}
20+
21+
for ($d = 0, $p = 2, $c = 9; $c >= 0; $c--, ($p < 9) ? $p++ : $p = 2) {
22+
$d += $nis[$c] * $p;
23+
}
24+
25+
return ($nis[10] == (((10 * $d) % 11) % 10));
26+
}
27+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace geekcom\ValidatorDocs\Rules;
6+
7+
use function preg_replace;
8+
9+
abstract class Sanitization
10+
{
11+
public function sanitize($value): string
12+
{
13+
return preg_replace('/[^\d]/', '', $value);
14+
}
15+
}

0 commit comments

Comments
 (0)