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