blib/lib/oEdtk/IBAN.pm

filter: Statementstmt 41.1% 30 / 73 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 38.1% 8 / 21 Podpod 100% 13 / 13 totaltotal 36.9% 51 / 138 CC 13 SCAR 24.5
CC
13
Cov
31%
iban_error
42.0
is_in_list
12.0
bic_error
12.0
CRAP 11.6
« blib/lib/oEdtk/Ext.pm ↑ Summary blib/lib/oEdtk/Dict.pm »
1 package oEdtk::IBAN;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '2.1129';
6 1 ▶use bigint;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use experimental 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 Readonly my $IBAN_DIVISOR => 97;
13 1 Readonly my $IBAN_LETTER_A_VALUE => 10;
14  
15 1 Readonly my $REPUBLIC_OF_KOSOVO => 'XK'; # extra, specific case not in ISO-3166
16 1 Readonly my @ISO_3166 => qw(
17 AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL
18 BM BN BO BQ BR BS BT BV BW BY BZ CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV
19 CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR GA GB GD
20 GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM
21 IN IO IQ IR IS IT JE JM JO JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK
22 LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR MS MT MU MV MW
23 MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR
24 PS PT PW PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS
25 ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ UA UG UM US UY
26 UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW
27 );
28  
29 0 ▶sub is_in_list {
Subroutine is_in_list: not called CC=3 SCAR=24.8
Pod documented
30 0 my ( $val, $l ) = @_;
31 0 foreach my $v ( @{$l} ) {
32 0 ▶ return 1 if $v eq $val;
Branch: 1 branch
BranchTrueFalseTotal
if $v eq $val 0 0 0
33 }
34 0 return 0;
35 }
36  
37 0 ▶sub is_valid_country {
Subroutine is_valid_country: not called CC=2 SCAR=17.9
Pod documented
38 0 my ($country) = @_;
39 0 ▶ return $country eq $REPUBLIC_OF_KOSOVO
Condition0%0/3
$country eq $REPUBLIC_OF_KOSOVO || is_in_list($country, \@ISO_3166)
l!l&&r!l&&!r
000
MC/DC0%0/2
$country eq $REPUBLIC_OF_KOSOVO || is_in_list($country, \@ISO_3166)
$country eq $REPUBLIC_OF_KOSOVO is_in_list($country, \@ISO_3166)
Truth table0%0/3
A || B
A= $country eq $REPUBLIC_OF_KOSOVOB= is_in_list($country, \@ISO_3166)
ABresult
1X1
011
000
40 || is_in_list( $country, \@ISO_3166 );
41 }
42  
43 0 ▶sub is_valid_bic_format {
Subroutine is_valid_bic_format: not called CC=1 SCAR=6.9
Pod documented
44 0 my ($str) = @_;
45 0 return $str =~ m/^
46 [A-Z][A-Z][A-Z][A-Z]
47 [A-Z][A-Z]
48 [A-Z\d][A-Z\d]
49 (?:[A-Z\d][A-Z\d][A-Z\d])?
50 $/sx;
51 }
52  
53 0 ▶sub bic_country {
Subroutine bic_country: not called CC=1 SCAR=6.9
Pod documented
54 0 my ($str) = @_;
55 0 return substr $str, 4, 2;
56 }
57  
58 0 ▶sub iban_country {
Subroutine iban_country: not called CC=1 SCAR=6.9
Pod documented
59 0 my ($str) = @_;
60 0 return substr $str, 0, 2;
61 }
62  
63 # NATXFRPP
64 # https://en.wikipedia.org/wiki/ISO_9362#Structure
65 # the standard costs 38 CHF: https://www.iso.org/standard/60390.html
66 0 ▶sub is_valid_bic {
Subroutine is_valid_bic: not called CC=2 SCAR=17.9
Pod documented
67 0 my ($str) = @_;
68 0 ▶ return is_valid_bic_format($str) && is_valid_country( bic_country($str) );
Condition0%0/3
is_valid_bic_format($str) && is_valid_country(bic_country($str))
!ll&&!rl&&r
000
MC/DC0%0/2
is_valid_bic_format($str) && is_valid_country(bic_country($str))
is_valid_bic_format($str) is_valid_country(bic_country($str))
Truth table0%0/3
A && B
A= is_valid_bic_format($str)B= is_valid_country(bic_country($str))
ABresult
0X0
100
111
69 }
70  
71 # Convert one uppercase letter to its two-digit IBAN value (A -> 10, B -> 11,
72 # ..., Z -> 35). The fork read the global $1 instead of its own argument, so it
73 # only ever worked from inside is_valid_iban_checksum's s///e; called directly it
74 # returned -55. Read $char.
75 0 ▶sub iban_letter_to_number {
Subroutine iban_letter_to_number: not called CC=1 SCAR=6.9
Pod documented
76 0 my ($char) = @_;
77 0 return sprintf '%02d', ord($char) - ord('A') + $IBAN_LETTER_A_VALUE;
78 }
79  
80 # regexp computed with Editique_interne/lab/seb/tool/iban/iban.pl
81 0 ▶sub is_valid_iban_format {
Subroutine is_valid_iban_format: not called CC=1 SCAR=6.9
Pod documented
82 0 my ($str) = @_;
83 0 return $str =~ m/^(?:
84 AD[0-9]{10}[0-9a-zA-Z]{12}
85 |AE[0-9]{21}
86 |AL[0-9]{10}[0-9a-zA-Z]{16}
87 |AT[0-9]{18}
88 |AX[0-9]{16}
89 |AZ[0-9]{2}[A-Z]{4}[0-9a-zA-Z]{20}
90 |BA[0-9]{18}
91 |BE[0-9]{14}
92 |BG[0-9]{2}[A-Z]{4}[0-9]{6}[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-Z]{12}
164 |ST[0-9]{23}
165 |SV[0-9]{2}[A-Z]{4}[0-9]{20}
166 |TF[0-9]{12}[0-9a-zA-Z]{11}[0-9]{2}
167 |TL[0-9]{21}
168 |TN[0-9]{22}
169 |TR[0-9]{8}[0-9a-zA-Z]{16}
170 |UA[0-9]{8}[0-9a-zA-Z]{19}
171 |VA[0-9]{20}
172 |VG[0-9]{2}[A-Z]{4}[0-9]{16}
173 |WF[0-9]{12}[0-9a-zA-Z]{11}[0-9]{2}
174 |XK[0-9]{18}
175 |YT[0-9]{12}[0-9a-zA-Z]{11}[0-9]{2}
176 )$/sx;
177 }
178  
179 # ex: FR7630006000011234567890189
180 # https://en.wikipedia.org/wiki/IBAN#Validating_the_IBAN
181 0 ▶sub is_valid_iban_checksum {
Subroutine is_valid_iban_checksum: not called CC=1 SCAR=6.9
Pod documented
182 0 my ($str) = @_;
183 0 my $prefix = substr $str, 0, 4;
184 0 my $rest = substr $str, 4;
185 0 my $tmp = $rest . $prefix;
186 0 $tmp =~ s{([A-Z])}{iban_letter_to_number($1)}gsex;
187 0 my $n = 0 + $tmp; # convert to bigint
188 0 my $mod = $n % $IBAN_DIVISOR;
189 0 return $mod == 1;
190 }
191  
192 0 ▶sub is_valid_iban {
Subroutine is_valid_iban: not called CC=2 SCAR=17.9
Pod documented
193 0 my ($str) = @_;
194 0 ▶ return is_valid_iban_format($str) && is_valid_iban_checksum($str);
Condition0%0/3
is_valid_iban_format($str) && is_valid_iban_checksum($str)
!ll&&!rl&&r
000
MC/DC0%0/2
is_valid_iban_format($str) && is_valid_iban_checksum($str)
is_valid_iban_format($str) is_valid_iban_checksum($str)
Truth table0%0/3
A && B
A= is_valid_iban_format($str)B= is_valid_iban_checksum($str)
ABresult
0X0
100
111
195 }
196  
197 # https://www.swift.com/file/55811/download?token=Qng75mq9
198 # page 6
199 0 ▶sub is_credible_iban {
Subroutine is_credible_iban: not called CC=1 SCAR=6.9
Pod documented
200 0 my ($str) = @_;
201 0 return $str =~ m/^[A-Z]{2}[0-9]{2}[A-Za-z0-9]{0,30}$/sx;
202 }
203  
204 0 ▶sub iban_error {
Subroutine iban_error: not called CC=6 SCAR=37.4
Pod documented
205 0 my ($str) = @_;
206 0 ▶ return 'IBAN does not match common format' if !is_credible_iban($str);
Branch: 1 branch
BranchTrueFalseTotal
unless is_credible_iban($str) 0 0 0
207 0 ▶ return 'IBAN references an unknown country'
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_country(iban_country($str)) 0 0 0
208 if !is_valid_country( iban_country($str) );
209 0 ▶ return 'IBAN does not follow its country format'
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_iban_format($str) 0 0 0
210 if !is_valid_iban_format($str);
211 0 ▶ return q{}
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /[a-z]/sx 0 0 0
212 if $str =~
213 m{[a-z]}sx; # checksums with lower case letters not implemented yet (standard not found)
214 0 ▶ return 'IBAN checksum invalid' if !is_valid_iban_checksum($str);
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_iban_checksum($str) 0 0 0
215 0 return q{};
216 }
217  
218 0 ▶sub bic_error {
Subroutine bic_error: not called CC=3 SCAR=24.8
Pod documented
219 0 my ($str) = @_;
220 0 ▶ return 'BIC does not match common format' if !is_valid_bic_format($str);
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_bic_format($str) 0 0 0
221 0 ▶ return 'BIC references an unknown country'
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_country(bic_country($str)) 0 0 0
222 if !is_valid_country( bic_country($str) );
223 0 return q{};
224 }
225  
226 # bigint is only needed above; disabling it here keeps the module's final "1;"
227 # from being a void-context constant, which would otherwise emit
228 # "Useless use of a constant (1)" under use warnings.
229 1 ▶no bigint;
Subroutine BEGIN: called CC=1 SCAR=0.0
230  
231 1 1;
232  
233 =head1 NAME
234  
235 oEdtk::IBAN - validate IBAN and BIC identifiers
236  
237 =head1 SYNOPSIS
238  
239 use oEdtk::IBAN;
240  
241 my $err = oEdtk::IBAN::iban_error($iban);
242 # '' when the IBAN is valid, otherwise a human readable reason
243 $err = oEdtk::IBAN::bic_error($bic);
244  
245 =head1 DESCRIPTION
246  
247 Pure-Perl validation of International Bank Account Numbers (IBAN) and Bank
248 Identifier Codes (BIC), following the ISO-13616 and ISO-9362 structures.
249  
250 Every function is called fully qualified (the module exports nothing). The
251 C<*_error> functions return an empty string when their argument is valid and a
252 short English message otherwise, so a caller can decide whether to warn or
253 reject without catching exceptions.
254  
255 The fork shipped this module at version 0.8032; it was moved unmodified into the
256 mainline at S20 of the fork-merge plan, save for a fix to
257 C<iban_letter_to_number> (see L</BUGS>).
258  
259 =head1 FUNCTIONS
260  
261 =head2 iban_error
262  
263 my $reason = iban_error($iban);
264  
265 Run every IBAN check in order and return the first failure, or C<''>. Checks, in
266 order: the general shape (L</is_credible_iban>), the country code
267 (L</is_valid_country>), the country-specific format (L</is_valid_iban_format>)
268 and finally the checksum (L</is_valid_iban_checksum>). An IBAN containing lower
269 case letters is accepted without a checksum check, because the lower case
270 checksum is not implemented.
271  
272 =head2 bic_error
273  
274 my $reason = bic_error($bic);
275  
276 Return C<''> when the BIC has both a valid shape and a known country code, and a
277 message otherwise.
278  
279 =head2 is_valid_iban
280  
281 my $ok = is_valid_iban($iban);
282  
283 True when the IBAN matches its country format I<and> its checksum is valid.
284  
285 =head2 is_valid_iban_format
286  
287 my $ok = is_valid_iban_format($iban);
288  
289 True when the IBAN matches the length and structure registered for its country.
290  
291 =head2 is_valid_iban_checksum
292  
293 my $ok = is_valid_iban_checksum($iban);
294  
295 True when the IBAN passes the ISO-13616 mod-97 checksum. The letters are folded
296 to digits via L</iban_letter_to_number> and the arithmetic uses C<bigint>.
297  
298 =head2 is_credible_iban
299  
300 my $ok = is_credible_iban($iban);
301  
302 Cheap shape check: two uppercase letters, two digits and up to thirty
303 alphanumeric characters.
304  
305 =head2 iban_country / bic_country
306  
307 my $cc = iban_country($iban); # first two characters
308 my $cc = bic_country($bic); # characters 5 and 6
309  
310 Extract the country code from an IBAN or a BIC.
311  
312 =head2 is_valid_country
313  
314 my $ok = is_valid_country($cc);
315  
316 True when C<$cc> is an ISO-3166 alpha-2 code, or XK (Kosovo, a widely used code
317 that is absent from ISO-3166).
318  
319 =head2 is_valid_bic
320  
321 my $ok = is_valid_bic($bic);
322  
323 True when the BIC has a valid shape and a known country code.
324  
325 =head2 is_valid_bic_format
326  
327 my $ok = is_valid_bic_format($bic);
328  
329 True when the BIC follows the ISO-9362 structure: four letters (bank), two
330 letters (country), two alphanumeric (location) and an optional three-character
331 alphanumeric branch.
332  
333 =head2 iban_letter_to_number
334  
335 my $two_digits = iban_letter_to_number($char);
336  
337 Convert one uppercase letter to its two-digit IBAN value (A -> C<10>, B -> C<11>,
338 ..., Z -> C<35>).
339  
340 =head2 is_in_list
341  
342 my $ok = is_in_list($value, \@list);
343  
344 Helpers: true when C<$value> equals one element of C<\@list>.
345  
346 =head1 BUGS
347  
348 The fork version of C<iban_letter_to_number> read the global C<$1> instead of its
349 argument. It worked by accident inside C<is_valid_iban_checksum>, where C<$1>
350 was set by the enclosing C<s///e>, and returned C<-55> for any letter when called
351 directly. The port reads C<$char>; see F<t/int/30_iban.t>.
352  
353 =head1 AUTHOR
354  
355 The oEdtk team.
356  
357 =cut
« blib/lib/oEdtk/Ext.pm ↑ Summary blib/lib/oEdtk/Dict.pm »