Skip to content

Commit 7247b2a

Browse files
committed
fix CPF validation
1 parent af568cb commit 7247b2a

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/validator-docs/Rules/Cpf.php

+14
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,27 @@
44

55
namespace geekcom\ValidatorDocs\Rules;
66

7+
use geekcom\ValidatorDocs\ValidatorFormats;
8+
79
use function preg_match;
810
use function mb_strlen;
911

1012
final class Cpf extends Sanitization
1113
{
14+
protected function validateFormat($value, $document, $attribute = null)
15+
{
16+
if (!empty($value)) {
17+
return (new ValidatorFormats())->execute($value, $document);
18+
}
19+
}
20+
1221
public function validateCpf($attribute, $value): bool
1322
{
23+
24+
if (!$this->validateFormat($value, 'cpf')) {
25+
return false;
26+
}
27+
1428
$c = $this->sanitize($value);
1529

1630
if (mb_strlen($c) != 11 || preg_match("/^{$c[0]}{11}$/", $c)) {

src/validator-docs/Validator.php

-9
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,10 @@ public function __construct(
3030
parent::__construct($translator, $data, $rules, $messages, $customAttributes);
3131
}
3232

33-
protected function validateFormat($value, $document, $attribute = null)
34-
{
35-
if (!empty($value)) {
36-
return (new ValidatorFormats())->execute($value, $document);
37-
}
38-
}
39-
4033
protected function validateCpf($attribute, $value): bool
4134
{
4235
$cpf = new Cpf();
4336

44-
$this->validateFormat($value, 'cpf');
45-
4637
return $cpf->validateCpf($attribute, $value);
4738
}
4839

tests/TestValidator.php

+7
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@ public function cpf()
1919
['errado' => 'cpf']
2020
);
2121

22+
$incorrectWithAlpha = Validator::make(
23+
['errado' => '094.050.986-59ABCDEF'],
24+
['errado' => 'cpf']
25+
);
26+
2227
$this->assertTrue($correct->passes());
2328

2429
$this->assertTrue($incorrect->fails());
30+
31+
$this->assertTrue($incorrectWithAlpha->fails());
2532
}
2633

2734
/** @test **/

0 commit comments

Comments
 (0)