-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
geekcom
merged 1 commit into
geekcom:master
from
vicentimartins:111-valida-formato-se-nulo
Jun 29, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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; | ||
geekcom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * | ||
| * @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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.