| 1 | package CLDR::Number::Format::Currency; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use v5.8.1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use CLDR::Number::Constant qw( $C ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use CLDR::Number::Data::Currency; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 11 | our $VERSION = '0.17'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 11 | with qw( CLDR::Number::Role::Format ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has currency_code => ( | ||||||||||||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||||||||||||
| 18 | isa => sub { | ||||||||||||||||||||||||||||
| 19 | 86 | ▶ | croak 'uninitialized value' if !defined $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 86 | ▶ | croak qq{invalid value "$_[0]"} if $_[0] !~ m{ ^ [A-Z]{3} $ }x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | }, | ||||||||||||||||||||||||||||
| 22 | 86 | ▶ | coerce => sub { defined $_[0] ? uc $_[0] : $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 11 | trigger => 1, | |||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | has currency_sign => ( | ||||||||||||||||||||||||||||
| 27 | is => 'rw', | ||||||||||||||||||||||||||||
| 28 | isa => sub { | ||||||||||||||||||||||||||||
| 29 | 174 | ▶ | croak "currency_sign is not defined" if !defined $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | }, | ||||||||||||||||||||||||||||
| 31 | 11 | ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # TODO: accounting NYI | ||||||||||||||||||||||||||||
| 34 | has accounting => ( | ||||||||||||||||||||||||||||
| 35 | is => 'rw', | ||||||||||||||||||||||||||||
| 36 | 81 | ▶ | coerce => sub { $_[0] ? 1 : 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 11 | default => 0, | |||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | has cash => ( | ||||||||||||||||||||||||||||
| 41 | is => 'rw', | ||||||||||||||||||||||||||||
| 42 | 81 | ▶ | coerce => sub { $_[0] ? 1 : 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 11 | trigger => 1, | |||||||||||||||||||||||||||
| 44 | default => 0, | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 11 | has _pattern_type => ( | |||||||||||||||||||||||||||
| 48 | is => 'ro', | ||||||||||||||||||||||||||||
| 49 | default => 'currency', | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | after _trigger_locale => sub { | ||||||||||||||||||||||||||||
| 53 | 87 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 87 | ▶ | if ($self->currency_code) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 82 | $self->_build_currency_sign; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 87 | ▶ | if (my $decimal = $self->_get_data(symbol => 'currency_decimal')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 2 | $self->decimal_sign($decimal); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 11 | }; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | ▶ | sub BUILD {} | |||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub _trigger_currency_code { | ||||||||||||||||||||||||||||
| 67 | 86 | ▶ | my ($self, $currency_code) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_trigger_currency_code: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 86 | $self->_build_currency_sign; | |||||||||||||||||||||||||||
| 70 | 86 | $self->_trigger_cash; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub _build_currency_sign { | ||||||||||||||||||||||||||||
| 74 | 168 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_currency_sign: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 75 | 168 | my $data = $CLDR::Number::Data::Currency::LOCALES; | |||||||||||||||||||||||||||
| 76 | 168 | my $currency_sign; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 168 | ▶ | return if $self->_has_init_arg('currency_sign'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 166 | for my $locale (@{$self->_locale_inheritance}) { | |||||||||||||||||||||||||||
| 81 | 169 | ▶ | next if !exists $data->{$locale} || !exists $data->{$locale}{$self->currency_code}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $data->{$locale} and exists $data->{$locale}{$self->currency_code}
MC/DC100%2/2
exists $data->{$locale} and exists $data->{$locale}{$self->currency_code}
exists $data->{$locale}
exists $data->{$locale}{$self->currency_code}
Truth table100%3/3
A and B
A= exists $data->{$locale}B= exists $data->{$locale}{$self->currency_code}
| |||||||||||||||||||||||||||||
| 82 | 80 | $currency_sign = $data->{$locale}{$self->currency_code}; | |||||||||||||||||||||||||||
| 83 | 80 | last; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 166 | ▶ | $self->currency_sign($currency_sign || $self->currency_code); | ||||||||||||||||||||||||||
|
Condition66%2/3
$currency_sign || $self->currency_code
MC/DC0%0/2
$currency_sign || $self->currency_code
$currency_sign
$self->currency_code
Truth table66%2/3
A || B
A= $currency_signB= $self->currency_code
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub _trigger_cash { | ||||||||||||||||||||||||||||
| 90 | 86 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_trigger_cash: called CC=7 SCAR=22.8
| |||||||||||||||||||||||||||||
| 91 | 86 | my $currencies = $CLDR::Number::Data::Currency::CURRENCIES; | |||||||||||||||||||||||||||
| 92 | my $currency_data | ||||||||||||||||||||||||||||
| 93 | = $self->currency_code && exists $currencies->{$self->currency_code} | ||||||||||||||||||||||||||||
| 94 | ? $currencies->{$self->currency_code} | ||||||||||||||||||||||||||||
| 95 | 86 | ▶ | : $currencies->{DEFAULT}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->currency_code and exists $$currencies{$self->currency_code}
MC/DC50%1/2
$self->currency_code and exists $$currencies{$self->currency_code}
$self->currency_code
exists $$currencies{$self->currency_code}
Truth table66%2/3
A and B
A= $self->currency_codeB= exists $$currencies{$self->currency_code}
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 86 | ▶ | if ($self->cash && exists $currency_data->{cash_digits}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->cash and exists $currency_data->{"cash_digits"}
MC/DC0%0/2
$self->cash and exists $currency_data->{"cash_digits"}
$self->cash
exists $currency_data->{"cash_digits"}
Truth table33%1/3
A and B
A= $self->cashB= exists $currency_data->{"cash_digits"}
| |||||||||||||||||||||||||||||
| 98 | 0 | $self->minimum_fraction_digits($currency_data->{cash_digits}); | |||||||||||||||||||||||||||
| 99 | 0 | $self->maximum_fraction_digits($currency_data->{cash_digits}); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | 86 | $self->minimum_fraction_digits($currency_data->{digits}); | |||||||||||||||||||||||||||
| 103 | 86 | $self->maximum_fraction_digits($currency_data->{digits}); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 86 | ▶ | if ($self->cash && exists $currency_data->{cash_rounding}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->cash and exists $currency_data->{"cash_rounding"}
MC/DC0%0/2
$self->cash and exists $currency_data->{"cash_rounding"}
$self->cash
exists $currency_data->{"cash_rounding"}
Truth table33%1/3
A and B
A= $self->cashB= exists $currency_data->{"cash_rounding"}
| |||||||||||||||||||||||||||||
| 107 | 0 | $self->rounding_increment($currency_data->{cash_rounding}); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||||||||||
| 110 | 86 | $self->rounding_increment($currency_data->{rounding}); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub format { | ||||||||||||||||||||||||||||
| 115 | 111 | ▶ | my ($self, $num) = @_; | ||||||||||||||||||||||||||
|
Subroutine
format: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 111 | $num = $self->_validate_number(format => $num); | |||||||||||||||||||||||||||
| 118 | 111 | ▶ | return undef unless defined $num; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 110 | ▶ | croak 'Missing required attribute: currency_code' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | unless $self->currency_code; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 109 | my $sign = $self->currency_sign; | |||||||||||||||||||||||||||
| 124 | 109 | my $format = $self->_format_number($num); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # spacing before currency sign | ||||||||||||||||||||||||||||
| 127 | 109 | ▶ | if ($sign =~ m{ ^ \PS }x && $format =~ m{ \d $C }x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$sign =~ / ^ \PS /x and $format =~ / \d $CLDR::Number::Format::Currency::C /x
MC/DC100%2/2
$sign =~ / ^ \PS /x and $format =~ / \d $CLDR::Number::Format::Currency::C /x
$sign =~ / ^ \PS /x
$format =~ / \d $CLDR::Number::Format::Currency::C /x
Truth table100%3/3
A and B
A= $sign =~ / ^ \PS /xB= $format =~ / \d $CLDR::Number::Format::Currency::C /x
| |||||||||||||||||||||||||||||
| 128 | 4 | $sign = ' ' . $sign; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # spacing after currency sign | ||||||||||||||||||||||||||||
| 132 | 109 | ▶ | if ($sign =~ m{ \PS $ }x && $format =~ m{ $C \d }x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$sign =~ / \PS $ /x and $format =~ / $CLDR::Number::Format::Currency::C \d /x
MC/DC100%2/2
$sign =~ / \PS $ /x and $format =~ / $CLDR::Number::Format::Currency::C \d /x
$sign =~ / \PS $ /x
$format =~ / $CLDR::Number::Format::Currency::C \d /x
Truth table100%3/3
A and B
A= $sign =~ / \PS $ /xB= $format =~ / $CLDR::Number::Format::Currency::C \d /x
| |||||||||||||||||||||||||||||
| 133 | 14 | $sign .= ' '; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 109 | $format =~ s{$C}{$sign}; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 109 | return $format; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 11 | 1; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||