line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Business::CPI::Util::Types; |
2
|
|
|
|
|
|
|
# ABSTRACT: Basic types for Business::CPI |
3
|
9
|
|
|
9
|
|
40
|
use warnings; |
|
9
|
|
|
|
|
11
|
|
|
9
|
|
|
|
|
298
|
|
4
|
9
|
|
|
9
|
|
38
|
use strict; |
|
9
|
|
|
|
|
11
|
|
|
9
|
|
|
|
|
301
|
|
5
|
9
|
|
|
9
|
|
38
|
use Scalar::Util qw/looks_like_number blessed/; |
|
9
|
|
|
|
|
11
|
|
|
9
|
|
|
|
|
641
|
|
6
|
9
|
|
|
9
|
|
71
|
use List::Util qw/first/; |
|
9
|
|
|
|
|
12
|
|
|
9
|
|
|
|
|
763
|
|
7
|
9
|
|
|
9
|
|
4330
|
use Locale::Country (); |
|
9
|
|
|
|
|
171038
|
|
|
9
|
|
|
|
|
213
|
|
8
|
9
|
|
|
9
|
|
4901
|
use Email::Valid (); |
|
9
|
|
|
|
|
907477
|
|
|
9
|
|
|
|
|
358
|
|
9
|
|
|
|
|
|
|
|
10
|
9
|
|
|
9
|
|
5080
|
use Type::Utils -all; |
|
9
|
|
|
|
|
221264
|
|
|
9
|
|
|
|
|
190
|
|
11
|
9
|
|
|
9
|
|
36836
|
use Types::Standard qw/Str/; |
|
9
|
|
|
|
|
292984
|
|
|
9
|
|
|
|
|
142
|
|
12
|
|
|
|
|
|
|
use Type::Library |
13
|
9
|
|
|
|
|
66
|
-base, |
14
|
|
|
|
|
|
|
-declare => qw( |
15
|
|
|
|
|
|
|
DateTime Country Money PhoneNumber |
16
|
|
|
|
|
|
|
ExceptionType UserAgent HTTPResponse |
17
|
9
|
|
|
9
|
|
6387
|
); |
|
9
|
|
|
|
|
15
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.922'; # 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__ |