PartitaIva::isValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of PartitaIva.
5
 *
6
 * (c) Corrado Ronci <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace sorciulus\PartitaIva;
13
14
/**
15
* Partita Iva Class
16
*/
17
class PartitaIva
18
{
19
    /**
20
     * partita iva
21
     * 
22
     * @var string
23
     */
24
    private $partitaIva;
25
26
    /**
27
     * @var boolean
28
     */
29
    private $isValid = false;
30
31
    /**
32
     * Sets the value of partitaIva.
33
     *
34
     * @param mixed $partitaIva the partita iva
35
     *
36
     * @return self
37
     */
38
    public function setPartitaIva($partitaIva)
39
    {
40
        $this->setIsValid(false);
41
        $this->partitaIva = $partitaIva;
42
        $this->check();
43
        return $this;
44
    } 
45
46
    /**
47
     * Sets the value of isValid.
48
     *
49
     * @param bool $isValid the is valid
50
     *
51
     * @return self
52
     */
53
    private function setIsValid(bool $isValid)
54
    {
55
        $this->isValid = $isValid;
56
	
57
        return $this;
58
    } 
59
60
    /**
61
     * Gets the value of partitaIva.
62
     *
63
     * @return mixed
64
     */
65
    public function getPartitaIva()
66
    {
67
        return $this->partitaIva;
68
    } 
69
70
    /**
71
     * Gets the value of isValid.
72
     *
73
     * @return bool
74
     */
75
    public function isValid()
76
    {
77
        return $this->isValid;
78
    }
79
80
    /**
81
     * check if partita iva format is correct
82
     * 
83
     * @return void 
84
     */
85
    private function check()
86
    {    	
87
        $partitaIva = $this->getPartitaIva();
88
        if ($partitaIva === "" || strlen($partitaIva) != 11 || (preg_match("/^[0-9]+\$/", $partitaIva) != 1)) {
89
            $valid = false; 
90
        } else {    		
91
            $s = 0;
92
            for ($i = 0; $i <= 9; $i += 2) {
93
                $s += ord($partitaIva[$i]) - ord("0");
94
            }
95
96
            for ($i = 1; $i <= 9; $i += 2) {
97
                $c = 2 * ( ord($partitaIva[$i]) - ord("0") );
98
                if ($c > 9) {
99
                    $c = $c - 9;
100
                }
101
                $s += $c;
102
            }
103
104
            if (((10 - $s%10)%10) !== (ord($partitaIva[10]) - ord("0"))) {
105
                $valid = false;
106
            }
107
108
            if (!preg_match("/^0([0-9][1-9]|[1-9][0-9])|100|120|121|888|999/",substr($partitaIva,7,3))) {
109
                $valid = false;
110
            }
111
112
            if (!isset($valid)) {
113
                $valid = true;
114
            }
115
        }
116
        $this->setIsValid($valid); 
117
    }
118
}