Validator.pm

filter: Statementstmt 97.8% 46 / 47 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 95.7% 68 / 71 CC 7 SCAR 9.6
CC
7
Cov
97%
validate
5.0
is_sepa
2.0
AUTOLOAD
2.0
CRAP 2.6
↑ Summary Util.pm »
1 package Business::IBAN::Validator;
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = "0.08";
6  
7 3 ▶use Hash::Util qw/unlock_hash lock_hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Business::IBAN::Database;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
11 2 my $class = shift;
12  
13 2 my $db = iban_db();
14 2 unlock_hash(%$db);
15 2 my $self = bless $db, $class;
16 2 lock_hash(%$self);
17 2 return $self;
18 }
19  
20 174 ▶sub validate {
Subroutine validate: called CC=5 SCAR=16.1
Pod documented
21 174 my $self = shift;
22 174 my ($iban) = @_;
23  
24 174 (my $to_check = $iban) =~ s/\s+//g;
25 174 my $iso3166a2 = uc(substr($to_check, 0, 2));
26  
27 174 ▶ if (not exists($self->{$iso3166a2})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$iso3166a2}) 1 173 174
28 1 die "'$iso3166a2' is not an IBAN country code.\n";
29 }
30  
31 173 my $iban_info = $self->{$iso3166a2};
32 173 ▶ if (length($to_check) != $iban_info->{iban_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $to_check != $iban_info->{"iban_length"}) 1 172 173
33 die(
34 sprintf(
35 "'%s' has incorrect length %d (expected %d for %s).\n",
36 $iban,
37 length($to_check),
38 $iban_info->{iban_length},
39 $iban_info->{country}
40 )
41 1 );
42 }
43  
44 172 ▶ if ($to_check !~ $iban_info->{iban_structure}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($to_check =~ /$iban_info->{"iban_structure"}/) 1 171 172
45 die(
46 sprintf(
47 "'%s' does not match the pattern '%s'for %s.\n",
48 $iban,
49 $iban_info->{pattern},
50 $iban_info->{country}
51 )
52 1 );
53 }
54  
55 171 ▶ if ( mod97(numify_iban($to_check)) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (mod97(numify_iban($to_check)) != 1) 1 170 171
56 1 die "'$iban' does not comply with the 97-check.\n";
57 }
58  
59 170 return 1;
60 }
61  
62 170 ▶sub is_sepa {
Subroutine is_sepa: called CC=2 SCAR=7.1
Pod documented
63 170 my $self = shift;
64 170 my ($iban) = @_;
65 170 (my $to_check = $iban) =~ s/\s+//g;
66 170 my $iso3166a2 = uc(substr($to_check, 0, 2));
67  
68 170 ▶ if (not exists($self->{$iso3166a2})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$iso3166a2}) 0 170 170
69 0 die "'$iso3166a2' is not an IBAN country code.\n";
70 }
71  
72 170 return $self->{$iso3166a2}{is_sepa};
73 }
74  
75 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
76  
77 2 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
78 2 my $self = shift;
79 2 (my $cc = our $AUTOLOAD) =~ s/.*:://;
80 2 ▶ if (!exists $self->{uc $cc}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$self{uc $cc}) 1 1 2
81 1 require Carp;
82 1 Carp::croak("'$cc' is not a valid IBAN country code.");
83 }
84 1 return $self->{uc $cc};
85 }
86  
87 3 1;
88  
89 =head1 NAME
90  
91 Business::IBAN::Validator - A validator for the structure of IBANs.
92  
93 =head1 SYNOPSIS
94  
95 use Business::IBAN::Validator;
96 my $v = Business::IBAN::Validator->new;
97 while (1) {
98 print 'Enter IBAN: ';
99 chomp(my $input = <>);
100 last if !$input;
101 eval { $v->validate($input) };
102 if ($@) {
103 print "Not ok: $@";
104 }
105 else {
106 print "OK\n";
107 }
108 }
109  
110 =head1 DESCRIPTION
111  
112 This module does a series of checks on an IBAN:
113  
114 =over
115  
116 =item Country code
117  
118 =item Length
119  
120 =item Pattern
121  
122 =item 97-Check
123  
124 =back
125  
126 =head2 $v = Business::IBAN::Validator->new()
127  
128 Return an IBAN validator object.
129  
130 =head2 $v->validate($iban)
131  
132 Perform a series of checks, and die() as soon as one fails.
133  
134 Return 1 on success.
135  
136 =head2 $v->is_sepa($iban)
137  
138 Return the SEPA status of the country (as denoted by the first two letters).
139  
140 =head1 COPYRIGHT
141  
142 E<copy> MMXIII-MMXV - Abe Timmerman <abeltje@cpan.org>
143  
144 =head1 LICENSE
145  
146 This library is free software; you can redistribute it and/or modify
147 it under the same terms as Perl itself.
148  
149 This program is distributed in the hope that it will be useful,
150 but WITHOUT ANY WARRANTY; without even the implied warranty of
151 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
152  
153 =cut
↑ Summary Util.pm »