1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Dimtrovich/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) 2023 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Dimtrovich\Validation\Rules; |
13
|
|
|
|
14
|
|
|
use Dimtrovich\Validation\Traits\IbanTrait; |
15
|
|
|
use Dimtrovich\Validation\Utils\Country; |
16
|
|
|
use Rakit\Validation\Rule; |
17
|
|
|
|
18
|
|
|
class Iban extends AbstractRule |
19
|
|
|
{ |
20
|
|
|
use IbanTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Character map |
24
|
|
|
* |
25
|
|
|
* @var array|int[] |
26
|
|
|
*/ |
27
|
|
|
private array $characterMap = [ |
|
|
|
|
28
|
|
|
'A' => 10, |
29
|
|
|
'B' => 11, |
30
|
|
|
'C' => 12, |
31
|
|
|
'D' => 13, |
32
|
|
|
'E' => 14, |
33
|
|
|
'F' => 15, |
34
|
|
|
'G' => 16, |
35
|
|
|
'H' => 17, |
36
|
|
|
'I' => 18, |
37
|
|
|
'J' => 19, |
38
|
|
|
'K' => 20, |
39
|
|
|
'L' => 21, |
40
|
|
|
'M' => 22, |
41
|
|
|
'N' => 23, |
42
|
|
|
'O' => 24, |
43
|
|
|
'P' => 25, |
44
|
|
|
'Q' => 26, |
45
|
|
|
'R' => 27, |
46
|
|
|
'S' => 28, |
47
|
|
|
'T' => 29, |
48
|
|
|
'U' => 30, |
49
|
|
|
'V' => 31, |
50
|
|
|
'W' => 32, |
51
|
|
|
'X' => 33, |
52
|
|
|
'Y' => 34, |
53
|
|
|
'Z' => 35, |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get country code with length. |
58
|
|
|
* |
59
|
|
|
* @var array<string, int> |
60
|
|
|
*/ |
61
|
|
|
private array $ibanLengthByCountry = [ |
|
|
|
|
62
|
|
|
Country::ALBANIA => 28, |
63
|
|
|
Country::ALGERIA => 26, |
64
|
|
|
Country::ANDORRA => 24, |
65
|
|
|
Country::ANGOLA => 25, |
66
|
|
|
Country::AUSTRIA => 20, |
67
|
|
|
Country::AZERBAIJAN => 28, |
68
|
|
|
Country::BAHRAIN => 22, |
69
|
|
|
Country::BELARUS => 28, |
70
|
|
|
Country::BELGIUM => 16, |
71
|
|
|
Country::BENIN => 28, |
72
|
|
|
Country::BOSNIA_HERZEGOVINA => 20, |
73
|
|
|
Country::BRAZIL => 29, |
74
|
|
|
Country::BULGARIA => 22, |
75
|
|
|
Country::BURKINA_FASO => 28, |
76
|
|
|
Country::BURUNDI => 28, |
77
|
|
|
Country::CAMEROON => 27, |
78
|
|
|
Country::CAPE_VERDE => 25, |
79
|
|
|
Country::CENTRAL_AFRICAN_REPUBLIC => 27, |
80
|
|
|
Country::CHAD => 27, |
81
|
|
|
Country::COMOROS => 27, |
82
|
|
|
Country::CONGO => 27, |
83
|
|
|
Country::COSTA_RICA => 22, |
84
|
|
|
Country::CROATIA => 21, |
85
|
|
|
Country::CYPRUS => 28, |
86
|
|
|
Country::CZECH_REPUBLIC => 24, |
87
|
|
|
Country::DENMARK => 18, |
88
|
|
|
Country::DJIBOUTI => 27, |
89
|
|
|
Country::DOMINICAN_REPUBLIC => 28, |
90
|
|
|
Country::EGYPT => 29, |
91
|
|
|
Country::EL_SALVADOR => 28, |
92
|
|
|
Country::EQUATORIAL_GUINEA => 27, |
93
|
|
|
Country::ESTONIA => 20, |
94
|
|
|
Country::FAROE_ISLANDS => 18, |
95
|
|
|
Country::FINLAND => 18, |
96
|
|
|
Country::FRANCE => 27, |
97
|
|
|
Country::GABON => 27, |
98
|
|
|
Country::GEORGIA => 22, |
99
|
|
|
Country::GERMANY => 22, |
100
|
|
|
Country::GIBRALTAR => 23, |
101
|
|
|
Country::GREECE => 27, |
102
|
|
|
Country::GREENLAND => 18, |
103
|
|
|
Country::GUATEMALA => 28, |
104
|
|
|
Country::GUINEA_BISSAU => 25, |
105
|
|
|
Country::HOLY_SEE => 22, |
106
|
|
|
Country::HONDURAS => 28, |
107
|
|
|
Country::HUNGARY => 28, |
108
|
|
|
Country::ICELAND => 26, |
109
|
|
|
Country::IRAN => 26, |
110
|
|
|
Country::IRAQ => 23, |
111
|
|
|
Country::IRELAND => 22, |
112
|
|
|
Country::ISRAEL => 23, |
113
|
|
|
Country::ITALY => 27, |
114
|
|
|
Country::IVORY_COAST => 28, |
115
|
|
|
Country::JORDAN => 30, |
116
|
|
|
Country::KAZAKHSTAN => 20, |
117
|
|
|
Country::KOSOVO => 20, |
118
|
|
|
Country::KUWAIT => 30, |
119
|
|
|
Country::LATVIA => 21, |
120
|
|
|
Country::LEBANON => 28, |
121
|
|
|
Country::LIBYA => 25, |
122
|
|
|
Country::LIECHTENSTEIN => 21, |
123
|
|
|
Country::LITHUANIA => 20, |
124
|
|
|
Country::LUXEMBOURG => 20, |
125
|
|
|
Country::MADAGASCAR => 27, |
126
|
|
|
Country::MALI => 28, |
127
|
|
|
Country::MALTA => 31, |
128
|
|
|
Country::MAURITANIA => 27, |
129
|
|
|
Country::MAURITIUS => 30, |
130
|
|
|
Country::MOLDOVA => 24, |
131
|
|
|
Country::MONACO => 27, |
132
|
|
|
Country::MONGOLIA => 20, |
133
|
|
|
Country::MONTENEGRO => 22, |
134
|
|
|
Country::MOROCCO => 28, |
135
|
|
|
Country::MOZAMBIQUE => 25, |
136
|
|
|
Country::NETHERLANDS => 18, |
137
|
|
|
Country::NICARAGUA => 32, |
138
|
|
|
Country::NIGER => 28, |
139
|
|
|
Country::NORTH_MACEDONIA => 19, |
140
|
|
|
Country::NORWAY => 15, |
141
|
|
|
Country::PAKISTAN => 24, |
142
|
|
|
Country::PALESTINE => 29, |
143
|
|
|
Country::POLAND => 28, |
144
|
|
|
Country::PORTUGAL => 25, |
145
|
|
|
Country::QATAR => 29, |
146
|
|
|
Country::ROMANIA => 24, |
147
|
|
|
Country::RUSSIA => 33, |
148
|
|
|
Country::SAINT_LUCIA => 32, |
149
|
|
|
Country::SAN_MARINO => 27, |
150
|
|
|
Country::SAO_TOME_PRINCIPE => 25, |
151
|
|
|
Country::SAUDI_ARABIA => 24, |
152
|
|
|
Country::SENEGAL => 28, |
153
|
|
|
Country::SERBIA => 22, |
154
|
|
|
Country::SEYCHELLES => 31, |
155
|
|
|
Country::SLOVAKIA => 24, |
156
|
|
|
Country::SLOVENIA => 19, |
157
|
|
|
Country::SOMALIA => 23, |
158
|
|
|
Country::SPAIN => 24, |
159
|
|
|
Country::SUDAN => 18, |
160
|
|
|
Country::SWEDEN => 24, |
161
|
|
|
Country::SWITZERLAND => 21, |
162
|
|
|
Country::TIMOR_LESTE => 23, |
163
|
|
|
Country::TOGO => 28, |
164
|
|
|
Country::TUNISIA => 24, |
165
|
|
|
Country::TURKEY => 26, |
166
|
|
|
Country::UKRAINE => 29, |
167
|
|
|
Country::UNITED_ARAB_EMIRATES => 23, |
168
|
|
|
Country::UNITED_KINGDOM => 22, |
169
|
|
|
Country::VIRGIN_ISLANDS_BRITISH => 24, |
170
|
|
|
]; |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set multiple country codes to validate IBAN (Optional). |
174
|
|
|
*/ |
175
|
|
|
private ?array $countries; |
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Given $params and assign the $this->params |
179
|
|
|
* |
180
|
|
|
* @return self |
181
|
|
|
*/ |
182
|
|
|
public function fillParameters(array $params): Rule |
183
|
|
|
{ |
184
|
|
|
if (count($params) === 1 && is_array($params[0])) { |
185
|
2 |
|
$params = $params[0]; |
186
|
|
|
} |
187
|
|
|
|
188
|
2 |
|
return $this->countries((array) $params); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function countries(array|string $countries): self |
192
|
|
|
{ |
193
|
2 |
|
$countries = ! is_array($countries) ? func_get_args() : $countries; |
|
|
|
|
194
|
|
|
|
195
|
2 |
|
$this->setCountries($countries); |
196
|
|
|
|
197
|
2 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Check if a given value contains duplicated items. |
202
|
|
|
* |
203
|
|
|
* @credit <a href="https://github.com/milwad-dev/laravel-validate">milwad/laravel-validate - Milwad\LaravelValidate\Rules\ValidIban</a> |
204
|
|
|
* |
205
|
|
|
* @param mixed $value |
206
|
|
|
*/ |
207
|
|
|
public function check($value): bool |
208
|
|
|
{ |
209
|
2 |
|
return $this->isIbanValid($value); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|