| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Business::CPI::Util::Types; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: Basic types for Business::CPI |
|
3
|
10
|
|
|
10
|
|
941
|
use warnings; |
|
|
10
|
|
|
|
|
12
|
|
|
|
10
|
|
|
|
|
321
|
|
|
4
|
10
|
|
|
10
|
|
36
|
use strict; |
|
|
10
|
|
|
|
|
16
|
|
|
|
10
|
|
|
|
|
297
|
|
|
5
|
10
|
|
|
10
|
|
37
|
use Scalar::Util qw/looks_like_number blessed/; |
|
|
10
|
|
|
|
|
11
|
|
|
|
10
|
|
|
|
|
734
|
|
|
6
|
10
|
|
|
10
|
|
183
|
use List::Util qw/first/; |
|
|
10
|
|
|
|
|
10
|
|
|
|
10
|
|
|
|
|
1579
|
|
|
7
|
10
|
|
|
10
|
|
4178
|
use Locale::Country (); |
|
|
10
|
|
|
|
|
182841
|
|
|
|
10
|
|
|
|
|
277
|
|
|
8
|
10
|
|
|
10
|
|
5702
|
use Email::Valid (); |
|
|
10
|
|
|
|
|
924625
|
|
|
|
10
|
|
|
|
|
366
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
10
|
|
|
10
|
|
5297
|
use Type::Utils -all; |
|
|
10
|
|
|
|
|
225200
|
|
|
|
10
|
|
|
|
|
287
|
|
|
11
|
10
|
|
|
10
|
|
29783
|
use Types::Standard qw/Str/; |
|
|
10
|
|
|
|
|
299647
|
|
|
|
10
|
|
|
|
|
119
|
|
|
12
|
|
|
|
|
|
|
use Type::Library |
|
13
|
10
|
|
|
|
|
66
|
-base, |
|
14
|
|
|
|
|
|
|
-declare => qw( |
|
15
|
|
|
|
|
|
|
DateTime Country Money PhoneNumber |
|
16
|
|
|
|
|
|
|
ExceptionType UserAgent HTTPResponse |
|
17
|
10
|
|
|
10
|
|
6820
|
); |
|
|
10
|
|
|
|
|
18
|
|
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.924'; # VERSION |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
enum ExceptionType, [qw. |
|
22
|
|
|
|
|
|
|
invalid_data |
|
23
|
|
|
|
|
|
|
incomplete_data |
|
24
|
|
|
|
|
|
|
invalid_request |
|
25
|
|
|
|
|
|
|
resource_not_found |
|
26
|
|
|
|
|
|
|
unauthorized |
|
27
|
|
|
|
|
|
|
unauthenticated |
|
28
|
|
|
|
|
|
|
duplicate_transaction |
|
29
|
|
|
|
|
|
|
rejected |
|
30
|
|
|
|
|
|
|
gateway_unavailable |
|
31
|
|
|
|
|
|
|
gateway_error |
|
32
|
|
|
|
|
|
|
unknown |
|
33
|
|
|
|
|
|
|
.]; |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
class_type DateTime, { class => "DateTime" }; |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
class_type UserAgent, { class => 'LWP::UserAgent' }; |
|
38
|
|
|
|
|
|
|
class_type HTTPResponse, { class => 'HTTP::Response' }; |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my @CountryCodes = Locale::Country::all_country_codes(); |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
declare Country, as Str, |
|
43
|
|
|
|
|
|
|
where { |
|
44
|
|
|
|
|
|
|
my $re = qr/^$_$/; |
|
45
|
|
|
|
|
|
|
return !! first { m|$re| } @CountryCodes; |
|
46
|
|
|
|
|
|
|
}; |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
coerce Country, |
|
49
|
|
|
|
|
|
|
from Str, |
|
50
|
|
|
|
|
|
|
via { |
|
51
|
|
|
|
|
|
|
my $country = lc $_; |
|
52
|
|
|
|
|
|
|
my $re = qr/^$country$/; |
|
53
|
|
|
|
|
|
|
if (first { m|$re| } @CountryCodes) { |
|
54
|
|
|
|
|
|
|
return $country; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
return Locale::Country::country2code($country) || ''; |
|
57
|
|
|
|
|
|
|
}; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
declare Money, |
|
60
|
|
|
|
|
|
|
as Str, |
|
61
|
|
|
|
|
|
|
where { m|^ \-? [\d\,]+ \. \d{2} $|x }; |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
coerce Money, |
|
64
|
|
|
|
|
|
|
from Str, |
|
65
|
|
|
|
|
|
|
via { |
|
66
|
|
|
|
|
|
|
my $r = looks_like_number($_) ? $_ : 0; |
|
67
|
|
|
|
|
|
|
return sprintf( "%.2f", 0+$r); |
|
68
|
|
|
|
|
|
|
}; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
declare PhoneNumber, |
|
71
|
|
|
|
|
|
|
as Str, |
|
72
|
|
|
|
|
|
|
where { m|^ \+? \d+ $|x }; |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
coerce PhoneNumber, |
|
75
|
|
|
|
|
|
|
from Str, |
|
76
|
|
|
|
|
|
|
via { |
|
77
|
|
|
|
|
|
|
# avoid warnings |
|
78
|
|
|
|
|
|
|
return '' unless defined $_; |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# force it to stringify |
|
81
|
|
|
|
|
|
|
my $r = "$_"; |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Remove anything that is not alphanumerical or "+" |
|
84
|
|
|
|
|
|
|
# Note that we are using \w instead of \d here, because this sub is used |
|
85
|
|
|
|
|
|
|
# only for coersion. We don't want to remove letters from the phone number, |
|
86
|
|
|
|
|
|
|
# we want it to fail in the `is_valid_phone_number` routine. |
|
87
|
|
|
|
|
|
|
$r =~ s{[^\+\w]}{}g; |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
return $r; |
|
90
|
|
|
|
|
|
|
}; |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__END__ |