Skip to content

Commit 424fc22

Browse files
authored
Merge pull request #118 from vicentimartins/111-valida-formato-se-nulo
feat: valida formato se doc é informado
2 parents 0c60643 + dee6b3e commit 424fc22

18 files changed

+277
-141
lines changed

.github/workflows/proposing-changes.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ jobs:
1111
steps:
1212
- uses: actions/checkout@v2
1313

14+
- name: Setup PHP with Xdebug
15+
uses: shivammathur/setup-php@v2
16+
with:
17+
php-version: '7.4'
18+
coverage: xdebug
19+
1420
- name: Validate composer.json and composer.lock
1521
run: composer validate
1622

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ composer.lock
55
.idea/
66
/tests/log/
77
*.cache
8+
docker-compose.yml
9+
.phpcs-cache
10+
.phpunit.result.cache
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Contracts;
4+
5+
interface ValidatorFormats
6+
{
7+
public static function validateFormat(string $value): bool;
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Formats;
4+
5+
use geekcom\ValidatorDocs\Contracts\ValidatorFormats;
6+
7+
class Certidao implements ValidatorFormats
8+
{
9+
public static function validateFormat(string $value): bool
10+
{
11+
return preg_match(
12+
'/^\d{6}[. ]\d{2}[. ]\d{2}[. ]\d{4}[. ]\d{1}[. ]\d{5}[. ]\d{3}[. ]\d{7}[- ]\d{2}$/',
13+
$value
14+
) > 0;
15+
}
16+
}

src/validator-docs/Formats/Cnpj.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Formats;
4+
5+
class Cnpj implements \geekcom\ValidatorDocs\Contracts\ValidatorFormats
6+
{
7+
public static function validateFormat(string $value): bool
8+
{
9+
return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $value) > 0;
10+
}
11+
}

src/validator-docs/Formats/Cpf.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Formats;
4+
5+
use geekcom\ValidatorDocs\Contracts\ValidatorFormats;
6+
7+
class Cpf implements ValidatorFormats
8+
{
9+
public static function validateFormat(string $value): bool
10+
{
11+
return preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/', $value) > 0;
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Formats;
4+
5+
use geekcom\ValidatorDocs\Contracts\ValidatorFormats;
6+
7+
class CpfCnpj implements ValidatorFormats
8+
{
9+
public static function validateFormat(string $value): bool
10+
{
11+
$cpf = new Cpf();
12+
$cnpj = new Cnpj();
13+
14+
return $cpf->validateFormat($value) || $cnpj->validateFormat($value);
15+
}
16+
}

src/validator-docs/Formats/Nis.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Formats;
4+
5+
use geekcom\ValidatorDocs\Contracts\ValidatorFormats;
6+
7+
class Nis implements ValidatorFormats
8+
{
9+
10+
public static function validateFormat(string $value): bool
11+
{
12+
return preg_match('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/', $value) > 0;
13+
}
14+
}

src/validator-docs/Validator.php

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,47 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace geekcom\ValidatorDocs;
64

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;
817
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+
2719
class Validator extends BaseValidator
2820
{
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);
3230
}
3331

34-
protected function validateFormatoCnpj($attribute, $value): bool
32+
protected function validateFormat($value, $document, $attribute = null)
3533
{
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+
}
5237
}
5338

5439
protected function validateCpf($attribute, $value): bool
5540
{
5641
$cpf = new Cpf();
5742

43+
$this->validateFormat($value, 'cpf');
44+
5845
return $cpf->validateCpf($attribute, $value);
5946
}
6047

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs;
4+
5+
use geekcom\ValidatorDocs\Contracts\ValidatorFormats as Contract;
6+
use Exception;
7+
8+
class ValidatorFormats
9+
{
10+
private const STRATEGY_NAMESPACE = 'geekcom\ValidatorDocs\Formats\%s';
11+
12+
public function execute(string $value, string $document): bool
13+
{
14+
if (!$value) {
15+
throw new Exception('Value not informed.');
16+
}
17+
18+
$validator = sprintf(self::STRATEGY_NAMESPACE, ucfirst($document));
19+
if (
20+
class_exists($validator)
21+
&& new $validator() instanceof Contract
22+
) {
23+
return $validator::validateFormat($value);
24+
}
25+
26+
throw new Exception('Don\'t exists validator for this document.');
27+
}
28+
}

0 commit comments

Comments
 (0)