| 1 | package Business::CPI::Util::Types; | ||||||||||||
| 2 | # ABSTRACT: Basic types for Business::CPI | ||||||||||||
| 3 | 10 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 10 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 10 | ▶ | use Scalar::Util qw/looks_like_number blessed/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 10 | ▶ | use List::Util qw/first/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 10 | ▶ | use Locale::Country (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 10 | ▶ | use Email::Valid (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 10 | ▶ | use Type::Utils -all; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 10 | ▶ | use Types::Standard qw/Str/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | use Type::Library | ||||||||||||
| 13 | 10 | ▶ | -base, | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | -declare => qw( | ||||||||||||
| 15 | DateTime Country Money PhoneNumber | ||||||||||||
| 16 | ExceptionType UserAgent HTTPResponse | ||||||||||||
| 17 | 10 | ); | |||||||||||
| 18 | |||||||||||||
| 19 | 10 | our $VERSION = '0.924'; # VERSION | |||||||||||
| 20 | |||||||||||||
| 21 | 10 | 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 | 10 | class_type DateTime, { class => "DateTime" }; | |||||||||||
| 36 | |||||||||||||
| 37 | 10 | class_type UserAgent, { class => 'LWP::UserAgent' }; | |||||||||||
| 38 | 10 | class_type HTTPResponse, { class => 'HTTP::Response' }; | |||||||||||
| 39 | |||||||||||||
| 40 | 10 | my @CountryCodes = Locale::Country::all_country_codes(); | |||||||||||
| 41 | |||||||||||||
| 42 | declare Country, as Str, | ||||||||||||
| 43 | 25 | ▶ | where { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | 25 | my $re = qr/^$_$/; | |||||||||||
| 45 | 25 | ▶ | return !! first { m|$re| } @CountryCodes; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 46 | 10 | }; | |||||||||||
| 47 | |||||||||||||
| 48 | coerce Country, | ||||||||||||
| 49 | from Str, | ||||||||||||
| 50 | 7 | ▶ | via { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 51 | 7 | my $country = lc $_; | |||||||||||
| 52 | 7 | my $re = qr/^$country$/; | |||||||||||
| 53 | 7 | ▶ | if (first { m|$re| } @CountryCodes) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 54 | 5 | return $country; | |||||||||||
| 55 | } | ||||||||||||
| 56 | 2 | ▶ | return Locale::Country::country2code($country) || ''; | ||||||||||
|
Condition100%2/2
Locale::Country::country2code($country) || ''
MC/DC100%1/1
Locale::Country::country2code($country) || ''
Locale::Country::country2code($country)
Truth table100%2/2
A
A= Locale::Country::country2code($country)
| |||||||||||||
| 57 | 10 | }; | |||||||||||
| 58 | |||||||||||||
| 59 | declare Money, | ||||||||||||
| 60 | as Str, | ||||||||||||
| 61 | 136 | ▶ | where { m|^ \-? [\d\,]+ \. \d{2} $|x }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 62 | |||||||||||||
| 63 | coerce Money, | ||||||||||||
| 64 | from Str, | ||||||||||||
| 65 | 46 | ▶ | via { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 66 | 46 | ▶ | my $r = looks_like_number($_) ? $_ : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 46 | return sprintf( "%.2f", 0+$r); | |||||||||||
| 68 | 10 | }; | |||||||||||
| 69 | |||||||||||||
| 70 | declare PhoneNumber, | ||||||||||||
| 71 | as Str, | ||||||||||||
| 72 | 2 | ▶ | where { m|^ \+? \d+ $|x }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 73 | |||||||||||||
| 74 | coerce PhoneNumber, | ||||||||||||
| 75 | from Str, | ||||||||||||
| 76 | 1 | ▶ | via { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 77 | # avoid warnings | ||||||||||||
| 78 | 1 | ▶ | return '' unless defined $_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | |||||||||||||
| 80 | # force it to stringify | ||||||||||||
| 81 | 1 | 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 | 1 | $r =~ s{[^\+\w]}{}g; | |||||||||||
| 88 | |||||||||||||
| 89 | 1 | return $r; | |||||||||||
| 90 | 10 | }; | |||||||||||
| 91 | |||||||||||||
| 92 | 10 | 1; | |||||||||||
| 93 | |||||||||||||
| 94 | __END__ | ||||||||||||