Skip to content

feat: valida formato se doc é informado #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/proposing-changes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Setup PHP with Xdebug
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
coverage: xdebug

- name: Validate composer.json and composer.lock
run: composer validate

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ composer.lock
.idea/
/tests/log/
*.cache
docker-compose.yml
.phpcs-cache
.phpunit.result.cache
8 changes: 8 additions & 0 deletions src/validator-docs/Contracts/ValidatorFormats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace geekcom\ValidatorDocs\Contracts;

interface ValidatorFormats
{
public static function validateFormat(string $value): bool;
}
16 changes: 16 additions & 0 deletions src/validator-docs/Formats/Certidao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace geekcom\ValidatorDocs\Formats;

use geekcom\ValidatorDocs\Contracts\ValidatorFormats;

class Certidao implements ValidatorFormats
{
public static function validateFormat(string $value): bool
{
return preg_match(
'/^\d{6}[. ]\d{2}[. ]\d{2}[. ]\d{4}[. ]\d{1}[. ]\d{5}[. ]\d{3}[. ]\d{7}[- ]\d{2}$/',
$value
) > 0;
}
}
11 changes: 11 additions & 0 deletions src/validator-docs/Formats/Cnpj.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace geekcom\ValidatorDocs\Formats;

class Cnpj implements \geekcom\ValidatorDocs\Contracts\ValidatorFormats
{
public static function validateFormat(string $value): bool
{
return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $value) > 0;
}
}
13 changes: 13 additions & 0 deletions src/validator-docs/Formats/Cpf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace geekcom\ValidatorDocs\Formats;

use geekcom\ValidatorDocs\Contracts\ValidatorFormats;

class Cpf implements ValidatorFormats
{
public static function validateFormat(string $value): bool
{
return preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/', $value) > 0;
}
}
16 changes: 16 additions & 0 deletions src/validator-docs/Formats/CpfCnpj.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace geekcom\ValidatorDocs\Formats;

use geekcom\ValidatorDocs\Contracts\ValidatorFormats;

class CpfCnpj implements ValidatorFormats
{
public static function validateFormat(string $value): bool
{
$cpf = new Cpf();
$cnpj = new Cnpj();

return $cpf->validateFormat($value) || $cnpj->validateFormat($value);
}
}
14 changes: 14 additions & 0 deletions src/validator-docs/Formats/Nis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace geekcom\ValidatorDocs\Formats;

use geekcom\ValidatorDocs\Contracts\ValidatorFormats;

class Nis implements ValidatorFormats
{

public static function validateFormat(string $value): bool
{
return preg_match('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/', $value) > 0;
}
}
69 changes: 28 additions & 41 deletions src/validator-docs/Validator.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,47 @@
<?php

declare(strict_types=1);

namespace geekcom\ValidatorDocs;

use geekcom\ValidatorDocs\Rules\Ddd;
use geekcom\ValidatorDocs\Rules\{Certidao,
Cnh,
Cnpj,
Cns,
Cpf,
Ddd,
InscricaoEstadual,
Nis,
Placa,
Renavam,
TituloEleitoral};
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Validation\Validator as BaseValidator;
use geekcom\ValidatorDocs\Rules\TituloEleitoral;
use geekcom\ValidatorDocs\Rules\Cns;
use geekcom\ValidatorDocs\Rules\Nis;
use geekcom\ValidatorDocs\Rules\Cpf;
use geekcom\ValidatorDocs\Rules\Cnpj;
use geekcom\ValidatorDocs\Rules\Cnh;
use geekcom\ValidatorDocs\Rules\Certidao;
use geekcom\ValidatorDocs\Rules\InscricaoEstadual;
use geekcom\ValidatorDocs\Rules\Placa;
use geekcom\ValidatorDocs\Rules\Renavam;

use function preg_match;

/**
*
* @author Daniel Rodrigues Lima
* @email [email protected]
*/

class Validator extends BaseValidator
{
protected function validateFormatoCpf($attribute, $value): bool
{
return preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/', $value) > 0;
public function __construct(
Translator $translator,
ValidatorFormats $formatValidator,
array $data,
array $rules,
array $messages = [],
array $customAttributes = []
) {
parent::__construct($translator, $data, $rules, $messages, $customAttributes);
}

protected function validateFormatoCnpj($attribute, $value): bool
protected function validateFormat($value, $document, $attribute = null)
{
return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $value) > 0;
}

protected function validateFormatoCpfCnpj($attribute, $value): bool
{
return $this->validateFormatoCpf($attribute, $value) || $this->validateFormatoCnpj($attribute, $value);
}

protected function validateFormatoNis($attribute, $value): bool
{
return preg_match('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/', $value) > 0;
}

protected function validateFormatoCertidao($attribute, $value): bool
{
return preg_match('/^\d{6}[. ]\d{2}[. ]\d{2}[. ]\d{4}[. ]\d{1}[. ]\d{5}[. ]\d{3}[. ]\d{7}[- ]\d{2}$/', $value) > 0;
if (!empty($value)) {
return (new ValidatorFormats())->execute($value, $document);
}
}

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

$this->validateFormat($value, 'cpf');

return $cpf->validateCpf($attribute, $value);
}

Expand Down
28 changes: 28 additions & 0 deletions src/validator-docs/ValidatorFormats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace geekcom\ValidatorDocs;

use geekcom\ValidatorDocs\Contracts\ValidatorFormats as Contract;
use Exception;

class ValidatorFormats
{
private const STRATEGY_NAMESPACE = 'geekcom\ValidatorDocs\Formats\%s';

public function execute(string $value, string $document): bool
{
if (!$value) {
throw new Exception('Value not informed.');
}

$validator = sprintf(self::STRATEGY_NAMESPACE, ucfirst($document));
if (
class_exists($validator)
&& new $validator() instanceof Contract
) {
return $validator::validateFormat($value);
}

throw new Exception('Don\'t exists validator for this document.');
}
}
13 changes: 9 additions & 4 deletions src/validator-docs/ValidatorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ public function boot()
{
$me = $this;

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

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

return new Validator($translator, $validatorFormats, $data, $rules, $messages, $attributes);
}
);
}

protected function getMessages()
Expand Down
17 changes: 17 additions & 0 deletions tests/Formats/CertidaoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace geekcom\ValidatorDocs\Tests\Formats;

use geekcom\ValidatorDocs\Formats\Certidao;
use PHPUnit\Framework\TestCase;

class CertidaoTest extends TestCase
{
public function testValidateFormat()
{
$certidao = new Certidao();

self::assertTrue($certidao->validateFormat('434546.02.55.2019.1.71037.134.6484858-10'));
self::assertFalse($certidao->validateFormat('434546.02.55.2019.1.71037.134.6484858'));
}
}
17 changes: 17 additions & 0 deletions tests/Formats/CnpjTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace geekcom\ValidatorDocs\Tests\Formats;

use geekcom\ValidatorDocs\Formats\Cnpj;
use PHPUnit\Framework\TestCase;

class CnpjTest extends TestCase
{
public function testValidateFormat()
{
$cnpjFormat = new Cnpj();

self::assertTrue($cnpjFormat->validateFormat('63.657.343/0001-43'));
self::assertFalse($cnpjFormat->validateFormat('63.657.343/0001'));
}
}
19 changes: 19 additions & 0 deletions tests/Formats/CpfCnpjTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace geekcom\ValidatorDocs\Tests\Formats;

use geekcom\ValidatorDocs\Formats\CpfCnpj;
use PHPUnit\Framework\TestCase;

class CpfCnpjTest extends TestCase
{
public function testValidateFormat()
{
$instance = new CpfCnpj();

self::assertTrue($instance->validateFormat('111.111.111-11'));
self::assertTrue($instance->validateFormat('63.657.343/0001-43'));
self::assertFalse($instance->validateFormat('11111111111'));
self::assertFalse($instance->validateFormat('63.657.343/0001'));
}
}
22 changes: 22 additions & 0 deletions tests/Formats/CpfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace geekcom\ValidatorDocs\Tests\Formats;

use geekcom\ValidatorDocs\Formats\Cpf;
use PHPUnit\Framework\TestCase;

class CpfTest extends TestCase
{
private Cpf $cpf;

protected function setUp(): void
{
$this->cpf = new Cpf();
}

public function testIsFormatValidationWorksWell(): void
{
self::assertTrue($this->cpf->validateFormat('111.111.111-11'));
self::assertFalse($this->cpf->validateFormat('11111111111'));
}
}
16 changes: 16 additions & 0 deletions tests/Formats/NisTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace geekcom\ValidatorDocs\Tests\Formats;

use geekcom\ValidatorDocs\Formats\Nis;
use PHPUnit\Framework\TestCase;

class NisTest extends TestCase
{
public function testValidateFormat()
{
$nis = new Nis();

self::assertTrue($nis->validateFormat('201.73374.34-9'));
}
}
Loading