File Coverage

blib/lib/Business/RO/CIF.pm
Criterion Covered Total %
statement 51 53 96.2
branch 12 14 85.7
condition 2 3 66.6
subroutine 8 8 100.0
pod 2 2 100.0
total 75 80 93.7


line stmt bran cond sub pod time code
1             package Business::RO::CIF;
2             $Business::RO::CIF::VERSION = '0.005';
3             # ABSTRACT: Romanian CIF validation
4              
5 2     2   33054 use Moo;
  2         27732  
  2         15  
6 2     2   2924 use 5.010;
  2         8  
  2         83  
7 2     2   1292 use utf8;
  2         21  
  2         9  
8 2     2   1167 use Types::Standard qw(Int ArrayRef Str);
  2         142929  
  2         33  
9              
10             has 'cif' => (
11             is => 'ro',
12             isa => Str,
13             required => 1,
14             );
15              
16             has 'errstr' => (
17             is => 'rw',
18             isa => Str,
19             default => sub {''},
20             );
21              
22             has 'rev_key' => (
23             is => 'lazy',
24             isa => ArrayRef,
25             init_arg => undef,
26             );
27              
28             sub _build_rev_key {
29 9     9   557 my $self = shift;
30 9         33 my @revkey = reverse split //, '753217532';
31 9         133 return \@revkey;
32             }
33              
34             has 'checksum' => (
35             is => 'rw',
36             isa => Int,
37             init_arg => undef,
38             );
39              
40             has 'rev_cif' => (
41             is => 'lazy',
42             isa => ArrayRef,
43             init_arg => undef,
44             );
45              
46             sub _build_rev_cif {
47 9     9   618 my $self = shift;
48 9 50       22 if ( my $cif = $self->cif ) {
49 9         37 my @revcif = reverse split //, $cif;
50 9         141 $self->checksum(shift @revcif);
51 9         1434 return \@revcif;
52             }
53             else {
54 0         0 die "No CIF?";
55             }
56             }
57              
58             sub valid {
59 10     10 1 3292 my $self = shift;
60              
61 10 100       48 if ( $self->cif =~ m{[^0-9]} ) {
62 1         4 $self->errstr('The input string contains invalid characters');
63 1         556 return 0;
64             }
65              
66 9         9 my @rev_cif = @{ $self->rev_cif };
  9         194  
67 9         136 my @rev_key = @{ $self->rev_key };
  9         121  
68              
69 9         130 my $len = scalar @rev_cif;
70 9 100       21 if ($len < 5) {
71 1         18 $self->errstr('The input is too short (< 5)');
72 1         23 return 0;
73             }
74 8 100       15 if ($len > 9) {
75 1         16 $self->errstr('The input is too long (> 9)');
76 1         17 return 0;
77             }
78              
79 7         7 my $sum = 0;
80 7         14 foreach ( 0 .. $#rev_cif ) {
81 48         62 $sum += $rev_cif[$_] * $rev_key[$_];
82             }
83              
84 7         14 my $m11 = $sum * 10 % 11;
85 7 50       10 my $ctc = $m11 == 10 ? 0 : $m11;
86              
87 7 100       127 if ( $self->checksum == $ctc ) {
88 5         42 return 1;
89             }
90             else {
91 2         40 $self->errstr('The checksum failed');
92 2         36 return 0;
93             }
94             return
95 0         0 }
96              
97             sub BUILDARGS {
98 10     10 1 5516 my ( $class, @args ) = @_;
99 10 100 66     39 if ( @args == 1 && !ref $args[0] ) {
100 1         4 $args[0] =~ s{^RO\s*}{}i;
101 1         17 return { cif => $args[0] };
102             }
103             else {
104 9         19 my %para = @args;
105 9         22 $para{cif} =~ s{^RO\s*}{}i;
106 9         146 return \%para;
107             }
108             }
109              
110             1;
111              
112             __END__