1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IbanGenerator\Bban; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class SpainBban implements BbanInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
private $bankCode; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $branchCode; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $checkDigits; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $accountNumber; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* SpainBban constructor. |
31
|
|
|
* |
32
|
|
|
* @param string $bankCode |
33
|
|
|
* @param string $branchCode |
34
|
|
|
* @param string $checkDigits |
35
|
|
|
* @param string $accountNumber |
36
|
|
|
* |
37
|
|
|
* @throws InvalidArgumentException |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
$bankCode, |
41
|
|
|
$branchCode, |
42
|
|
|
$checkDigits, |
43
|
|
|
$accountNumber |
44
|
|
|
) { |
45
|
|
|
static::validateBankCodeFormat($bankCode); |
|
|
|
|
46
|
|
|
static::validateBranchCodeFormat($branchCode); |
|
|
|
|
47
|
|
|
static::validateCheckDigitsFormat($checkDigits); |
|
|
|
|
48
|
|
|
static::validateAccountNumberFormat($accountNumber); |
|
|
|
|
49
|
|
|
static::validateControlDigit( |
|
|
|
|
50
|
|
|
$bankCode, |
51
|
|
|
$branchCode, |
52
|
|
|
$checkDigits, |
53
|
|
|
$accountNumber |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$this->bankCode = $bankCode; |
57
|
|
|
$this->branchCode = $branchCode; |
58
|
|
|
$this->checkDigits = $checkDigits; |
59
|
|
|
$this->accountNumber = $accountNumber; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $bban |
64
|
|
|
* |
65
|
|
|
* @throws InvalidArgumentException |
66
|
|
|
* |
67
|
|
|
* @return static |
68
|
|
|
*/ |
69
|
|
|
public static function fromString($bban) |
70
|
|
|
{ |
71
|
|
|
if (! preg_match('/^[\d]{20}$/', $bban)) { |
72
|
|
|
throw new InvalidArgumentException('Bban should be 20 numbers'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$bankCode = substr($bban, 0, 4); |
76
|
|
|
$branchCode = substr($bban, 4, 4); |
77
|
|
|
$checkDigits = substr($bban, 8, 2); |
78
|
|
|
$accountNumber = substr($bban, 10, 10); |
79
|
|
|
|
80
|
|
|
return new static($bankCode, $branchCode, $checkDigits, $accountNumber); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function bankCode() |
87
|
|
|
{ |
88
|
|
|
return $this->bankCode; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function branchCode() |
95
|
|
|
{ |
96
|
|
|
return $this->branchCode; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function checkDigits() |
103
|
|
|
{ |
104
|
|
|
return $this->checkDigits; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function accountNumber() |
111
|
|
|
{ |
112
|
|
|
return $this->accountNumber; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function __toString() |
119
|
|
|
{ |
120
|
|
|
return $this->bankCode . $this->branchCode . $this->checkDigits . $this->accountNumber; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $bankCode |
125
|
|
|
* |
126
|
|
|
* @throws InvalidArgumentException |
127
|
|
|
*/ |
128
|
|
|
private static function validateBankCodeFormat($bankCode) |
129
|
|
|
{ |
130
|
|
|
if (! preg_match('/^[\d]{4}$/', $bankCode)) { |
131
|
|
|
throw new InvalidArgumentException('Bank code should be 4 numbers'); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $branchCode |
137
|
|
|
* |
138
|
|
|
* @throws InvalidArgumentException |
139
|
|
|
*/ |
140
|
|
|
private static function validateBranchCodeFormat($branchCode) |
141
|
|
|
{ |
142
|
|
|
if (! preg_match('/^[\d]{4}$/', $branchCode)) { |
143
|
|
|
throw new InvalidArgumentException('Branch code should be 4 numbers'); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param $checkDigits |
149
|
|
|
* |
150
|
|
|
* @throws InvalidArgumentException |
151
|
|
|
*/ |
152
|
|
|
private static function validateCheckDigitsFormat($checkDigits) |
153
|
|
|
{ |
154
|
|
|
if (! preg_match('/^[\d]{2}$/', $checkDigits)) { |
155
|
|
|
throw new InvalidArgumentException('Check digits should be 4 numbers'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $accountNumber |
161
|
|
|
* |
162
|
|
|
* @throws InvalidArgumentException |
163
|
|
|
*/ |
164
|
|
|
private static function validateAccountNumberFormat($accountNumber) |
165
|
|
|
{ |
166
|
|
|
if (! preg_match('/^[\d]{10}$/', $accountNumber)) { |
167
|
|
|
throw new InvalidArgumentException('Account number should be 10 numbers'); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $bankCode |
173
|
|
|
* @param $branchCode |
174
|
|
|
* @param $checkDigits |
175
|
|
|
* @param $accountNumber |
176
|
|
|
* |
177
|
|
|
* @throws InvalidArgumentException |
178
|
|
|
*/ |
179
|
|
|
private static function validateControlDigit( |
180
|
|
|
$bankCode, |
181
|
|
|
$branchCode, |
182
|
|
|
$checkDigits, |
183
|
|
|
$accountNumber |
184
|
|
|
) { |
185
|
|
|
$dc = ''; |
186
|
|
|
$validations = [6, 3, 7, 9, 10, 5, 8, 4, 2, 1]; |
187
|
|
|
foreach ([$bankCode . $branchCode, $accountNumber] as $string) { |
188
|
|
|
$suma = 0; |
189
|
|
|
for ($i = 0, $len = strlen($string); $i < $len; $i++) { |
190
|
|
|
$suma += $validations[$i] * substr($string, $len - $i - 1, 1); |
191
|
|
|
} |
192
|
|
|
$digit = 11 - $suma % 11; |
193
|
|
|
if ($digit == 11) { |
194
|
|
|
$digit = 0; |
195
|
|
|
} elseif ($digit == 10) { |
196
|
|
|
$digit = 1; |
197
|
|
|
} |
198
|
|
|
$dc .= $digit; |
199
|
|
|
} |
200
|
|
|
if ($checkDigits !== $dc) { |
201
|
|
|
throw new InvalidArgumentException('Control digits are not valid'); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
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: