Completed
Push — master ( 9de2ae...02afd8 )
by Ronan
06:45
created

Cif::validate()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 31
rs 8.439
cc 6
eloc 18
nc 6
nop 1
1
<?php
2
3
namespace IsoCodes;
4
5
/**
6
 * Class Nif.
7
 *
8
 * El Código de identificación fiscal ('CIF') ha sido hasta 2008 el nombre del sistema de identificación
9
 * tributaria utilizada en España para las personas jurídicas o entidades en general
10
 * según regula el Decreto 2423/1975, de 25 de septiembre.
11
 *
12
 * @source  https://github.com/alrik11es/spanish-utils
13
 *
14
 * @link    http://es.wikipedia.org/wiki/C%C3%B3digo_de_identificaci%C3%B3n_fiscal
15
 */
16
class Cif implements IsoCodeInterface
17
{
18
    /**
19
     * CIF validation.
20
     *
21
     * @param string $cif The CIF
22
     *
23
     * @return bool
24
     */
25
    public static function validate($cif)
26
    {
27
        $cifCodes = 'JABCDEFGHI';
28
29
        if (9 !== strlen($cif)) {
30
            return false;
31
        }
32
        $cif = strtoupper(trim($cif));
33
        $sum = (string) Nif::getCifSum($cif);
34
35
        $n = (10 - substr($sum, -1)) % 10;
36
37
        if (preg_match('/^[ABCDEFGHJKNPQRSUVW]{1}/', $cif)) {
38
            if (in_array($cif[0], array('A', 'B', 'E', 'H'))) {
39
                // Numerico
40
                return $cif[8] == $n;
41
            } elseif (in_array($cif[0], array('K', 'P', 'Q', 'S'))) {
42
                // Letras
43
                return $cif[8] == $cifCodes[$n];
44
            } else {
45
                // Alfanumérico
46
                if (is_numeric($cif[8])) {
47
                    return $cif[8] == $n;
48
                } else {
49
                    return $cif[8] == $cifCodes[$n];
50
                }
51
            }
52
        }
53
54
        return false;
55
    }
56
}
57