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.003'; # TRIAL
3             # ABSTRACT: Romanian CIF validation
4              
5 2     2   45079 use Moo;
  2         34908  
  2         51  
6 2     2   3300 use 5.010;
  2         6  
  2         75  
7 2     2   1467 use utf8;
  2         19  
  2         9  
8 2     2   1629 use Types::Standard qw(Int ArrayRef Str);
  2         217592  
  2         39  
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   593 my $self = shift;
30 9         32 my @revkey = reverse split //, '753217532';
31 9         127 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   582 my $self = shift;
48 9 50       25 if ( my $cif = $self->cif ) {
49 9         38 my @revcif = reverse split //, $cif;
50 9         132 $self->checksum(shift @revcif);
51 9         994 return \@revcif;
52             }
53             else {
54 0         0 die "No CIF?";
55             }
56             }
57              
58             sub valid {
59 10     10 1 4320 my $self = shift;
60              
61 10 100       59 if ( $self->cif =~ m{[^0-9]} ) {
62 1         5 $self->errstr('The input string contains invalid characters');
63 1         531 return 0;
64             }
65              
66 9         11 my @rev_cif = @{ $self->rev_cif };
  9         197  
67 9         157 my @rev_key = @{ $self->rev_key };
  9         126  
68              
69 9         140 my $len = scalar @rev_cif;
70 9 100       24 if ($len < 5) {
71 1         18 $self->errstr('The input is too short (< 5)');
72 1         27 return 0;
73             }
74 8 100       16 if ($len > 9) {
75 1         16 $self->errstr('The input is too long (> 9)');
76 1         19 return 0;
77             }
78              
79 7         7 my $sum = 0;
80 7         15 foreach ( 0 .. $#rev_cif ) {
81 48         61 $sum += $rev_cif[$_] * $rev_key[$_];
82             }
83              
84 7         15 my $m11 = $sum * 10 % 11;
85 7 50       13 my $ctc = $m11 == 10 ? 0 : $m11;
86              
87 7 100       127 if ( $self->checksum == $ctc ) {
88 5         45 return 1;
89             }
90             else {
91 2         39 $self->errstr('The checksum failed');
92 2         38 return 0;
93             }
94             return
95 0         0 }
96              
97             sub BUILDARGS {
98 10     10 1 6278 my ( $class, @args ) = @_;
99 10 100 66     45 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         24 my %para = @args;
105 9         23 $para{cif} =~ s{^RO\s*}{}i;
106 9         173 return \%para;
107             }
108             }
109              
110             1;
111              
112             __END__