Skip to content

Commit 57a16aa

Browse files
authored
Merge pull request #95 from omarkdev/placa
Validação Placa
2 parents ec1f357 + 8218f66 commit 57a16aa

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ $this->validate($request, [
115115
]);
116116
```
117117

118+
* **placa** - Verifica se a PLACA é válida
119+
120+
```php
121+
$this->validate($request, [
122+
'placa' => 'required|placa',
123+
]);
124+
```
125+
118126
* **formato_cnpj** - Verifica se o formato de um CNPJ é válida. ( 99.999.999/9999-99 )
119127

120128
```php
@@ -185,6 +193,7 @@ public function store(Request $request)
185193
'nis' => 'required|nis',
186194
'cns' => 'required|cns',
187195
'renavam' => 'required|renavam',
196+
'placa' => 'required|placa',
188197
]);
189198

190199
dd($data);
@@ -203,6 +212,8 @@ public function store(Request $request)
203212
* **CNS** - https://geradornv.com.br/gerador-cns/
204213
* **CERTIDÃO** - https://www.treinaweb.com.br/ferramentas-para-desenvolvedores/gerador/certidao
205214
* **RENAVAM** - https://www.4devs.com.br/gerador_de_renavam
215+
* **PLACA** - https://www.4devs.com.br/gerador_de_placa_automoveis
216+
206217
Fique a vontade para contribuir fazendo um fork.
207218

208219
Caso tenha alguma dúvida ou encontre algum bug, abra uma [issue](https://github.com/geekcom/validator-docs/issues) ou pesquise por issues antigas.

src/validator-docs/Rules/Placa.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Rules;
4+
5+
final class Placa extends Sanitization
6+
{
7+
public function validatePlaca($attribute, $placa): bool
8+
{
9+
$placa = $this->sanitize($placa);
10+
11+
$regexPlaca = '/^[A-Z][0-9]{5}$|' . // Placas dos anos 1915-1941 [ex A12345]
12+
'^[0-9]{7}$|' . // Placas dos anos 1941-1969 [ex 1234567]
13+
'^[A-Z]{2}[0-9]{4}$|' . // Placas dos anos 1969-1990 [ex AA1234]
14+
'^[A-Z]{3}[0-9]{4}$|' . // Placas dos anos 1990-2018 [ex AAA1234]
15+
'^[A-Z]{3}[0-9]{1}[A-Z]{1}[0-9]{2}$|' . // Placas de carros dos anos 2018+ [ex AAA1A23]
16+
'^[A-Z]{3}[0-9]{2}[A-Z]{1}[0-9]{1}$' . // Placas de motos dos anos 2018+ [ex AAA12A3]
17+
'/i';
18+
19+
return preg_match($regexPlaca, $placa) > 0;
20+
}
21+
}

src/validator-docs/Validator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace geekcom\ValidatorDocs;
66

7+
use geekcom\ValidatorDocs\Rules\Placa;
78
use geekcom\ValidatorDocs\Rules\Renavam;
89
use Illuminate\Validation\Validator as BaseValidator;
910
use geekcom\ValidatorDocs\Rules\TituloEleitoral;
@@ -111,4 +112,11 @@ protected function validateRenavam($attribute, $value): bool
111112

112113
return $renavam->validateRenavam($attribute, $value);
113114
}
115+
116+
protected function validatePlaca($attribute, $value): bool
117+
{
118+
$placa = new Placa();
119+
120+
return $placa->validatePlaca($attribute, $value);
121+
}
114122
}

tests/TestValidator.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,21 @@ public function formatoRenavam()
291291

292292
$this->assertTrue($correct->passes());
293293
}
294-
}
294+
295+
public function placa()
296+
{
297+
$correct = Validator::make(
298+
['certo' => 'P15186'],
299+
['certo' => 'placa']
300+
);
301+
302+
$incorrect = Validator::make(
303+
['errado' => 'X1234'],
304+
['errado' => 'placa']
305+
);
306+
307+
$this->assertTrue($correct->passes());
308+
309+
$this->assertTrue($incorrect->fails());
310+
}
311+
}

0 commit comments

Comments
 (0)