File Coverage

blib/lib/Business/IBAN/Validator.pm
Criterion Covered Total %
statement 44 45 97.7
branch 11 12 91.6
condition n/a
subroutine 8 9 88.8
pod 3 3 100.0
total 66 69 95.6


line stmt bran cond sub pod time code
1             package Business::IBAN::Validator;
2 3     3   228425 use warnings;
  3         18  
  3         98  
3 3     3   18 use strict;
  3         6  
  3         111  
4              
5             our $VERSION = "0.07_01";
6              
7 3     3   1627 use Hash::Util qw/unlock_hash lock_hash/;
  3         8727  
  3         18  
8 3     3   1646 use Business::IBAN::Database;
  3         14  
  3         1769  
9              
10             sub new {
11 2     2 1 194 my $class = shift;
12              
13 2         14 my $db = iban_db();
14 2         11 unlock_hash(%$db);
15 2         253 my $self = bless $db, $class;
16 2         16 lock_hash(%$self);
17 2         235 return $self;
18             }
19              
20             sub validate {
21 174     174 1 124293 my $self = shift;
22 174         374 my ($iban) = @_;
23              
24 174         901 (my $to_check = $iban) =~ s/\s+//g;
25 174         441 my $iso3166a2 = uc(substr($to_check, 0, 2));
26              
27 174 100       498 if (not exists($self->{$iso3166a2})) {
28 1         10 die "'$iso3166a2' is not an IBAN country code.\n";
29             }
30              
31 173         315 my $iban_info = $self->{$iso3166a2};
32 173 100       618 if (length($to_check) != $iban_info->{iban_length}) {
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         11 );
42             }
43              
44 172 100       2618 if ($to_check !~ $iban_info->{iban_structure}) {
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         11 );
53             }
54              
55 171 100       629 if ( mod97(numify_iban($to_check)) != 1) {
56 1         8 die "'$iban' does not comply with the 97-check.\n";
57             }
58              
59 170         481 return 1;
60             }
61              
62             sub is_sepa {
63 170     170 1 51561 my $self = shift;
64 170         334 my ($iban) = @_;
65 170         950 (my $to_check = $iban) =~ s/\s+//g;
66 170         432 my $iso3166a2 = uc(substr($to_check, 0, 2));
67              
68 170 50       449 if (not exists($self->{$iso3166a2})) {
69 0         0 die "'$iso3166a2' is not an IBAN country code.\n";
70             }
71              
72 170         713 return $self->{$iso3166a2}{is_sepa};
73             }
74              
75       0     sub DESTROY { }
76              
77             sub AUTOLOAD {
78 2     2   1146 my $self = shift;
79 2         15 (my $cc = our $AUTOLOAD) =~ s/.*:://;
80 2 100       11 if (!exists $self->{uc $cc}) {
81 1         6 require Carp;
82 1         175 Carp::croak("'$cc' is not a valid IBAN country code.");
83             }
84 1         5 return $self->{uc $cc};
85             }
86              
87             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 MMXIII-MMXV - Abe Timmerman
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