|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -declare(strict_types=1); |
4 |
| - |
5 | 3 | namespace geekcom\ValidatorDocs;
|
6 | 4 |
|
7 |
| -use geekcom\ValidatorDocs\Rules\Ddd; |
| 5 | +use geekcom\ValidatorDocs\Rules\{Certidao, |
| 6 | + Cnh, |
| 7 | + Cnpj, |
| 8 | + Cns, |
| 9 | + Cpf, |
| 10 | + Ddd, |
| 11 | + InscricaoEstadual, |
| 12 | + Nis, |
| 13 | + Placa, |
| 14 | + Renavam, |
| 15 | + TituloEleitoral}; |
| 16 | +use Illuminate\Contracts\Translation\Translator; |
8 | 17 | use Illuminate\Validation\Validator as BaseValidator;
|
9 |
| -use geekcom\ValidatorDocs\Rules\TituloEleitoral; |
10 |
| -use geekcom\ValidatorDocs\Rules\Cns; |
11 |
| -use geekcom\ValidatorDocs\Rules\Nis; |
12 |
| -use geekcom\ValidatorDocs\Rules\Cpf; |
13 |
| -use geekcom\ValidatorDocs\Rules\Cnpj; |
14 |
| -use geekcom\ValidatorDocs\Rules\Cnh; |
15 |
| -use geekcom\ValidatorDocs\Rules\Certidao; |
16 |
| -use geekcom\ValidatorDocs\Rules\InscricaoEstadual; |
17 |
| -use geekcom\ValidatorDocs\Rules\Placa; |
18 |
| -use geekcom\ValidatorDocs\Rules\Renavam; |
19 |
| - |
20 |
| -use function preg_match; |
21 |
| - |
22 |
| -/** |
23 |
| - * |
24 |
| - * @author Daniel Rodrigues Lima |
25 |
| - |
26 |
| - */ |
| 18 | + |
27 | 19 | class Validator extends BaseValidator
|
28 | 20 | {
|
29 |
| - protected function validateFormatoCpf($attribute, $value): bool |
30 |
| - { |
31 |
| - return preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/', $value) > 0; |
| 21 | + public function __construct( |
| 22 | + Translator $translator, |
| 23 | + ValidatorFormats $formatValidator, |
| 24 | + array $data, |
| 25 | + array $rules, |
| 26 | + array $messages = [], |
| 27 | + array $customAttributes = [] |
| 28 | + ) { |
| 29 | + parent::__construct($translator, $data, $rules, $messages, $customAttributes); |
32 | 30 | }
|
33 | 31 |
|
34 |
| - protected function validateFormatoCnpj($attribute, $value): bool |
| 32 | + protected function validateFormat($value, $document, $attribute = null) |
35 | 33 | {
|
36 |
| - return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $value) > 0; |
37 |
| - } |
38 |
| - |
39 |
| - protected function validateFormatoCpfCnpj($attribute, $value): bool |
40 |
| - { |
41 |
| - return $this->validateFormatoCpf($attribute, $value) || $this->validateFormatoCnpj($attribute, $value); |
42 |
| - } |
43 |
| - |
44 |
| - protected function validateFormatoNis($attribute, $value): bool |
45 |
| - { |
46 |
| - return preg_match('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/', $value) > 0; |
47 |
| - } |
48 |
| - |
49 |
| - protected function validateFormatoCertidao($attribute, $value): bool |
50 |
| - { |
51 |
| - return preg_match('/^\d{6}[. ]\d{2}[. ]\d{2}[. ]\d{4}[. ]\d{1}[. ]\d{5}[. ]\d{3}[. ]\d{7}[- ]\d{2}$/', $value) > 0; |
| 34 | + if (!empty($value)) { |
| 35 | + return (new ValidatorFormats())->execute($value, $document); |
| 36 | + } |
52 | 37 | }
|
53 | 38 |
|
54 | 39 | protected function validateCpf($attribute, $value): bool
|
55 | 40 | {
|
56 | 41 | $cpf = new Cpf();
|
57 | 42 |
|
| 43 | + $this->validateFormat($value, 'cpf'); |
| 44 | + |
58 | 45 | return $cpf->validateCpf($attribute, $value);
|
59 | 46 | }
|
60 | 47 |
|
|
0 commit comments