1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IbanGenerator; |
4
|
|
|
|
5
|
|
|
use IbanGenerator\Bban\BbanInterface; |
6
|
|
|
use IbanGenerator\Bban\SpainBban; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
class Iban |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var BbanInterface |
13
|
|
|
*/ |
14
|
|
|
private $bban; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $countryCode; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $checkDigits; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private static $countriesSupported = [ |
30
|
|
|
'ES' => SpainBban::class, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Iban constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $countryCode |
37
|
|
|
* @param string $checkDigits |
38
|
|
|
* @param BbanInterface $bban |
39
|
|
|
* |
40
|
|
|
* @throws InvalidArgumentException |
41
|
|
|
*/ |
42
|
|
|
public function __construct($countryCode, $checkDigits, BbanInterface $bban) |
43
|
|
|
{ |
44
|
|
|
$countryCode = strtoupper($countryCode); |
45
|
|
|
static::validateCountryCodeFormat($countryCode); |
|
|
|
|
46
|
|
|
static::validateCheckDigitsFormat($checkDigits); |
|
|
|
|
47
|
|
|
static::validateControlDigit($countryCode, $checkDigits, $bban); |
|
|
|
|
48
|
|
|
$this->countryCode = $countryCode; |
49
|
|
|
$this->checkDigits = $checkDigits; |
50
|
|
|
$this->bban = $bban; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $iban |
55
|
|
|
* |
56
|
|
|
* @throws InvalidArgumentException |
57
|
|
|
* |
58
|
|
|
* @return static |
59
|
|
|
*/ |
60
|
|
|
public static function fromString($iban) |
61
|
|
|
{ |
62
|
|
|
if (! preg_match('/^[0-9A-Z]{16,34}$/', $iban)) { |
63
|
|
|
throw new InvalidArgumentException('Iban should be between 16 and 34 characters'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$countryCode = substr($iban, 0, 2); |
67
|
|
|
$checkDigits = substr($iban, 2, 2); |
68
|
|
|
$bbanString = substr($iban, 4); |
69
|
|
|
|
70
|
|
|
if (! array_key_exists($countryCode, self::$countriesSupported)) { |
71
|
|
|
throw new InvalidArgumentException( |
72
|
|
|
sprintf( |
73
|
|
|
'The country code %s is not supported', |
74
|
|
|
$countryCode |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
$bbanClass = self::$countriesSupported[$countryCode]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var BbanInterface |
82
|
|
|
*/ |
83
|
|
|
$bban = $bbanClass::fromString($bbanString); |
84
|
|
|
|
85
|
|
|
return new static($countryCode, $checkDigits, $bban); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function countryCode() |
92
|
|
|
{ |
93
|
|
|
return $this->countryCode; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function ibanCheckDigits() |
100
|
|
|
{ |
101
|
|
|
return $this->checkDigits; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function bankCode() |
108
|
|
|
{ |
109
|
|
|
return $this->bban->bankCode(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function branchCode() |
116
|
|
|
{ |
117
|
|
|
return $this->bban->branchCode(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function countryCheckDigits() |
124
|
|
|
{ |
125
|
|
|
return $this->bban->checkDigits(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function accountNumber() |
132
|
|
|
{ |
133
|
|
|
return $this->bban->accountNumber(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function __toString() |
140
|
|
|
{ |
141
|
|
|
$bbanString = $this->bban; |
142
|
|
|
|
143
|
|
|
return $this->countryCode . $this->checkDigits . $bbanString; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param $countryCode |
148
|
|
|
* |
149
|
|
|
* @throws InvalidArgumentException |
150
|
|
|
*/ |
151
|
|
|
private static function validateCountryCodeFormat($countryCode) |
152
|
|
|
{ |
153
|
|
|
if (! preg_match('/^[A-Z]{2}$/', $countryCode)) { |
154
|
|
|
throw new InvalidArgumentException('The country code should be 2 letters'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $checkDigits |
160
|
|
|
* |
161
|
|
|
* @throws InvalidArgumentException |
162
|
|
|
*/ |
163
|
|
|
private static function validateCheckDigitsFormat($checkDigits) |
164
|
|
|
{ |
165
|
|
|
if (! preg_match('/^[\d]{2}$/', $checkDigits)) { |
166
|
|
|
throw new InvalidArgumentException('The IBAN checksum must be 2 numeric characters'); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $countryCode |
172
|
|
|
* @param string $checkDigits |
173
|
|
|
* @param BbanInterface $bban |
174
|
|
|
* |
175
|
|
|
* @throws InvalidArgumentException |
176
|
|
|
*/ |
177
|
|
|
private static function validateControlDigit( |
178
|
|
|
$countryCode, |
179
|
|
|
$checkDigits, |
180
|
|
|
BbanInterface $bban |
181
|
|
|
) { |
182
|
|
|
$rearranged = strval($bban) . $countryCode . $checkDigits; |
183
|
|
|
$digitsList = str_split($rearranged); |
184
|
|
|
|
185
|
|
|
$digitsList = array_map(['self', 'digitToInt'], $digitsList); |
186
|
|
|
$stringToCompute = implode('', $digitsList); |
187
|
|
|
|
188
|
|
|
if (bcmod($stringToCompute, '97') !== '1') { |
189
|
|
|
throw new InvalidArgumentException('The IBAN checksum digits are not valid'); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string $value |
195
|
|
|
* |
196
|
|
|
* @return int |
197
|
|
|
*/ |
198
|
|
|
private static function digitToInt($value) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
if (is_numeric($value)) { |
201
|
|
|
return intval($value); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return ord($value) - 55; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: