Skip to content

Commit dd6806a

Browse files
authored
Merge pull request #3 from andrergcosta/validar_cpf_e_cnpj
Criada as funções validateFormatoCpfCnpj e validateCpfCnpj
2 parents 33b1817 + 5d1e410 commit dd6806a

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/validator-docs/Validator.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ protected function validateFormatoCnpj($attribute, $value)
3333
return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $value) > 0;
3434
}
3535

36+
/**
37+
* Valida o formato do cpf ou cnpj
38+
* @param string $attribute
39+
* @param string $value
40+
* @return boolean
41+
*/
42+
protected function validateFormatoCpfCnpj($attribute, $value)
43+
{
44+
return $this->validateFormatoCpf($attribute, $value) || $this->validateFormatoCnpj($attribute, $value);
45+
}
46+
3647
/**
3748
* Valida se o CPF é válido
3849
* @param string $attribute
@@ -96,6 +107,17 @@ protected function validateCnpj($attribute, $value)
96107

97108
}
98109

110+
/**
111+
* Valida se o CNPJ é válido
112+
* @param string $attribute
113+
* @param string $value
114+
* @return boolean
115+
*/
116+
protected function validateCpfCnpj($attribute, $value)
117+
{
118+
return ($this->validateCpf($attribute, $value) || $this->validateCnpj($attribute, $value));
119+
}
120+
99121
/**
100122
* Valida se o CNH é válido
101123
* @param string $attribute

src/validator-docs/ValidatorProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ protected function getMessages()
4040
'cnh' => 'O campo :attribute não é uma carteira nacional de habilitação válida',
4141
'cnpj' => 'O campo :attribute não é um CNPJ válido',
4242
'cpf' => 'O campo :attribute não é um CPF válido',
43+
'cpf_cnpj' => 'O campo :attribute não é válido',
4344
'formato_cnpj' => 'O campo :attribute não possui o formato válido de CNPJ',
4445
'formato_cpf' => 'O campo :attribute não possui o formato válido de CPF',
46+
'formato_cpf_cnpj' => 'O campo :attribute não possui um formato válido',
4547
];
4648
}
4749

tests/TestValidator.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,43 @@ public function testCnpjFormato()
7474
$this->assertTrue($incorrect->fails());
7575
}
7676

77+
78+
public function testCpfCnpj()
79+
{
80+
$correct = \Validator::make(
81+
['certo' => '53.084.587/0001-20'],
82+
['certo' => 'cpf-cnpj']
83+
);
84+
85+
$incorrect = \Validator::make(
86+
['errado' => '99800-1926'],
87+
['errado' => 'cpf-cnpj']
88+
);
89+
90+
$this->assertTrue($correct->passes());
91+
92+
$this->assertTrue($incorrect->fails());
93+
}
94+
95+
96+
public function testCpfCnpjFormato()
97+
{
98+
$correct = \Validator::make(
99+
['certo' => '094.050.986-59'],
100+
['certo' => 'formato-cpf-cnpj']
101+
);
102+
103+
$incorrect = \Validator::make(
104+
['errado' => '51.084.587/000120'],
105+
['errado' => 'formato-cpf-cnpj']
106+
);
107+
108+
$this->assertTrue($correct->passes());
109+
110+
$this->assertTrue($incorrect->fails());
111+
}
112+
113+
77114
public function testCnh()
78115
{
79116
$correct = \Validator::make(

0 commit comments

Comments
 (0)