diff --git a/src/validator-docs/Rules/Cpf.php b/src/validator-docs/Rules/Cpf.php index 0a28d36..1175a2a 100644 --- a/src/validator-docs/Rules/Cpf.php +++ b/src/validator-docs/Rules/Cpf.php @@ -4,13 +4,27 @@ namespace geekcom\ValidatorDocs\Rules; +use geekcom\ValidatorDocs\ValidatorFormats; + use function preg_match; use function mb_strlen; final class Cpf extends Sanitization { + protected function validateCPFFormat(string $value) + { + if (!empty($value)) { + return (new ValidatorFormats())->execute($value, 'cpf'); + } + } + public function validateCpf($attribute, $value): bool { + + if (!$this->validateCPFFormat($value)) { + return false; + } + $c = $this->sanitize($value); if (mb_strlen($c) != 11 || preg_match("/^{$c[0]}{11}$/", $c)) { diff --git a/src/validator-docs/Validator.php b/src/validator-docs/Validator.php index 84a4dd0..efb0a90 100644 --- a/src/validator-docs/Validator.php +++ b/src/validator-docs/Validator.php @@ -30,19 +30,10 @@ public function __construct( parent::__construct($translator, $data, $rules, $messages, $customAttributes); } - protected function validateFormat($value, $document, $attribute = null) - { - 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); } diff --git a/tests/TestValidator.php b/tests/TestValidator.php index 2a870b4..7506901 100644 --- a/tests/TestValidator.php +++ b/tests/TestValidator.php @@ -19,9 +19,16 @@ public function cpf() ['errado' => 'cpf'] ); + $incorrectWithAlpha = Validator::make( + ['errado' => '094.050.986-59ABCDEF'], + ['errado' => 'cpf'] + ); + $this->assertTrue($correct->passes()); $this->assertTrue($incorrect->fails()); + + $this->assertTrue($incorrectWithAlpha->fails()); } /** @test **/