« Westpac/PaymentsPlus/Australian/Payment/Import/File.pm
↑ Summary
Westpac/PaymentsPlus/Australian/Payment/Import/Invoice.pm »
| 1 | package Business::Westpac::Types; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Business::Westpac::Types | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | use Business::Westpac::Types qw/ | ||||||||||
| 10 | add_max_string_attribute | ||||||||||
| 11 | /; | ||||||||||
| 12 | |||||||||||
| 13 | has [ qw/ | ||||||||||
| 14 | funding_bsb_number | ||||||||||
| 15 | / ] => ( | ||||||||||
| 16 | is => 'ro', | ||||||||||
| 17 | isa => 'BSBNumber', | ||||||||||
| 18 | required => 0, | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | ... | ||||||||||
| 22 | |||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||
| 24 | |||||||||||
| 25 | Package for defining type constraints for use in the Business::Westpac | ||||||||||
| 26 | namespace. | ||||||||||
| 27 | |||||||||||
| 28 | =cut | ||||||||||
| 29 | |||||||||||
| 30 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 11 | ▶ | use feature qw/ signatures /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | 11 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 11 | ▶ | no warnings qw/ experimental::signatures /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 11 | ▶ | use DateTime::Format::DateParse; ## no critic | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 11 | ▶ | use Mojo::Util qw/ decamelize /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 11 | ▶ | use Exporter::Easy ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 11 | OK => [ qw/ | |||||||||
| 41 | add_max_string_attribute | ||||||||||
| 42 | / ], | ||||||||||
| 43 | 11 | ); | |||||||||
| 44 | |||||||||||
| 45 | =head1 TYPES | ||||||||||
| 46 | |||||||||||
| 47 | =over | ||||||||||
| 48 | |||||||||||
| 49 | =item WestpacDate | ||||||||||
| 50 | |||||||||||
| 51 | A DateTime object, this will be coerced from the string DDMMYYYY | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | 11 | class_type 'DateTime'; | |||||||||
| 56 | |||||||||||
| 57 | 11 | subtype 'WestpacDate' | |||||||||
| 58 | => as 'DateTime'; | ||||||||||
| 59 | |||||||||||
| 60 | coerce 'WestpacDate' | ||||||||||
| 61 | => from 'Str' | ||||||||||
| 62 | 11 | ▶ | => via { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 63 | 11 | my $date_str = $_; | |||||||||
| 64 | |||||||||||
| 65 | 11 | ▶ | return $date_str if ref( $date_str ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | |||||||||||
| 67 | 11 | my ( $dd,$mm,$yyyy ) = ( $date_str =~ /^(\d{2})(\d{2})(\d{4})$/ ); | |||||||||
| 68 | 11 | return DateTime::Format::DateParse->parse_datetime( "$yyyy-$mm-$dd" ); | |||||||||
| 69 | 11 | }; | |||||||||
| 70 | |||||||||||
| 71 | =item PositiveInt | ||||||||||
| 72 | |||||||||||
| 73 | An Int greater than zero | ||||||||||
| 74 | |||||||||||
| 75 | =cut | ||||||||||
| 76 | |||||||||||
| 77 | subtype 'PositiveInt' | ||||||||||
| 78 | => as 'Int' | ||||||||||
| 79 | 8 | ▶ | => where { $_ > 0 } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 2 | ▶ | => message { "The number provided, $_, was not positive" } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 81 | 11 | ; | |||||||||
| 82 | |||||||||||
| 83 | =item PositiveNum | ||||||||||
| 84 | |||||||||||
| 85 | A Num greater than zero | ||||||||||
| 86 | |||||||||||
| 87 | =cut | ||||||||||
| 88 | |||||||||||
| 89 | subtype 'PositiveNum' | ||||||||||
| 90 | => as 'Num' | ||||||||||
| 91 | 20 | ▶ | => where { $_ > 0 } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 92 | 0 | ▶ | => message { "The number provided, $_, was not positive" } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 93 | 11 | ; | |||||||||
| 94 | |||||||||||
| 95 | =item BSBNumber | ||||||||||
| 96 | |||||||||||
| 97 | A Str of the form C</^\d{3}-\d{3}$/> | ||||||||||
| 98 | |||||||||||
| 99 | =cut | ||||||||||
| 100 | |||||||||||
| 101 | subtype 'BSBNumber' | ||||||||||
| 102 | => as 'Str', | ||||||||||
| 103 | 19 | ▶ | => where { $_ =~ /^\d{3}-\d{3}$/ } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 104 | 1 | ▶ | => message { "The BSB provided, $_, does not match \\d{3}-\\d{3}" } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 105 | 11 | ; | |||||||||
| 106 | |||||||||||
| 107 | =back | ||||||||||
| 108 | |||||||||||
| 109 | =head1 METHODS | ||||||||||
| 110 | |||||||||||
| 111 | =head4 add_max_string_attribute | ||||||||||
| 112 | |||||||||||
| 113 | Helper method to allow easier definition of Str types that are limited | ||||||||||
| 114 | to a particular lengths. For example: | ||||||||||
| 115 | |||||||||||
| 116 | |||||||||||
| 117 | __PACKAGE__->add_max_string_attribute( | ||||||||||
| 118 | 'RecipientNumber[20]' | ||||||||||
| 119 | is => 'ro', | ||||||||||
| 120 | required => 0, | ||||||||||
| 121 | ); | ||||||||||
| 122 | |||||||||||
| 123 | Is equivalent to: | ||||||||||
| 124 | |||||||||||
| 125 | subtype 'RecipientNumber' | ||||||||||
| 126 | => as 'Maybe[Str]' | ||||||||||
| 127 | => where { | ||||||||||
| 128 | ! defined( $_ ) | ||||||||||
| 129 | or length( $_ ) <= 20 | ||||||||||
| 130 | } | ||||||||||
| 131 | => message { | ||||||||||
| 132 | "The string provided for recipient_number" | ||||||||||
| 133 | . " was outside 1..20 chars" | ||||||||||
| 134 | } | ||||||||||
| 135 | ; | ||||||||||
| 136 | |||||||||||
| 137 | __PACKAGE__->meta->add_attribute( 'recipient_number', | ||||||||||
| 138 | isa => 'RecipientNumber', | ||||||||||
| 139 | predicate => "_has_recipient_number", | ||||||||||
| 140 | is => 'ro', | ||||||||||
| 141 | required => 0, | ||||||||||
| 142 | ); | ||||||||||
| 143 | |||||||||||
| 144 | =cut | ||||||||||
| 145 | |||||||||||
| 146 | 148 | ▶ | sub add_max_string_attribute ( | ||||||||
|
Subroutine
add_max_string_attribute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 147 | $package, | ||||||||||
| 148 | $name_spec, | ||||||||||
| 149 | %attr_spec, | ||||||||||
| 150 | ) { | ||||||||||
| 151 | 148 | my ( $subtype_name,$max_length ) = ( $name_spec =~ /^(\w+)\[(\d+)\]$/ ); | |||||||||
| 152 | 148 | my $attr_name = decamelize( $subtype_name ); | |||||||||
| 153 | |||||||||||
| 154 | subtype $subtype_name | ||||||||||
| 155 | => as 'Maybe[Str]' | ||||||||||
| 156 | 231 | ▶ | => where { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 157 | 231 | ▶ | ! defined( $_ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | or length( $_ ) <= $max_length | ||||||||||
| 159 | } | ||||||||||
| 160 | 2 | ▶ | => message { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 161 | 2 | "The string provided for $attr_name" | |||||||||
| 162 | . " was outside 1..$max_length chars" | ||||||||||
| 163 | } | ||||||||||
| 164 | 148 | ; | |||||||||
| 165 | |||||||||||
| 166 | 148 | $package->meta->add_attribute( $attr_name, | |||||||||
| 167 | isa => $subtype_name, | ||||||||||
| 168 | predicate => "_has_$attr_name", | ||||||||||
| 169 | %attr_spec, | ||||||||||
| 170 | ); | ||||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | 11 | 1; | |||||||||