Test Setup Failed
Pull Request — master (#32)
by
unknown
04:55
created

Cpf::getMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types = 1);
3
namespace CpfCnpjValidation;
4
5
use Zend\Validator\AbstractValidator;
6
7
class Cpf extends AbstractValidator
8
{
9
10
    private $invalidCpf = [
11
        '00000000000',
12
        '11111111111',
13
        '22222222222',
14
        '33333333333',
15
        '44444444444',
16
        '55555555555',
17
        '66666666666',
18
        '77777777777',
19
        '88888888888',
20
        '99999999999'
21
    ];
22
23 5
    public function isValid($value)
24
    {
25 5
        if (empty($value)) {
26 1
            return false;
27
        }
28
29
        // Elimina possivel mascara
30 4
        $cpf = preg_replace('/[^0-9]/', '', (string) $value);
31
32 4
        if (strlen($cpf) > 11) {
33 1
            return false;
34
        }
35 3
        $cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);
36
37 3
        if (in_array($cpf, $this->invalidCpf)) {
38 1
            return false;
39
        }
40
41 2
        for ($t = 9; $t < 11; $t ++) {
42 2
            for ($d = 0, $c = 0; $c < $t; $c ++) {
43 2
                $d += $cpf{$c} * (($t + 1) - $c);
44
            }
45 2
            $d = ((10 * $d) % 11) % 10;
46 2
            if ($cpf{$c} != $d) {
47 1
                return false;
48
            }
49
        }
50 1
        return true;
51
    }
52
}
53