Skip to content

solved: complexity, update: tests #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!-- changelog-linker -->

<!-- dumped content start -->

## Unreleased

- [#48] added support for changelogs, Thanks to [@geekcom]
- [#45] fixing "Unparenthesized is deprecated" error, Thanks to [@kidh0]
- [#17] fix: erro em phpunit.xml, melhorias nos testes, namespace para testes, Thanks to [@geekcom]
- [#25] Melhorias de leitura nos códigos, Thanks to [@geekcom]
- [#2] Alteração da função validateCnpj para impedir que o Cnpj 00.000.000/0…, Thanks to [@andrergcosta]
- [#3] Criada as funções validateFormatoCpfCnpj e validateCpfCnpj, Thanks to [@andrergcosta]
- [#4] Inclusão do exemplo para validação de CPF/CNPJ no arquivo readme, Thanks to [@andrergcosta]
- [#5] Validar título eleitor, Thanks to [@andrergcosta]
- [#8] Faltando parâmetro $attributes em ValidatorProvider., Thanks to [@setefocos]
- [#11] nova badge e melhoria no readme.me, Thanks to [@geekcom]
- [#12] scrutinizer CI, Thanks to [@geekcom]
- [#19] Criação da função para verificar PIS/PASEP/NIS/NIT, Thanks to [@lordantonelli]
- [#20] Master from develop, Thanks to [@geekcom]
- [#21] Merge pull request [#20] from geekcom/master, Thanks to [@geekcom]
- [#23] update docs, minor corrections, Thanks to [@geekcom]
- [#24] update docs, Thanks to [@geekcom]
- [#30] update scrutinizer CI check, Thanks to [@geekcom]
- [#27] atualizacao para Laravel 6, Thanks to [@geekcom]
- [#47] Merge pull request [#46] from geekcom/master, Thanks to [@geekcom]
- [#31] Atualização dos requisitos e testes, Thanks to [@victorhsanjos]
- [#32] master into develop, Thanks to [@geekcom]
- [#33] Merge pull request [#32] from geekcom/master, Thanks to [@geekcom]
- [#35] auto-discover composer, Thanks to [@geekcom]
- [#37] Adicionando validação do Cartão Nacional de Saúde, Thanks to [@MrEko]
- [#38] Merge, Thanks to [@geekcom]
- [#39] Merge pull request [#38] from geekcom/master, Thanks to [@geekcom]
- [#40] melhorias de documentacao e sintaxe, v3.0.0, Thanks to [@geekcom]
- [#41] Validação de Certidão de Nascimento, Casamento ou Óbito, Thanks to [@MrEko]
- [#42] merge master into develop, Thanks to [@geekcom]
- [#43] Merge pull request [#42] from geekcom/master, Thanks to [@geekcom]
- [#44] melhoria na validacao de CNH,strict_types PHP ativado, Thanks to [@geekcom]
- [#46] merge master inte develop, Thanks to [@geekcom]
- [#1] Update README.md, Thanks to [@yvescabral]

<!-- dumped content end -->

[#48]: https://github.com/geekcom/validator-docs/pull/48
[#47]: https://github.com/geekcom/validator-docs/pull/47
[#46]: https://github.com/geekcom/validator-docs/pull/46
[#45]: https://github.com/geekcom/validator-docs/pull/45
[#44]: https://github.com/geekcom/validator-docs/pull/44
[#43]: https://github.com/geekcom/validator-docs/pull/43
[#42]: https://github.com/geekcom/validator-docs/pull/42
[#41]: https://github.com/geekcom/validator-docs/pull/41
[#40]: https://github.com/geekcom/validator-docs/pull/40
[#39]: https://github.com/geekcom/validator-docs/pull/39
[#38]: https://github.com/geekcom/validator-docs/pull/38
[#37]: https://github.com/geekcom/validator-docs/pull/37
[#35]: https://github.com/geekcom/validator-docs/pull/35
[#33]: https://github.com/geekcom/validator-docs/pull/33
[#32]: https://github.com/geekcom/validator-docs/pull/32
[#31]: https://github.com/geekcom/validator-docs/pull/31
[#30]: https://github.com/geekcom/validator-docs/pull/30
[#27]: https://github.com/geekcom/validator-docs/pull/27
[#25]: https://github.com/geekcom/validator-docs/pull/25
[#24]: https://github.com/geekcom/validator-docs/pull/24
[#23]: https://github.com/geekcom/validator-docs/pull/23
[#21]: https://github.com/geekcom/validator-docs/pull/21
[#20]: https://github.com/geekcom/validator-docs/pull/20
[#19]: https://github.com/geekcom/validator-docs/pull/19
[#17]: https://github.com/geekcom/validator-docs/pull/17
[#12]: https://github.com/geekcom/validator-docs/pull/12
[#11]: https://github.com/geekcom/validator-docs/pull/11
[#8]: https://github.com/geekcom/validator-docs/pull/8
[#5]: https://github.com/geekcom/validator-docs/pull/5
[#4]: https://github.com/geekcom/validator-docs/pull/4
[#3]: https://github.com/geekcom/validator-docs/pull/3
[#2]: https://github.com/geekcom/validator-docs/pull/2
[#1]: https://github.com/geekcom/validator-docs/pull/1
[@yvescabral]: https://github.com/yvescabral
[@victorhsanjos]: https://github.com/victorhsanjos
[@setefocos]: https://github.com/setefocos
[@lordantonelli]: https://github.com/lordantonelli
[@kidh0]: https://github.com/kidh0
[@geekcom]: https://github.com/geekcom
[@andrergcosta]: https://github.com/andrergcosta
[@MrEko]: https://github.com/MrEko
59 changes: 59 additions & 0 deletions src/validator-docs/Rules/Certidao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace geekcom\ValidatorDocs\Rules;

use function mb_strlen;
use function preg_match;
use function substr;

final class Certidao extends Sanitization
{
/*
* CERTIDÃO DE NASCIMENTO/CASAMENTO/ÓBITO
* Fonte: http://ghiorzi.org/DVnew.htm#zc
*
* Nota: se o resto for "10", o DV será "1"
*/
public function validateCertidao($attribute, $value): bool
{
$certidao = $this->sanitize($value);

if (!preg_match("/[0-9]{32}/", $certidao)) {
return false;
}

$num = substr($certidao, 0, -2);
$dv = substr($certidao, -2);

$dv1 = $this->somaPonderadaCertidao($num) % 11;
$dv1 = $dv1 > 9 ? 1 : $dv1;
$dv2 = $this->somaPonderadaCertidao($num.$dv1) % 11;
$dv2 = $dv2 > 9 ? 1 : $dv2;

// Compara o dv recebido com os dois numeros calculados
if ($dv === $dv1.$dv2) {
return true;
}

return false;
}

private function somaPonderadaCertidao($value): int
{
$soma = 0;

$multiplicador = 32 - mb_strlen($value);

for ($i = 0; $i < mb_strlen($value); $i++) {

$soma += $value[$i] * $multiplicador;

$multiplicador += 1;
$multiplicador = $multiplicador > 10 ? 0 : $multiplicador;
}

return $soma;
}
}
43 changes: 43 additions & 0 deletions src/validator-docs/Rules/Cnh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace geekcom\ValidatorDocs\Rules;

use function mb_strlen;
use function is_scalar;

final class Cnh extends Sanitization
{
/**
* Trecho retirado do respect validation
*/
public function validateCnh($attribute, $value): bool
{
$value = $this->sanitize($value);

if (!is_scalar($value)) {
return false;
}

if (mb_strlen($value) != 11 || ((int) $value === 0)) {
return false;
}

for ($c = $s1 = $s2 = 0, $p = 9; $c < 9; $c++, $p--) {
$s1 += (int) $value[$c] * $p;
$s2 += (int) $value[$c] * (10 - $p);
}

$dv1 = $s1 % 11;
if ($value[9] != ($dv1 > 9) ? 0 : $dv1) {
return false;
}

$dv2 = $s2 % 11 - ($dv1 > 9 ? 2 : 0);

$check = $dv2 < 0 ? $dv2 + 11 : ($dv2 > 9 ? 0 : $dv2);

return $value[10] == $check;
}
}
36 changes: 36 additions & 0 deletions src/validator-docs/Rules/Cnpj.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace geekcom\ValidatorDocs\Rules;

use function mb_strlen;
use function preg_match;

final class Cnpj extends Sanitization
{
public function validateCnpj($attribute, $value): bool
{
$c = $this->sanitize($value);

if (mb_strlen($c) != 14 || preg_match("/^{$c[0]}{14}$/", $c)) {
return false;
}

$b = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];

for ($i = 0, $n = 0; $i < 12; $n += $c[$i] * $b[++$i]);

if ($c[12] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}

for ($i = 0, $n = 0; $i <= 12; $n += $c[$i] * $b[$i++]);

if ($c[13] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}

return true;
}
}
34 changes: 34 additions & 0 deletions src/validator-docs/Rules/Cns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace geekcom\ValidatorDocs\Rules;

use function preg_match;
use function mb_strlen;

final class Cns extends Sanitization
{
public function validateCns($attribute, $value): bool
{
$cns = $this->sanitize($value);

// CNSs definitivos começam em 1 ou 2 / CNSs provisórios em 7, 8 ou 9
if (preg_match("/[1-2][0-9]{10}00[0-1][0-9]/", $cns) || preg_match("/[7-9][0-9]{14}/", $cns)) {
return $this->somaPonderadaCns($cns) % 11 == 0;
}

return false;
}

private function somaPonderadaCns($value): int
{
$soma = 0;

for ($i = 0; $i < mb_strlen($value); $i++) {
$soma += $value[$i] * (15 - $i);
}

return $soma;
}
}
34 changes: 34 additions & 0 deletions src/validator-docs/Rules/Cpf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace geekcom\ValidatorDocs\Rules;

use function preg_match;
use function mb_strlen;

final class Cpf extends Sanitization
{
public function validateCpf($attribute, $value): bool
{
$c = $this->sanitize($value);

if (mb_strlen($c) != 11 || preg_match("/^{$c[0]}{11}$/", $c)) {
return false;
}

for ($s = 10, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);

if ($c[9] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}

for ($s = 11, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);

if ($c[10] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}

return true;
}
}
27 changes: 27 additions & 0 deletions src/validator-docs/Rules/Nis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace geekcom\ValidatorDocs\Rules;

use function mb_strlen;
use function sprintf;
use function preg_match;

final class Nis extends Sanitization
{
public function validateNis($attribute, $value): bool
{
$nis = sprintf('%011s', $this->sanitize($value));

if (mb_strlen($nis) != 11 || preg_match("/^{$nis[0]}{11}$/", $nis)) {
return false;
}

for ($d = 0, $p = 2, $c = 9; $c >= 0; $c--, ($p < 9) ? $p++ : $p = 2) {
$d += $nis[$c] * $p;
}

return ($nis[10] == (((10 * $d) % 11) % 10));
}
}
15 changes: 15 additions & 0 deletions src/validator-docs/Rules/Sanitization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace geekcom\ValidatorDocs\Rules;

use function preg_replace;

abstract class Sanitization
{
public function sanitize($value): string
{
return preg_replace('/[^\d]/', '', $value);
}
}
Loading