Cpf::getMessages()   A
last analyzed

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
namespace CpfCnpjValidation;
3
4
use Zend\Validator\ValidatorInterface;
5
6
class Cpf implements ValidatorInterface
7
{
8
9
    private $invalidCpf = [
10
        '00000000000',
11
        '11111111111',
12
        '22222222222',
13
        '33333333333',
14
        '44444444444',
15
        '55555555555',
16
        '66666666666',
17
        '77777777777',
18
        '88888888888',
19
        '99999999999'
20
    ];
21
22
    /**
23
     * Returns true if and only if $value meets the validation requirements
24
     *
25
     * If $value fails validation, then this method returns false, and
26
     * getMessages() will return an array of messages that explain why the
27
     * validation failed.
28
     *
29
     * @param mixed $value            
30
     * @return bool
31
     * @throws Exception\RuntimeException If validation of $value is impossible
32
     */
33 5
    public function isValid($value)
34
    {
35 5
        if (empty($value)) {
36 1
            return false;
37
        }
38
        
39
        // Elimina possivel mascara
40 4
        $cpf = preg_replace('/[^0-9]/', '', (string) $value);
41
        
42 4
        if (strlen($cpf) > 11) {
43 1
            return false;
44
        }
45 3
        $cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);
46
        
47 3
        if (in_array($cpf, $this->invalidCpf)) {
48 1
            return false;
49
        }
50
        
51 2
        for ($t = 9; $t < 11; $t ++) {
52 2
            for ($d = 0, $c = 0; $c < $t; $c ++) {
53 2
                $d += $cpf{$c} * (($t + 1) - $c);
54
            }
55 2
            $d = ((10 * $d) % 11) % 10;
56 2
            if ($cpf{$c} != $d) {
57 1
                return false;
58
            }
59
        }
60 1
        return true;
61
    }
62
63
    /**
64
     * Returns an array of messages that explain why the most recent isValid()
65
     * call returned false.
66
     * The array keys are validation failure message identifiers,
67
     * and the array values are the corresponding human-readable message strings.
68
     *
69
     * If isValid() was never called or if the most recent isValid() call
70
     * returned true, then this method returns an empty array.
71
     *
72
     * @return array
73
     */
74 1
    public function getMessages()
75
    {
76 1
        return [];
77
    }
78
}
79