Cif   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 70
rs 10
c 1
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 3 1
A isValid() 0 3 1
B validateCIF() 0 30 8
A __construct() 0 5 1
1
<?php
2
3
namespace alcea\cif;
4
5
/**
6
 * Class Cif - Validation for Romanian VAT code (CIF)
7
 *
8
 * Valid format:
9
 * |XXXXXXXXX|C| - max length 10 - min length 2
10
 * |X|  - from 1 to 9 characters
11
 * |C|  - check Digit
12
 * 
13
 * ```php
14
 * use alcea\cif\Cif;
15
 *
16
 * $cifToBeValidated = '159'; // without prefix digit (RO|R)
17
 * $cif = new Cif($cifToBeValidated);
18
 * echo "CIF {$cifToBeValidated} is " . ( $cif->isValid() ? 'valid' : 'invalid' ) . PHP_EOL;
19
 *
20
 * // or
21
 *
22
 * echo "CIF {$cifToBeValidated} is " . ( Cif::validate($cifToBeValidated) ? 'valid' : 'invalid' ) . PHP_EOL;
23
 * ```
24
 * 
25
 * @property string $_cif - CIF without prefix digit (RO|R)  - $cif = preg_replace("/[^0-9]/", "", $cif);
26
 * @property boolean $_isValid
27
 * @see https://ro.wikipedia.org/wiki/Cod_de_Identificare_Fiscal%C4%83
28
 * @author Alcea Nicolae <nicu(dotta)alcea(atta)gmail(dotta)com>
29
 */
30
class Cif
31
{
32
33
    private static $controlKey = [7, 5, 3, 2, 1, 7, 5, 3, 2];
34
    private $_cif;
35
    private $_isValid;
36
37
    /**
38
     * CIF constructor.
39
     * @param string|integer $cif - without prefix digit (RO|R)
40
     */
41
    public function __construct($cif)
42
    {
43
        $this->_cif = trim($cif);
44
        $this->_isValid = false;
45
        $this->validateCIF();
46
    }
47
48
    /**
49
     * @param string|int $cif
50
     * @return bool
51
     */
52
    public static function validate($cif)
53
    {
54
        return (new static($cif))->isValid();
55
    }
56
57
    /**
58
     * 
59
     * @return boolean
60
     */
61
    public function isValid()
62
    {
63
        return $this->_isValid;
64
    }
65
66
    /**
67
     * Validate CIF
68
     * @return boolean
69
     */
70
    private function validateCIF()
71
    {
72
        $cif = $this->_cif;
73
        if (!is_numeric($cif)) {
74
            return false;
75
        }
76
        if ((int) $cif <= 0) {
77
            return false;
78
        }
79
        $cifLength = strlen($cif);
80
        if ($cifLength > 10) {
81
            return false;
82
        }
83
        if ($cifLength < 2) {
84
            return false;
85
        }
86
87
        $controlKey = (int) substr($cif, -1);
88
        $cif = substr($cif, 0, -1);
89
        $cif = str_pad($cif, 9, '0', STR_PAD_LEFT);
90
        $suma = 0;
91
        foreach (self::$controlKey as $i => $key) {
92
            $suma += $cif[$i] * $key;
93
        }
94
        $suma = $suma * 10;
95
        $rest = (int) ($suma % 11);
96
        $rest = ($rest == 10) ? 0 : $rest;
97
98
        if ($rest === $controlKey) {
99
            $this->_isValid = true;
100
        }
101
    }
102
103
}
104