|
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
|
|
|
public function getElementName() |
|
114
|
|
|
{ |
|
115
|
|
|
if ($this->isValidIban()) { |
|
116
|
|
|
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
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function asDom(\DOMDocument $doc) |
|
134
|
|
|
{ |
|
135
|
2 |
|
if ($this->isValidIban()) { |
|
136
|
2 |
|
$xml = $doc->createElement('IBAN', $this->normalize()); |
|
137
|
2 |
|
} else { |
|
138
|
|
|
$xml = $doc->createElement('Othr'); |
|
139
|
|
|
$xml->appendChild($doc->createElement('Id', $this->normalize())); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
return $xml; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|