From 7247b2ac6742d9b454d1010faba175f7961fc57c Mon Sep 17 00:00:00 2001 From: Anton J Conradie Date: Mon, 14 Apr 2025 22:59:59 -0300 Subject: [PATCH 1/2] fix CPF validation --- src/validator-docs/Rules/Cpf.php | 14 ++++++++++++++ src/validator-docs/Validator.php | 9 --------- tests/TestValidator.php | 7 +++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/validator-docs/Rules/Cpf.php b/src/validator-docs/Rules/Cpf.php index 0a28d36..2bab0c7 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 validateFormat($value, $document, $attribute = null) + { + if (!empty($value)) { + return (new ValidatorFormats())->execute($value, $document); + } + } + public function validateCpf($attribute, $value): bool { + + if (!$this->validateFormat($value, 'cpf')) { + 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 **/ From 2899ae68103f6e223371bcb5764adcc7d8ac19a2 Mon Sep 17 00:00:00 2001 From: Anton J Conradie Date: Wed, 30 Apr 2025 00:14:21 -0300 Subject: [PATCH 2/2] changes per discussion --- src/validator-docs/Rules/Cpf.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/validator-docs/Rules/Cpf.php b/src/validator-docs/Rules/Cpf.php index 2bab0c7..1175a2a 100644 --- a/src/validator-docs/Rules/Cpf.php +++ b/src/validator-docs/Rules/Cpf.php @@ -11,17 +11,17 @@ final class Cpf extends Sanitization { - protected function validateFormat($value, $document, $attribute = null) + protected function validateCPFFormat(string $value) { if (!empty($value)) { - return (new ValidatorFormats())->execute($value, $document); + return (new ValidatorFormats())->execute($value, 'cpf'); } } public function validateCpf($attribute, $value): bool { - if (!$this->validateFormat($value, 'cpf')) { + if (!$this->validateCPFFormat($value)) { return false; }