Skip to content

Commit 883fdc2

Browse files
authored
Merge pull request #44 from milwad-dev/fix-valid-iban-pr
[1.x] Fix PR #43
2 parents b89f0d8 + cb41551 commit 883fdc2

File tree

3 files changed

+95
-48
lines changed

3 files changed

+95
-48
lines changed

README.md

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,52 +62,54 @@ return [
6262
```
6363

6464
# Rules
65-
| # | Rule name | Usage |
66-
|-----|----------------------------|-------------------------------------------------------------------------|
67-
| 1 | ValidBitcoinAddress | Validate bitcoin address for ex (1KFHE7w8BhaENAswwryaoccDb6qcT6DbYY) |
68-
| 2 | ValidCapitalCharWithNumber | Validate String for ex (MILWAD-84) |
69-
| 3 | ValidDiscordUsername | Validate username discord for ex (Milwad#2134) |
70-
| 4 | ValidHashtag | Validate hashtag for ex (#milwad) |
71-
| 5 | ValidIpAddressIPV4 | Validate ip address IPV4 for ex (129.144.50.56) |
65+
66+
| # | Rule name | Usage |
67+
|-----|----------------------------|--------------------------------------------------------------------------|
68+
| 1 | ValidBitcoinAddress | Validate bitcoin address for ex (1KFHE7w8BhaENAswwryaoccDb6qcT6DbYY) |
69+
| 2 | ValidCapitalCharWithNumber | Validate String for ex (MILWAD-84) |
70+
| 3 | ValidDiscordUsername | Validate username discord for ex (Milwad#2134) |
71+
| 4 | ValidHashtag | Validate hashtag for ex (#milwad) |
72+
| 5 | ValidIpAddressIPV4 | Validate ip address IPV4 for ex (129.144.50.56) |
7273
| 6 | ValidIpAddressIPV6 | Validate ip address IPV6 for ex (2001:db8:3333:4444:5555:6666:7777:8888) |
73-
| 7 | ValidPhoneNumber | Validate phone number for ex (09366000000) |
74-
| 8 | ValidPort | Validate port value for ex (8080) |
75-
| 9 | ValidSlug | Validate slug for ex (milwad-dev) |
76-
| 10 | ValidImei | Validate imei phone for ex (354809104295874) |
77-
| 11 | ValidStrongPassword | Validate password for ex (Milwad123!) |
78-
| 12 | ValidUrl | Validate url for ex (https://www.google.com) |
79-
| 13 | ValidUsername | Validate username for ex (milwad) |
80-
| 14 | ValidUuid | Validate uuid for ex (123e4567-e89b-12d3-a456-426655440000) |
81-
| 15 | ValidHexColor | Validate hex color for ex (#fcba03) |
82-
| 16 | ValidHtmlTag | Validate html tags for ex (``<h1></h1>``) |
83-
| 17 | ValidNationalCard | Validate national card for ex (015016437) |
84-
| 18 | ValidCarNumber | Validate car number for ex (KA01AB1234) |
85-
| 19 | ValidNameDaysWeek | Validate day week for ex (monday) |
86-
| 20 | ValidEvenNumber | Validate number is even for ex (1024) |
87-
| 21 | ValidOddNumber | Validate number is odd for ex (4321) |
88-
| 22 | ValidSlashEndOfString | Validate write slash at the string for ex (milwad/) |
89-
| 23 | ValidCartNumberIran | Validate cart number for ex (1234123412341234) |
90-
| 24 | ValidBase64 | Validate base64 for ex (bWlsd2Fk) |
91-
| 25 | ValidJwt | Validate jwt |
92-
| 26 | ValidJalaliDate | Validate jajali date for ex (1384/8/25) |
93-
| 27 | ValidDuplicateCharacter | Validate duplicate characters for ex (1,2,3,4,5,6,7,8,9) |
94-
| 28 | ValidPattern | Validate texts with specific pattern 🔥 for ex (4444-4444-4444) |
95-
| 29 | ValidDuplicate | Validate duplicate string, numbers for ex (1123456) |
96-
| 30 | ValidUlid | Validate ulid for ex (01ARZ3NDEKTSV4RRFFQ69G5FAV) |
97-
| 31 | ValidDomain | Validate domain name for ex (github.com) |
98-
| 32 | ValidCamelCase | Validate camel case for ex (milwadDev) |
99-
| 33 | ValidPascalCase | Validate pascal case for ex (MilwadDev) |
100-
| 34 | ValidKebabCase | Validate kebab case for ex (milwad-dev) |
101-
| 35 | ValidSnakeCase | Validate snake case for ex (milwad_dev) |
102-
| 36 | ValidVatId | Validate european VAT ID ex (EL123456789123) |
103-
| 37 | ValidIban | Validate IBAN ex (IR062960000000100324200001) |
74+
| 7 | ValidPhoneNumber | Validate phone number for ex (09366000000) |
75+
| 8 | ValidPort | Validate port value for ex (8080) |
76+
| 9 | ValidSlug | Validate slug for ex (milwad-dev) |
77+
| 10 | ValidImei | Validate imei phone for ex (354809104295874) |
78+
| 11 | ValidStrongPassword | Validate password for ex (Milwad123!) |
79+
| 12 | ValidUrl | Validate url for ex (https://www.google.com) |
80+
| 13 | ValidUsername | Validate username for ex (milwad) |
81+
| 14 | ValidUuid | Validate uuid for ex (123e4567-e89b-12d3-a456-426655440000) |
82+
| 15 | ValidHexColor | Validate hex color for ex (#fcba03) |
83+
| 16 | ValidHtmlTag | Validate html tags for ex (``<h1></h1>``) |
84+
| 17 | ValidNationalCard | Validate national card for ex (015016437) |
85+
| 18 | ValidCarNumber | Validate car number for ex (KA01AB1234) |
86+
| 19 | ValidNameDaysWeek | Validate day week for ex (monday) |
87+
| 20 | ValidEvenNumber | Validate number is even for ex (1024) |
88+
| 21 | ValidOddNumber | Validate number is odd for ex (4321) |
89+
| 22 | ValidSlashEndOfString | Validate write slash at the string for ex (milwad/) |
90+
| 23 | ValidCartNumberIran | Validate cart number for ex (1234123412341234) |
91+
| 24 | ValidBase64 | Validate base64 for ex (bWlsd2Fk) |
92+
| 25 | ValidJwt | Validate jwt |
93+
| 26 | ValidJalaliDate | Validate jajali date for ex (1384/8/25) |
94+
| 27 | ValidDuplicateCharacter | Validate duplicate characters for ex (1,2,3,4,5,6,7,8,9) |
95+
| 28 | ValidPattern | Validate texts with specific pattern 🔥 for ex (4444-4444-4444) |
96+
| 29 | ValidDuplicate | Validate duplicate string, numbers for ex (1123456) |
97+
| 30 | ValidUlid | Validate ulid for ex (01ARZ3NDEKTSV4RRFFQ69G5FAV) |
98+
| 31 | ValidDomain | Validate domain name for ex (github.com) |
99+
| 32 | ValidCamelCase | Validate camel case for ex (milwadDev) |
100+
| 33 | ValidPascalCase | Validate pascal case for ex (MilwadDev) |
101+
| 34 | ValidKebabCase | Validate kebab case for ex (milwad-dev) |
102+
| 35 | ValidSnakeCase | Validate snake case for ex (milwad_dev) |
103+
| 36 | ValidVatId | Validate european VAT ID ex (EL123456789123) |
104+
| 37 | ValidIban | Validate IBAN ex (IR062960000000100324200001) |
104105

105106
# Rules
106107

107108
### ValidPattern
108109

109110
Sometimes you need to validate text with pattern, For example you want to validate a text like (4444-4444-4444).
110-
If you want to check the length of text is seperator after ```-``` is 4, You can use this Rule:
111+
If you want to check the length of text is seperator after `-` is 4, You can use this Rule:
112+
111113
```php
112114
use Milwad\LaravelValidate\Rules\ValidPattern;
113115

@@ -116,6 +118,18 @@ return [
116118
];
117119
```
118120

121+
### ValidIban
122+
123+
Sometimes you need to validate Iban(International Bank Account Number) code.You can use `ValidIban` rule:
124+
125+
```php
126+
use Milwad\LaravelValidate\Rules\ValidIban;
127+
128+
return [
129+
'iban' => ['required', new ValidIban()], // iban => SC74MCBL01031234567890123456USD
130+
];
131+
```
132+
119133
<a name="support-languages"></a>
120134
# Support Languages
121135

src/Rules/ValidIban.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77
class ValidIban implements Rule
88
{
9-
private $characterMap = [
9+
/**
10+
* Character map
11+
*
12+
* @var array|int[]
13+
*/
14+
private array $characterMap = [
1015
'A' => 10,
1116
'B' => 11,
1217
'C' => 12,
@@ -35,7 +40,12 @@ class ValidIban implements Rule
3540
'Z' => 35,
3641
];
3742

38-
private $ibanLengthByCountry = [
43+
/**
44+
* Get country code with length.
45+
*
46+
* @var array|int[]
47+
*/
48+
private array $ibanLengthByCountry = [
3949
'AD' => 24, // Andorra
4050
'AE' => 23, // United Arab Emirates
4151
'AL' => 28, // Albania
@@ -146,6 +156,16 @@ class ValidIban implements Rule
146156
'XK' => 20, // Kosovo
147157
];
148158

159+
/**
160+
* Country code from outside.
161+
*/
162+
private string|null $country;
163+
164+
public function __construct(string|null $country = null) // TODO: Add $country into the progress
165+
{
166+
$this->country = $country;
167+
}
168+
149169
/**
150170
* Check IBAN.
151171
*
@@ -156,11 +176,17 @@ class ValidIban implements Rule
156176
public function passes($attribute, $value)
157177
{
158178
if (! $this->isIbanValid($value)) {
159-
160179
return false;
161180
}
162181

182+
/*
183+
* Connect Iban title with value (code) ex: 8330001234567NO .
184+
*/
163185
$parsedIban = substr($value, 4).substr($value, 0, 4);
186+
187+
/*
188+
* Replace iban value with character map.
189+
*/
164190
$parsedIban = strtr($parsedIban, $this->characterMap);
165191

166192
return bcmod($parsedIban, '97') === '1';
@@ -176,11 +202,18 @@ public function message()
176202
return __('validate.iban');
177203
}
178204

179-
private function isIbanValid($iban)
205+
/**
206+
* Check iban is valid.
207+
*
208+
* @return bool
209+
*/
210+
private function isIbanValid(string $iban)
180211
{
181-
$countryCode = substr($iban, 0, 2);
212+
$countryCode = substr($iban, 0, 2); // Get character of value
182213

183-
return ! (! function_exists('bcmod') || empty($iban) || ! ctype_alpha(substr($iban, 0, 2)) ||
184-
strlen($iban) != $this->ibanLengthByCountry[$countryCode]);
214+
return ! empty($iban)
215+
|| function_exists('bcmod') // Check `bcmod` is exists
216+
|| ctype_alpha(substr($iban, 0, 2)) //
217+
|| strlen($iban) !== $this->ibanLengthByCountry[$countryCode];
185218
}
186219
}

tests/Rules/ValidIbanTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ public function iban_is_valid()
140140
];
141141

142142
foreach ($ibans as $iban) {
143-
144143
$data = ['iban' => $iban];
145144
$passes = $this->app['validator']->make($data, $rules)->passes();
145+
146146
$this->assertTrue($passes);
147147
}
148148
}
@@ -165,9 +165,9 @@ public function iban_is_not_valid()
165165
];
166166

167167
foreach ($ibans as $iban) {
168-
169168
$data = ['iban' => $iban];
170169
$passes = $this->app['validator']->make($data, $rules)->passes();
170+
171171
$this->assertFalse($passes);
172172
}
173173
}

0 commit comments

Comments
 (0)