-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from vicentimartins/111-valida-formato-se-nulo
feat: valida formato se doc é informado
- Loading branch information
Showing
18 changed files
with
277 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ composer.lock | |
.idea/ | ||
/tests/log/ | ||
*.cache | ||
docker-compose.yml | ||
.phpcs-cache | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
Oops, something went wrong.