1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IbanGenerator; |
4
|
|
|
|
5
|
|
|
use IbanGenerator\Bban; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
class Iban |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Bban\BbanInterface |
12
|
|
|
*/ |
13
|
|
|
private $bban; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $countryCode; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $checkDigits; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $countriesSupported = [ |
29
|
|
|
'ES' => Bban\SpainBban::class, |
30
|
|
|
'AD' => Bban\AndorraBban::class, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Iban constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $countryCode |
37
|
|
|
* @param string $checkDigits |
38
|
|
|
* @param Bban\BbanInterface $bban |
39
|
|
|
* |
40
|
|
|
* @throws InvalidArgumentException |
41
|
|
|
*/ |
42
|
46 |
|
public function __construct($countryCode, $checkDigits, Bban\BbanInterface $bban) |
43
|
|
|
{ |
44
|
46 |
|
$countryCode = strtoupper($countryCode); |
45
|
46 |
|
self::validateCountryCodeFormat($countryCode); |
46
|
42 |
|
self::validateCheckDigitsFormat($checkDigits); |
47
|
38 |
|
self::validateControlDigit($countryCode, $checkDigits, $bban); |
48
|
36 |
|
$this->countryCode = $countryCode; |
49
|
36 |
|
$this->checkDigits = $checkDigits; |
50
|
36 |
|
$this->bban = $bban; |
51
|
36 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $iban |
55
|
|
|
* |
56
|
|
|
* @throws InvalidArgumentException |
57
|
|
|
* |
58
|
|
|
* @return static |
59
|
|
|
*/ |
60
|
16 |
|
public static function fromString($iban) |
61
|
|
|
{ |
62
|
16 |
|
$iban = preg_replace('/[^0-9a-zA-Z]+/', '', $iban); |
63
|
|
|
|
64
|
16 |
|
if (! preg_match('/^[0-9a-zA-Z]{16,34}$/', $iban)) { |
65
|
3 |
|
throw new InvalidArgumentException('Iban should be between 16 and 34 characters'); |
66
|
|
|
} |
67
|
|
|
|
68
|
13 |
|
$countryCode = strtoupper(substr($iban, 0, 2)); |
69
|
13 |
|
$checkDigits = strtoupper(substr($iban, 2, 2)); |
70
|
13 |
|
$bbanString = strtoupper(substr($iban, 4)); |
71
|
|
|
|
72
|
13 |
|
self::validateSupportedCountry($countryCode); |
73
|
12 |
|
$bbanClass = self::$countriesSupported[$countryCode]; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var Bban\BbanInterface |
77
|
|
|
*/ |
78
|
12 |
|
$bban = $bbanClass::fromString($bbanString); |
79
|
|
|
|
80
|
12 |
|
return new static($countryCode, $checkDigits, $bban); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Bban\BbanInterface $bban |
85
|
|
|
* @param string $countryCode |
86
|
|
|
* |
87
|
|
|
* @throws InvalidArgumentException |
88
|
|
|
* |
89
|
|
|
* @return static |
90
|
|
|
*/ |
91
|
12 |
|
public static function fromBbanAndCountry(Bban\BbanInterface $bban, $countryCode) |
92
|
|
|
{ |
93
|
12 |
|
self::validateCountryCodeFormat($countryCode); |
94
|
12 |
|
self::validateCountryCodeFormat($countryCode); |
95
|
12 |
|
self::validateSupportedCountry($countryCode); |
96
|
|
|
|
97
|
12 |
|
$checksum = self::validateChecksum($countryCode, '00', $bban); |
98
|
12 |
|
$checkDigit = 98 - (int) $checksum; |
99
|
12 |
|
$checkDigit = str_pad($checkDigit, 2, 0, STR_PAD_LEFT); |
100
|
|
|
|
101
|
12 |
|
return new static($countryCode, $checkDigit, $bban); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
12 |
|
public function countryCode() |
108
|
|
|
{ |
109
|
12 |
|
return $this->countryCode; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
24 |
|
public function ibanCheckDigits() |
116
|
|
|
{ |
117
|
24 |
|
return $this->checkDigits; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
12 |
|
public function bankCode() |
124
|
|
|
{ |
125
|
12 |
|
return $this->bban->bankCode(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
12 |
|
public function branchCode() |
132
|
|
|
{ |
133
|
12 |
|
return $this->bban->branchCode(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
12 |
|
public function countryCheckDigits() |
140
|
|
|
{ |
141
|
12 |
|
return $this->bban->checkDigits(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
12 |
|
public function accountNumber() |
148
|
|
|
{ |
149
|
12 |
|
return $this->bban->accountNumber(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
12 |
|
public function __toString() |
156
|
|
|
{ |
157
|
12 |
|
$bbanString = $this->bban; |
158
|
|
|
|
159
|
12 |
|
return $this->countryCode . $this->checkDigits . $bbanString; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param $countryCode |
164
|
|
|
* |
165
|
|
|
* @throws InvalidArgumentException |
166
|
|
|
*/ |
167
|
46 |
|
private static function validateCountryCodeFormat($countryCode) |
168
|
|
|
{ |
169
|
46 |
|
if (! preg_match('/^[A-Z]{2}$/', $countryCode)) { |
170
|
4 |
|
throw new InvalidArgumentException('The country code should be 2 letters'); |
171
|
|
|
} |
172
|
42 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param $checkDigits |
176
|
|
|
* |
177
|
|
|
* @throws InvalidArgumentException |
178
|
|
|
*/ |
179
|
42 |
|
private static function validateCheckDigitsFormat($checkDigits) |
180
|
|
|
{ |
181
|
42 |
|
if (! preg_match('/^[\d]{2}$/', $checkDigits)) { |
182
|
4 |
|
throw new InvalidArgumentException('The IBAN checksum must be 2 numeric characters'); |
183
|
|
|
} |
184
|
38 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $countryCode |
188
|
|
|
* @param string $checkDigits |
189
|
|
|
* @param Bban\BbanInterface $bban |
190
|
|
|
* |
191
|
|
|
* @throws InvalidArgumentException |
192
|
|
|
*/ |
193
|
38 |
|
private static function validateControlDigit( |
194
|
|
|
$countryCode, |
195
|
|
|
$checkDigits, |
196
|
|
|
Bban\BbanInterface $bban |
197
|
|
|
) { |
198
|
38 |
|
$checksum = self::validateChecksum($countryCode, $checkDigits, $bban); |
199
|
|
|
|
200
|
38 |
|
if ($checksum !== '01') { |
201
|
2 |
|
throw new InvalidArgumentException('The IBAN checksum digits are not valid'); |
202
|
|
|
} |
203
|
36 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param $countryCode |
207
|
|
|
* |
208
|
|
|
* @throws InvalidArgumentException |
209
|
|
|
*/ |
210
|
25 |
|
private static function validateSupportedCountry($countryCode) |
211
|
|
|
{ |
212
|
25 |
|
if (!array_key_exists($countryCode, self::$countriesSupported)) { |
213
|
1 |
|
throw new InvalidArgumentException( |
214
|
1 |
|
sprintf( |
215
|
1 |
|
'The country code %s is not supported', |
216
|
1 |
|
$countryCode |
217
|
|
|
) |
218
|
|
|
); |
219
|
|
|
} |
220
|
24 |
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param $countryCode |
224
|
|
|
* @param $checkDigits |
225
|
|
|
* @param Bban\BbanInterface $bban |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
38 |
|
private static function validateChecksum($countryCode, $checkDigits, Bban\BbanInterface $bban) |
230
|
|
|
{ |
231
|
38 |
|
$rearranged = (string) $bban . $countryCode . $checkDigits; |
232
|
38 |
|
$digitsList = str_split($rearranged); |
233
|
|
|
|
234
|
38 |
|
$digitsList = array_map(['self', 'digitToInt'], $digitsList); |
235
|
38 |
|
$stringToCompute = implode('', $digitsList); |
236
|
|
|
|
237
|
38 |
|
$checksum = bcmod($stringToCompute, '97'); |
238
|
|
|
|
239
|
38 |
|
return str_pad($checksum, 2, 0, STR_PAD_LEFT); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $value |
244
|
|
|
* |
245
|
|
|
* @return int |
246
|
|
|
*/ |
247
|
38 |
|
private static function digitToInt($value) |
|
|
|
|
248
|
|
|
{ |
249
|
38 |
|
if (is_numeric($value)) { |
250
|
38 |
|
return (int) $value; |
251
|
|
|
} |
252
|
|
|
|
253
|
38 |
|
return ord($value) - 55; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|