File Coverage

blib/lib/Finance/MIFIR/CONCAT.pm
Criterion Covered Total %
statement 45 45 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 56 56 100.0


line stmt bran cond sub pod time code
1             package Finance::MIFIR::CONCAT;
2 2     2   284941 use 5.014;
  2         23  
3 2     2   14 use warnings;
  2         6  
  2         232  
4 2     2   16 use strict;
  2         4  
  2         130  
5              
6             our $VERSION = '0.02';
7              
8 2     2   1406 use Date::Utility;
  2         1673936  
  2         122  
9 2     2   27 use Exporter 'import';
  2         3  
  2         92  
10 2     2   11 use File::ShareDir;
  2         24  
  2         151  
11 2     2   1236 use YAML::XS qw/LoadFile/;
  2         16885  
  2         296  
12 2     2   25 use utf8;
  2         3  
  2         23  
13             our @EXPORT_OK = qw(mifir_concat);
14              
15             =head1 NAME
16              
17             Finance::MIFIR::CONCAT - provides CONCAT code generation out of client data according to MIFIR rules
18              
19             =head1 SYNOPSIS
20              
21             use Finance::MIFIR::CONCAT qw/mifir_concat/;
22              
23             print mifir_concat({
24             cc => 'DE',
25             date => '1960-01-01',
26             first_name => 'Jack',
27             last_name => 'Daniels',
28             });
29              
30             =head1 DESCRIPTION
31              
32             =cut
33              
34             =head2 mifir_concat
35              
36             Accepts hashref of person's data with keys: cc, date, first_name, last_name.
37              
38             Returns string representing CONCATed MIFIR ID.
39              
40             =cut
41              
42             our $config = LoadFile(File::ShareDir::dist_file('Finance-MIFIR-CONCAT', 'mifir.yml'));
43             our $romanization = LoadFile(File::ShareDir::dist_file('Finance-MIFIR-CONCAT', 'romanization.yml'));
44              
45             sub mifir_concat {
46 6     6 1 272763 my $args = shift;
47 6         19 my $cc = $args->{cc};
48 6         210 my $date = Date::Utility->new($args->{date})->date_yyyymmdd;
49 6         10599 $date =~ s/\-//g;
50 6         27 my $first_name = _process_name($args->{first_name});
51 6         24 my $last_name = _process_name($args->{last_name});
52 6         77 return uc($cc . $date . $first_name . $last_name);
53             }
54              
55             sub _process_name {
56 46     46   77279 my ($str) = @_;
57 46         221 $str = lc($str);
58              
59             # Remove titles and prefixes with word boundary
60 46         109 my $strip_re = join "|", (@{$config->{titles}}, @{$config->{prefixes}});
  46         194  
  46         600  
61 46         867 $strip_re = qr/\b($strip_re)\s+/i;
62 46         542 $str =~ s/$strip_re//g;
63 46         189 $str =~ s/\s+.*$//; # Remove everything after first space
64              
65             # Romanization
66 46         2154 my $re = join '', keys %$romanization;
67 46         920 $re = qr/([$re])/;
68 46         396 $str =~ s/$re/$romanization->{$1}/ge;
  156         816  
69              
70             # Remove non-alphabetic characters
71 46         154 $str =~ s/[^a-z]//g;
72              
73             # Pad to 5 characters
74 46         192 $str = substr($str . '######', 0, 5);
75 46         327 return $str;
76             }
77             1;