Skip to content

Commit 84e6ea6

Browse files
author
Daniel Rodrigues Lima
committed
commit inicial
1 parent ded2700 commit 84e6ea6

File tree

10 files changed

+433
-0
lines changed

10 files changed

+433
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.phar
3+
composer.lock
4+
.DS_Store

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#Laravel 5 - Validação em Português
2+
3+
Biblioteca para validação dos seguintes documentos: CPF, CNPJ e CNH.
4+
5+
#Instalação
6+
7+
No arquivo `composer.json`, adicione:
8+
9+
```json
10+
{
11+
"geekcom/validator-docs" : "1.*"
12+
}
13+
```
14+
15+
Rode o comando `composer update --no-scripts`.
16+
17+
Após a instalação, adicione no arquivo `config/app.php` a seguinte linha:
18+
19+
```php
20+
21+
geekcom\ValidatorDocs\ValidatorProvider::class
22+
23+
```
24+
25+
Para utilizar a validação agora, basta fazer o procedimento padrão do `Laravel`.
26+
27+
A diferença é que agora, você terá os seguintes métodos de validação:
28+
29+
* cnpj - Valida se o CNPJ é valido. Para testar, basta utilizar o site http://www.geradorcnpj.com/
30+
31+
* cpf - Valida se o cpf é valido. Para testar, basta utilizar o site http://geradordecpf.
32+
org
33+
34+
* formato_cnpj - Valida se a mascará do CNPJ é válida
35+
36+
* formato_cpf - Valida se a mascará do cpf está certo. 999.999.999-99
37+
38+
39+
Então, podemos usar um simples teste:
40+
41+
42+
```php
43+
$validator = Validator::make(
44+
['telefone' => '(77)9999-3333'],
45+
['telefone' => 'required|telefone_com_ddd']
46+
);
47+
48+
dd($validator->fails());
49+
50+
```
51+
52+
53+
Já existe nessa biblioteca algumas mensagens padrão para as validações de cada um dos items citados acima.
54+
55+
Para modificar isso, basta adicionar ao terceiro parâmetro de `Validator::make` um array, contendo o índice com o nome da validação e o valor com a mensagem desejada.
56+
57+
58+
Exemplo:
59+
60+
61+
```php
62+
63+
```
64+

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "geekcom/validator-docs",
3+
"description": "Biblioteca para validação de CPF e CNPJ",
4+
"license" : "MIT",
5+
"authors": [
6+
{
7+
"name": "Daniel Rodrigues Lima",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"illuminate/support": "5.*"
13+
},
14+
"required-dev" : {
15+
"laravel/framework": "5.*"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"geekcom\\ValidatorDocs\\": "src/validator-docs"
20+
}
21+
},
22+
"minimum-stability": "stable",
23+
"require-dev": {
24+
"laravel/laravel": "5.0.*"
25+
}
26+
}

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="tests/bootstrap.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Package Test Suite">
15+
<directory suffix=".php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

src/validator-docs/Validator.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs;
4+
5+
use Illuminate\Validation\Validator as BaseValidator;
6+
7+
/**
8+
*
9+
* @author Daniel Rodrigues Lima
10+
*/
11+
12+
class Validator extends BaseValidator
13+
{
14+
/**
15+
* Valida o formato do cpf
16+
* @param string $attribute
17+
* @param string $value
18+
* @return boolean
19+
*/
20+
protected function validateFormatoCpf($attribute, $value)
21+
{
22+
return preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/', $value) > 0;
23+
}
24+
25+
/**
26+
* Valida o formato do cnpj
27+
* @param string $attribute
28+
* @param string $value
29+
* @return boolean
30+
*/
31+
protected function validateFormatoCnpj($attribute, $value)
32+
{
33+
return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $value) > 0;
34+
}
35+
36+
/**
37+
* Valida se o CPF é válido
38+
* @param string $attribute
39+
* @param string $value
40+
* @return boolean
41+
*/
42+
43+
protected function validateCpf($attribute, $value)
44+
{
45+
$c = preg_replace('/\D/', '', $value);
46+
47+
if (strlen($c) != 11 || preg_match("/^{$c[0]}{11}$/", $c)) {
48+
return false;
49+
}
50+
51+
for ($s = 10, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
52+
53+
if ($c[9] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
54+
return false;
55+
}
56+
57+
for ($s = 11, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
58+
59+
if ($c[10] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
60+
return false;
61+
}
62+
63+
return true;
64+
65+
}
66+
67+
/**
68+
* Valida se o CNPJ é válido
69+
* @param string $attribute
70+
* @param string $value
71+
* @return boolean
72+
*/
73+
protected function validateCnpj($attribute, $value)
74+
{
75+
$c = preg_replace('/\D/', '', $value);
76+
77+
$b = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
78+
79+
if (strlen($c) != 14) {
80+
return false;
81+
}
82+
83+
for ($i = 0, $n = 0; $i < 12; $n += $c[$i] * $b[++$i]);
84+
85+
if ($c[12] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
86+
return false;
87+
}
88+
89+
for ($i = 0, $n = 0; $i <= 12; $n += $c[$i] * $b[$i++]);
90+
91+
if ($c[13] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
92+
return false;
93+
}
94+
95+
return true;
96+
97+
}
98+
99+
/**
100+
* Valida se o CNH é válido
101+
* @param string $attribute
102+
* @param string $value
103+
* @return boolean
104+
*/
105+
106+
protected function validateCnh($attribute, $value)
107+
{
108+
// Trecho retirado do respect validation
109+
110+
$ret = false;
111+
112+
if ((strlen($input = preg_replace('/[^\d]/', '', $value)) == 11)
113+
&& (str_repeat($input[1], 11) != $input)) {
114+
$dsc = 0;
115+
116+
for ($i = 0, $j = 9, $v = 0; $i < 9; ++$i, --$j) {
117+
118+
$v += (int) $input[$i] * $j;
119+
120+
}
121+
122+
if (($vl1 = $v % 11) >= 10) {
123+
124+
$vl1 = 0;
125+
$dsc = 2;
126+
127+
}
128+
129+
for ($i = 0, $j = 1, $v = 0; $i < 9; ++$i, ++$j) {
130+
131+
$v += (int) $input[$i] * $j;
132+
133+
}
134+
135+
$vl2 = ($x = ($v % 11)) >= 10 ? 0 : $x - $dsc;
136+
137+
$ret = sprintf('%d%d', $vl1, $vl2) == substr($input, -2);
138+
}
139+
140+
return $ret;
141+
}
142+
143+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class ValidatorProvider extends ServiceProvider
8+
{
9+
10+
/**
11+
* Indicates if loading of the provider is deferred.
12+
*
13+
* @var bool
14+
*/
15+
protected $defer = false;
16+
17+
18+
/**
19+
* Bootstrap the application events.
20+
*
21+
* @return void
22+
*/
23+
24+
public function boot()
25+
{
26+
27+
$me = $this;
28+
29+
$this->app['validator']->resolver(function ($translator, $data, $rules, $messages) use($me)
30+
{
31+
$messages += $me->getMessages();
32+
33+
return new Validator($translator, $data, $rules, $messages);
34+
});
35+
}
36+
37+
38+
protected function getMessages()
39+
{
40+
return [
41+
'cnh' => 'O campo :attribute não é uma carteira nacional de habilitação válida',
42+
'cnpj' => 'O campo :attribute não é um CNPJ válido',
43+
'cpf' => 'O campo :attribute não é um CPF válido',
44+
'formato_cnpj' => 'O campo :attribute não possui o formato válido de CNPJ',
45+
'formato_cpf' => 'O campo :attribute não possui o formato válido de CPF',
46+
];
47+
}
48+
49+
/**
50+
* Register the service provider.
51+
*
52+
* @return void
53+
*/
54+
public function register(){}
55+
56+
/**
57+
* Get the services provided by the provider.
58+
*
59+
* @return array
60+
*/
61+
public function provides()
62+
{
63+
return array();
64+
}
65+
66+
}

tests/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)