Passed
Push — 1.x ( 295c80...461df0 )
by Milwad
01:11 queued 13s
created

ValidIban   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 146
c 1
b 0
f 1
dl 0
loc 178
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isIbanValid() 0 6 4
A passes() 0 11 2
A message() 0 3 1
1
<?php
2
3
namespace Milwad\LaravelValidate\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
7
class ValidIban implements Rule
8
{
9
    private $characterMap = [
10
        'A' => 10,
11
        'B' => 11,
12
        'C' => 12,
13
        'D' => 13,
14
        'E' => 14,
15
        'F' => 15,
16
        'G' => 16,
17
        'H' => 17,
18
        'I' => 18,
19
        'J' => 19,
20
        'K' => 20,
21
        'L' => 21,
22
        'M' => 22,
23
        'N' => 23,
24
        'O' => 24,
25
        'P' => 25,
26
        'Q' => 26,
27
        'R' => 27,
28
        'S' => 28,
29
        'T' => 29,
30
        'U' => 30,
31
        'V' => 31,
32
        'W' => 32,
33
        'X' => 33,
34
        'Y' => 34,
35
        'Z' => 35,
36
    ];
37
38
    private $ibanLengthByCountry = [
39
        'AD' => 24, // Andorra
40
        'AE' => 23, // United Arab Emirates
41
        'AL' => 28, // Albania
42
        'AO' => 25, // Angola
43
        'AT' => 20, // Austria
44
        'AZ' => 28, // Azerbaijan
45
        'BA' => 20, // Bosnia and Herzegovina
46
        'BE' => 16, // Belgium
47
        'BF' => 28, // Burkina Faso
48
        'BG' => 22, // Bulgaria
49
        'BH' => 22, // Bahrain
50
        'BI' => 28, // Burundi
51
        'BJ' => 28, // Benin
52
        'BR' => 29, // Brazil
53
        'BY' => 28, // Belarus
54
        'CF' => 27, // Central African Republic
55
        'CG' => 27, // Congo
56
        'CH' => 21, // Switzerland
57
        'CI' => 28, // Ivory Coast
58
        'CM' => 27, // Cameroon
59
        'CR' => 22, // Costa Rica
60
        'CV' => 25, // Cape Verde
61
        'CY' => 28, // Cyprus
62
        'CZ' => 24, // Czech Republic
63
        'DE' => 22, // Germany
64
        'DJ' => 27, // Djibouti
65
        'DK' => 18, // Denmark
66
        'DO' => 28, // Dominican Republic
67
        'DZ' => 26, // Algeria
68
        'EE' => 20, // Estonia
69
        'EG' => 29, // Egypt
70
        'ES' => 24, // Spain
71
        'FI' => 18, // Finland
72
        'FO' => 18, // Faroe Islands
73
        'FR' => 27, // France
74
        'GA' => 27, // Gabon
75
        'GB' => 22, // United Kingdom
76
        'GE' => 22, // Georgia
77
        'GI' => 23, // Gibraltar
78
        'GL' => 18, // Greenland
79
        'GQ' => 27, // Equatorial Guinea
80
        'GR' => 27, // Greece
81
        'GT' => 28, // Guatemala
82
        'GW' => 25, // Guinea-Bissau
83
        'HN' => 28, // Honduras
84
        'HR' => 21, // Croatia
85
        'HU' => 28, // Hungary
86
        'IE' => 22, // Ireland
87
        'IL' => 23, // Israel
88
        'IQ' => 23, // Iraq
89
        'IR' => 26, // Iran
90
        'IS' => 26, // Iceland
91
        'IT' => 27, // Italy
92
        'JO' => 30, // Jordan
93
        'KM' => 27, // Comoros
94
        'KW' => 30, // Kuwait
95
        'KZ' => 20, // Kazakhstan
96
        'LB' => 28, // Lebanon
97
        'LC' => 32, // Saint Lucia
98
        'LI' => 21, // Liechtenstein
99
        'LT' => 20, // Lithuania
100
        'LU' => 20, // Luxembourg
101
        'LV' => 21, // Latvia
102
        'LY' => 25, // Libya
103
        'MA' => 28, // Morocco
104
        'MC' => 27, // Monaco
105
        'MD' => 24, // Moldova
106
        'ME' => 22, // Montenegro
107
        'MG' => 27, // Madagascar
108
        'MK' => 19, // North Macedonia
109
        'ML' => 28, // Mali
110
        'MN' => 20, // Mongolia
111
        'MR' => 27, // Mauritania
112
        'MT' => 31, // Malta
113
        'MU' => 30, // Mauritius
114
        'MZ' => 25, // Mozambique
115
        'NE' => 28, // Niger
116
        'NI' => 32, // Nicaragua
117
        'NL' => 18, // Netherlands
118
        'NO' => 15, // Norway
119
        'PK' => 24, // Pakistan
120
        'PL' => 28, // Poland
121
        'PS' => 29, // Palestine
122
        'PT' => 25, // Portugal
123
        'QA' => 29, // Qatar
124
        'RO' => 24, // Romania
125
        'RS' => 22, // Serbia
126
        'RU' => 33, // Russia
127
        'SA' => 24, // Saudi Arabia
128
        'SC' => 31, // Seychelles
129
        'SD' => 18, // Sudan
130
        'SE' => 24, // Sweden
131
        'SI' => 19, // Slovenia
132
        'SK' => 24, // Slovakia
133
        'SM' => 27, // San Marino
134
        'SN' => 28, // Senegal
135
        'SO' => 23, // Somalia
136
        'ST' => 25, // Sao Tome and Principe
137
        'SV' => 28, // El Salvador
138
        'TD' => 27, // Chad
139
        'TG' => 28, // Togo
140
        'TL' => 23, // Timor-Leste
141
        'TN' => 24, // Tunisia
142
        'TR' => 26, // Turkey
143
        'UA' => 29, // Ukraine
144
        'VA' => 22, // Holy See (the)
145
        'VG' => 24, // Virgin Islands, British
146
        'XK' => 20, // Kosovo
147
    ];
148
149
    /**
150
     * Check IBAN.
151
     *
152
     * @param string $attribute
153
     * @param mixed $value
154
     * @return bool
155
     */
156
    public function passes($attribute, $value)
157
    {
158
        if (!$this->isIbanValid($value)) {
159
160
            return false;
161
        }
162
163
        $parsedIban = substr($value, 4) . substr($value, 0, 4);
164
        $parsedIban = strtr($parsedIban, $this->characterMap);
165
166
        return bcmod($parsedIban, '97') === '1';
167
    }
168
169
    /**
170
     * Get the validation error message.
171
     *
172
     * @return string
173
     */
174
    public function message()
175
    {
176
        return __('validate.iban');
177
    }
178
179
    private function isIbanValid($iban)
180
    {
181
        $countryCode = substr($iban, 0, 2);
182
183
        return !(!function_exists('bcmod') || empty($iban) || !ctype_alpha(substr($iban, 0, 2)) ||
184
            strlen($iban) != $this->ibanLengthByCountry[$countryCode]);
185
    }
186
}
187