Completed
Pull Request — master (#6)
by
unknown
03:15
created

IBAN::getCountry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Z38\SwissPayment;
4
5
/**
6
 * IBAN
7
 */
8
class IBAN
9
{
10
    const PATTERN = '/^[A-Z]{2,2}[0-9]{2,2}[A-Z0-9]{1,30}$/';
11
12
    /**
13
     * @var string
14
     */
15
    protected $iban;
16
    /**
17
     * @var boolean
18
     */
19
    private $validIban = false;
20
21
    /**
22
     * Constructor
23
     *
24
     * @param string $iban
25
     *
26
     * @throws \InvalidArgumentException When the IBAN does contain invalid characters or the checksum calculation fails.
27
     */
28 9
    public function __construct($iban)
29
    {
30 9
        $cleanedIban = str_replace(' ', '', strtoupper($iban));
31 9
        if (preg_match(self::PATTERN, $cleanedIban) && self::check($cleanedIban)) {
32 6
            $this->validIban = true;
33 6
        }
34
35 9
        $this->iban = $cleanedIban;
36 9
    }
37
38
    /**
39
     * Format the IBAN either in a human-readable manner
40
     *
41
     * @return string The formatted IBAN
42
     */
43 1
    public function format()
44
    {
45 1
        $parts = str_split($this->iban, 4);
46
47 1
        return implode(' ', $parts);
48
    }
49
50
    /**
51
     * Normalize the IBAN
52
     *
53
     * @return string The normalized IBAN
54
     */
55 3
    public function normalize()
56
    {
57 3
        return $this->iban;
58
    }
59
60
    /**
61
     * Gets the country
62
     *
63
     * @return string A ISO 3166-1 alpha-2 country code
64
     */
65 6
    public function getCountry()
66
    {
67 6
        return substr($this->iban, 0, 2);
68
    }
69
70
    /**
71
     * Checks whether the checksum of an IBAN is correct
72
     *
73
     * @param string $iban
74
     *
75
     * @return bool true if checksum is correct, false otherwise
76
     */
77 4
    protected static function check($iban)
78
    {
79 4
        $chars = str_split(substr($iban, 4).substr($iban, 0, 4));
80 4
        $length = count($chars);
81 4
        for ($i = 0; $i < $length; $i++) {
82 4
            $code = ord($chars[$i]);
83 4
            if ($code >= 65 && $code <= 90) { // A-Z
84 4
                $chars[$i] = $code - 65 + 10;
85 4
            }
86 4
        }
87 4
        $prepared = implode($chars);
88
89 4
        $r = '';
90 4
        $rLength = 0;
91 4
        $i = 0;
92 4
        $length = strlen($prepared);
93 4
        while ($i < $length) {
94 4
            $d = $r.substr($prepared, $i, 9 - $rLength);
95 4
            $i += 9 - $rLength;
96 4
            $r = $d % 97;
97 4
            $rLength = 1 + ($r >= 10);
98 4
        }
99
100 4
        return ($r == 1);
101
    }
102
103
    /**
104
     * Returns a string representation.
105
     *
106
     * @return string The string representation.
107
     */
108 3
    public function __toString()
109
    {
110 3
        return $this->format();
111
    }
112
113 2
    public function getElementName()
114
    {
115 2
        if ($this->isValidIban()) {
116 2
            return 'IBAN';
117
        }
118
119
        return 'Othr';
120
    }
121
122
    /**
123
     * @return boolean
124
     */
125 2
    public function isValidIban()
126
    {
127 2
        return $this->validIban;
128
    }
129
}
130