Cpf::toDatabase()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Validate;
4
5
use Validate\Traits\GetDataTrait;
6
7
class Cpf implements \Validate\Contracts\Validate
8
{
9
    use GetDataTrait;
10
11
    /**
12
     * Get User name from user and convert to Database
13
     *
14
     * @param  string $cpf
15
     * @return string
16
     */
17
    public static function toDatabase($cpf)
18
    {
19
        $cpf = preg_replace("/[^0-9]/", "", $cpf);
20
        return str_pad($cpf, 11, '0', STR_PAD_LEFT);
21
    }
22
    
23
    /**
24
     * Get User name from database and convert to User Format
25
     *
26
     * @param  string $cpf
27
     * @return string
28
     */
29
    public static function toUser($cpf)
30
    {
31
        return $cpf;
32
    }
33
34
    /**
35
     * Validate if Cpf is Valid
36
     *
37
     * @param  string $cpf
38
     * @return bool
39
     */
40
    public static function validate($cpf)
41
    {
42
        // Delete not Numbers || Elimina possivel mascara
43
        $cpf = self::toDatabase($cpf);
44
        
45
        // Verify Digit not can be 11 || Verifica se o numero de digitos informados é igual a 11 
46
        if (strlen($cpf) != 11) {
47
            return false;
48
        }
49
        
50
        // Block Black Cpf's
51
        if (static::foundInFile($cpf, 'black-cpf')) {
52
            return false;
53
        }
54
        
55
         // Verify if Cpf is Valid || Calcula os digitos verificadores para verificar se o
56
         // CPF é válido
57
        for ($t = 9; $t < 11; $t++) {
58
            
59
            for ($d = 0, $c = 0; $c < $t; $c++) {
60
                $d += $cpf{$c} * (($t + 1) - $c);
61
            }
62
            $d = ((10 * $d) % 11) % 10;
63
            if ($cpf{$c} != $d) {
64
                return false;
65
            }
66
        }
67
68
        return true;
69
    }
70
71
    /**
72
     * Verify if Cpf is the same
73
     *
74
     * @param  string $to
75
     * @param  string $from
76
     * @return boolean
77
     */
78
    public static function isSame(string $to, string $from)
79
    {
80
        return (self::toDatabase($to)===self::toDatabase($from));
81
    }
82
83
}
84