Skip to content

Commit 023f3d1

Browse files
authored
Merge pull request #20 from geekcom/master
Master from develop
2 parents 9f9ef96 + 871d581 commit 023f3d1

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ a diferença é que agora, você terá os seguintes métodos de validação:
3737
* **cnpj** - Verifica se o CNPJ é valido. Para testar, basta utilizar o site http://www.geradorcnpj.com/
3838
* **cpf** - Verifica se o CPF é valido. Para testar, basta utilizar o site http://geradordecpf.org
3939
* **cpf_cnpj** - Verifica se é um CPF ou CNPJ valido. Para testar, basta utilizar um dos sites acima
40+
* **nis** - Verifica se o PIS/PASEP/NIT/NIS é valido. Para testar, basta utilizar o site https://www.4devs.com.br/gerador_de_pis_pasep
4041
* **formato_cnpj** - Verifica se a mascara do CNPJ é válida. ( 99.999.999/9999-99 )
4142
* **formato_cpf** - Verifica se a mascara do CPF é válida. ( 999.999.999-99 )
4243
* **formato_cpf_cnpj** - Verifica se a mascara do CPF ou CNPJ é válida. ( 999.999.999-99 ) ou ( 99.999.999/9999-99 )
44+
* **formato_nis** - Verifica se a mascara do PIS/PASEP/NIT/NIS é válida. ( 999.99999-99.9 )
4345

4446

4547
Então, podemos realizar um simples teste onde dizemos que o campo CPF será obrigatório e usamos a biblioteca para validar:

src/validator-docs/Validator.php

+33
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ protected function validateFormatoCpfCnpj($attribute, $value)
4343
{
4444
return $this->validateFormatoCpf($attribute, $value) || $this->validateFormatoCnpj($attribute, $value);
4545
}
46+
47+
/**
48+
* Valida o formato do PIS/PASEP/NIS/NIT
49+
* @param string $attribute
50+
* @param string $value
51+
* @return boolean
52+
*/
53+
protected function validateFormatoNis($attribute, $value)
54+
{
55+
return preg_match('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/', $value) > 0;
56+
}
4657

4758
/**
4859
* Valida CPF
@@ -224,5 +235,27 @@ protected function validateTituloEleitor($attribute, $value)
224235

225236
return true;
226237
}
238+
239+
/**
240+
* Valida PIS/PASEP/NIS/NIT
241+
* @param string $attribute
242+
* @param string $value
243+
* @return boolean
244+
*/
245+
246+
protected function validateNis($attribute, $value)
247+
{
248+
$nis = sprintf('%011s', preg_replace('{\D}', '', $value));
249+
250+
if (strlen($nis) != 11 || preg_match("/^{$nis[0]}{11}$/", $nis)) {
251+
return false;
252+
}
253+
254+
for ($d = 0, $p = 2, $c = 9; $c >= 0; $c--, ($p < 9) ? $p++ : $p = 2) {
255+
$d += $nis[$c] * $p;
256+
}
257+
258+
return ($nis[10] == (((10 * $d) % 11) % 10));
259+
}
227260

228261
}

src/validator-docs/ValidatorProvider.php

+2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ protected function getMessages()
4242
'cnpj' => 'CNPJ inválido',
4343
'cpf' => 'CPF inválido',
4444
'cpf_cnpj' => 'CPF ou CNPJ inválido',
45+
'nis' => 'PIS/PASEP/NIT/NIS inválido',
4546
'formato_cnpj' => 'Formato inválido para CNPJ',
4647
'formato_cpf' => 'Formato inválido para CPF',
4748
'formato_cpf_cnpj' => 'Formato inválido para CPF ou CNPJ',
49+
'formato_nis' => 'Formato inválido para PIS/PASEP/NIT/NIS',
4850
];
4951
}
5052

tests/TestValidator.php

+34
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,39 @@ public function testTituloEleitor()
145145

146146
$this->assertTrue($incorrect->fails());
147147
}
148+
149+
public function testNis()
150+
{
151+
$correct = \Validator::make(
152+
['certo' => '201.73374.34-9'],
153+
['certo' => 'nis']
154+
);
155+
156+
$incorrect = \Validator::make(
157+
['errado' => '201.73374.34-0'],
158+
['errado' => 'nis']
159+
);
160+
161+
$this->assertTrue($correct->passes());
162+
163+
$this->assertTrue($incorrect->fails());
164+
}
165+
166+
public function testNisormato()
167+
{
168+
$correct = \Validator::make(
169+
['certo' => '201.73374.34-9'],
170+
['certo' => 'formato-nis']
171+
);
172+
173+
$incorrect = \Validator::make(
174+
['errado' => '201.733.7434-9'],
175+
['errado' => 'formato-nis']
176+
);
177+
178+
$this->assertTrue($correct->passes());
179+
180+
$this->assertTrue($incorrect->fails());
181+
}
148182

149183
}

0 commit comments

Comments
 (0)