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

+6
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

+3
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
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+
}
+16
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

+11
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

+13
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+
}
+16
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

+14
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

+28-41
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

+28
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+
}

src/validator-docs/ValidatorProvider.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ public function boot()
2222
{
2323
$me = $this;
2424

25-
$this->app['validator']->resolver(function ($translator, $data, $rules, $messages, $attributes) use ($me) {
26-
$messages += $me->getMessages();
25+
$validatorFormats = new ValidatorFormats();
2726

28-
return new Validator($translator, $data, $rules, $messages, $attributes);
29-
});
27+
$this->app['validator']
28+
->resolver(
29+
function ($translator, $data, $rules, $messages, $attributes) use ($me, $validatorFormats) {
30+
$messages += $me->getMessages();
31+
32+
return new Validator($translator, $validatorFormats, $data, $rules, $messages, $attributes);
33+
}
34+
);
3035
}
3136

3237
protected function getMessages()

tests/Formats/CertidaoTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Tests\Formats;
4+
5+
use geekcom\ValidatorDocs\Formats\Certidao;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class CertidaoTest extends TestCase
9+
{
10+
public function testValidateFormat()
11+
{
12+
$certidao = new Certidao();
13+
14+
self::assertTrue($certidao->validateFormat('434546.02.55.2019.1.71037.134.6484858-10'));
15+
self::assertFalse($certidao->validateFormat('434546.02.55.2019.1.71037.134.6484858'));
16+
}
17+
}

tests/Formats/CnpjTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Tests\Formats;
4+
5+
use geekcom\ValidatorDocs\Formats\Cnpj;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class CnpjTest extends TestCase
9+
{
10+
public function testValidateFormat()
11+
{
12+
$cnpjFormat = new Cnpj();
13+
14+
self::assertTrue($cnpjFormat->validateFormat('63.657.343/0001-43'));
15+
self::assertFalse($cnpjFormat->validateFormat('63.657.343/0001'));
16+
}
17+
}

tests/Formats/CpfCnpjTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Tests\Formats;
4+
5+
use geekcom\ValidatorDocs\Formats\CpfCnpj;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class CpfCnpjTest extends TestCase
9+
{
10+
public function testValidateFormat()
11+
{
12+
$instance = new CpfCnpj();
13+
14+
self::assertTrue($instance->validateFormat('111.111.111-11'));
15+
self::assertTrue($instance->validateFormat('63.657.343/0001-43'));
16+
self::assertFalse($instance->validateFormat('11111111111'));
17+
self::assertFalse($instance->validateFormat('63.657.343/0001'));
18+
}
19+
}

tests/Formats/CpfTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Tests\Formats;
4+
5+
use geekcom\ValidatorDocs\Formats\Cpf;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class CpfTest extends TestCase
9+
{
10+
private Cpf $cpf;
11+
12+
protected function setUp(): void
13+
{
14+
$this->cpf = new Cpf();
15+
}
16+
17+
public function testIsFormatValidationWorksWell(): void
18+
{
19+
self::assertTrue($this->cpf->validateFormat('111.111.111-11'));
20+
self::assertFalse($this->cpf->validateFormat('11111111111'));
21+
}
22+
}

tests/Formats/NisTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Tests\Formats;
4+
5+
use geekcom\ValidatorDocs\Formats\Nis;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class NisTest extends TestCase
9+
{
10+
public function testValidateFormat()
11+
{
12+
$nis = new Nis();
13+
14+
self::assertTrue($nis->validateFormat('201.73374.34-9'));
15+
}
16+
}

0 commit comments

Comments
 (0)