NAB/Types.pm

filter: Statementstmt 95.6% 88 / 92 Branchbran 84.6% 22 / 26 Conditioncond 91.6% 11 / 12 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 87.1% 27 / 31 Podpod 50.0% 1 / 2 totaltotal 90.6% 155 / 171 CC 18 SCAR 7.6
CC
18
Cov
93%
__ANON__
4.0
__ANON__
3.0
__ANON__
3.0
CRAP 2.1
« NAB/BPAY/Payments/Results.pm ↑ Summary NAB/Acknowledgement.pm »
1 package Business::NAB::Types;
2 29 $Business::NAB::Types::VERSION = '0.11';
3 =head1 NAME
4  
5 Business::NAB::Types
6  
7 =head1 SYNOPSIS
8  
9 use Business::NAB::Types qw/
10 add_max_string_attribute
11 /;
12  
13 has [ qw/
14 process_date
15 / ] => (
16 is => 'ro',
17 isa => 'NAB::Type::Date',
18 required => 1,
19 coerce => 1,
20 );
21  
22 ...
23  
24 =head1 DESCRIPTION
25  
26 Package for defining type constraints for use in the Business::NAB
27 namespace. All types are namespaced to C<NAB::Type::*>.
28  
29 =cut
30  
31 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 29 ▶use feature qw/ signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 29 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 29 ▶no warnings qw/ experimental::signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 29 ▶use DateTime::Format::DateParse; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
39 29 ▶use Exporter::Easy (
Subroutine BEGIN: called CC=1 SCAR=0.0
40 29 OK => [
41 qw/
42 add_max_string_attribute
43 decamelize
44 /
45 ]
46 29 );
47  
48 =head1 TYPES
49  
50 =over
51  
52 =item NAB::Type::Date
53  
54 A DateTime object, this will be coerced from the string DDMMYY or DDMMYYYY
55  
56 =cut
57  
58 29 class_type 'DateTime';
59  
60 29 subtype 'NAB::Type::Date'
61 => as 'DateTime';
62  
63 coerce 'NAB::Type::Date'
64 => from 'Str'
65 22 ▶ => via {
Subroutine __ANON__: called CC=4 SCAR=13.9
66 22 my $date_str = $_;
67  
68 22 ▶ return $date_str if ref( $date_str );
Branch: 1 branch
BranchTrueFalseTotal
if ref $date_str 0 22 22
69  
70 22 ▶ if ( $date_str =~ /^(\d{2})(\d{2})(\d{2,4})$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_str =~ /^(\d{2})(\d{2})(\d{2,4})$/) 19 3 22
71 19 my ( $dd, $mm, $yy ) = ( $1, $2, $3 );
72 19 ▶ my $yyyy = length( $yy ) == 4 ? $yy : "20$yy"; # Y2K never happened?
Branch: 1 branch
BranchTrueFalseTotal
length $yy == 4 ? : 10 9 19
73 19 $date_str = "$yyyy-$mm-$dd";
74 }
75  
76 22 return DateTime::Format::DateParse->parse_datetime( $date_str );
77 29 };
78  
79 =item NAB::Type::StatementDate
80  
81 A DateTime object, this will be coerced from the string YYMMDD
82  
83 =cut
84  
85 29 subtype 'NAB::Type::StatementDate'
86 => as 'DateTime';
87  
88 coerce 'NAB::Type::StatementDate'
89 => from 'Str'
90 61 ▶ => via {
Subroutine __ANON__: called CC=3 SCAR=11.0
91 61 my $date_str = $_;
92  
93 61 ▶ return $date_str if ref( $date_str );
Branch: 1 branch
BranchTrueFalseTotal
if ref $date_str 0 61 61
94  
95 61 my ( $yy, $mm, $dd ) = ( $date_str =~ /^(\d{2,4})(\d{2})(\d{2})$/ );
96 61 ▶ my $yyyy = length( $yy ) == 4 ? $yy : "20$yy"; # Y2K never happened?
Branch: 1 branch
BranchTrueFalseTotal
length $yy == 4 ? : 54 7 61
97 61 return DateTime::Format::DateParse->parse_datetime( "$yyyy-$mm-$dd" );
98 29 };
99  
100 =item NAB::Type::BRFInt
101  
102 =cut
103  
104 29 subtype 'NAB::Type::BRFInt'
105 => as 'Int'
106 ;
107  
108 coerce 'NAB::Type::BRFInt'
109 => from 'Str'
110 14 ▶ => via {
Subroutine __ANON__: called CC=3 SCAR=11.0
111 14 my $str = $_;
112  
113 # trailer record amounts in BPAY Remittance Files use the last
114 # character to represent:
115 # - the last digit
116 # - the sign
117 #
118 # so we convert that to an actual signed integer here
119 # see also: Business::NAB::BPAY::Remittance::File::TrailerRecord
120 # sub _brf_int
121 14 ▶ if ( $str =~ /[{A-I]$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /[{A-I]$/) { } 13 1 14
elsif ($str =~ /[}J-R]$/) { } 1 0 1
122 13 $str =~ tr/{A-I/0-9/;
123 } elsif ( $str =~ /[}J-R]$/ ) {
124 1 $str =~ tr/}J-R/0-9/;
125 1 $str *= -1;
126 }
127  
128 14 return $str;
129 29 };
130  
131 =item NAB::Type::PositiveInt
132  
133 An Int greater than zero
134  
135 =cut
136  
137 subtype 'NAB::Type::PositiveInt'
138 => as 'Int'
139 142 ▶ => where { $_ > 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
140 0 ▶=> message { "The number provided, $_, was not positive" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 29 ;
142  
143 =item NAB::Type::PositiveIntOrZero
144  
145 An Int greater than or equal to zero
146  
147 =cut
148  
149 subtype 'NAB::Type::PositiveIntOrZero'
150 => as 'Int'
151 183 ▶ => where { $_ >= 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
152 0 ▶=> message { "The number provided, $_, was not positive or zero" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 29 ;
154  
155 =item NAB::Type::BSBNumber
156  
157 =item NAB::Type::BSBNumberNoDash
158  
159 A Str of the form C</^\d{3}-\d{3}$/>
160  
161 A Str of the form C</^\d{6}$/>
162  
163 Some file formats for NAB require a BSB with the dash, while other require
164 the BSB without the dash. This is a hard requirement and files will be
165 rejected if you fail to handle it.
166  
167 The types here are defined so that you can pass either format in and they
168 will be coerced to the correct format for the file type in question.
169  
170 =cut
171  
172 subtype 'NAB::Type::BSBNumber'
173 => as 'Str',
174 292 ▶ => where { $_ =~ /^\d{3}-\d{3}$/ }
Subroutine __ANON__: called CC=1 SCAR=0.0
175 1 ▶=> message { "The BSB provided, $_, does not match \\d{3}-\\d{3}" }
Subroutine __ANON__: called CC=1 SCAR=0.0
176 29 ;
177  
178 subtype 'NAB::Type::BSBNumberNoDash'
179 => as 'Str',
180 60 ▶ => where { $_ =~ /^\d{6}$/ }
Subroutine __ANON__: called CC=1 SCAR=0.0
181 0 ▶=> message { "The BSB provided, $_, does not match \\d{6}" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 29 ;
183  
184 coerce 'NAB::Type::BSBNumber'
185 => from 'Str'
186 10 ▶ => via {
Subroutine __ANON__: called CC=1 SCAR=0.0
187  
188 # NAB require the - char, ensure it's there
189 10 $_ =~ s/^(\d{3})(\d{3})/$1-$2/;
190 10 return $_;
191 29 };
192  
193 coerce 'NAB::Type::BSBNumberNoDash'
194 => from 'Str'
195 7 ▶ => via {
Subroutine __ANON__: called CC=1 SCAR=0.0
196  
197 # NAB don't require the - char, ensure it's not there
198 7 $_ =~ s/^(\d{3})-(\d{3})/$1$2/;
199 7 return $_;
200 29 };
201  
202  
203 =item NAB::Type::AccountNumber
204  
205 A Str of the form:
206  
207 * Alpha-Numeric (A-z0-9)
208 * Hyphens & blanks only are valid
209 * Must not contain all blanks or all zeros
210  
211 And:
212  
213 * Leading zeros, which are part of an account number, must be shown
214 * Edit out hyphens where account number exceeds nine characters
215 * Right justified
216 * Leave blank
217  
218 =cut
219  
220 subtype 'NAB::Type::AccountNumber'
221 => as 'Str',
222 176 ▶ => where {
Subroutine __ANON__: called CC=3 SCAR=11.0
223 176 ▶ length( $_ ) < 10
Branch: 1 branch
BranchTrueFalseTotal
length $_ < 10 and $_ =~ /^[A-z0-9\ \-]+$/ and not $_ =~ /^(\s|0)+$/ 172 4 176
Condition100%3/3
length $_ < 10 and $_ =~ /^[A-z0-9\ \-]+$/
!ll&&!rl&&r
31172
MC/DC100%2/2
length $_ < 10 and $_ =~ /^[A-z0-9\ \-]+$/
length $_ < 10 $_ =~ /^[A-z0-9\ \-]+$/
Truth table100%3/3
A and B
A= length $_ < 10B= $_ =~ /^[A-z0-9\ \-]+$/
ABresult
0X0
100
111
224 && $_ =~ /^[A-z0-9\ \-]+$/
225 && $_ !~ /^(\s|0)+$/
226 }
227 6 ▶=> message { "The account number provided, $_, is not valid" }
Subroutine __ANON__: called CC=1 SCAR=0.0
228 29 ;
229  
230 =item NAB::Type::Indicator
231  
232 A Str of the form C</^[\ NTWXY]$/>
233  
234 =cut
235  
236 subtype 'NAB::Type::Indicator'
237 => as 'Maybe[Str]',
238 61 ▶ => where { $_ =~ /^[ NTWXY]$/ }
Subroutine __ANON__: called CC=1 SCAR=0.0
239 21 ▶=> message { "The indicator provided, $_, does not match [ NTWXY]" }
Subroutine __ANON__: called CC=1 SCAR=0.0
240 29 ;
241  
242 =item NAB::Type::Str
243  
244 A Str that is restricted to the BECS EBCDIC character set
245  
246 =cut
247  
248 29 my $EBCDIC_re = qr/[^A-Za-z0-9^_[\]'\'',?;:=#\/.*()&%!$ \@+-]/a;
249  
250 subtype 'NAB::Type::Str'
251 => as 'Maybe[Str]',
252 913 ▶ => where {
Subroutine __ANON__: called CC=2 SCAR=6.9
253 913 ▶ !defined( $_ )
Condition100%3/3
defined $_ && $_ =~ /$EBCDIC_re/a
!ll&&!rl&&r
49063
MC/DC100%2/2
defined $_ && $_ =~ /$EBCDIC_re/a
defined $_ $_ =~ /$EBCDIC_re/a
Truth table100%3/3
A && B
A= defined $_B= $_ =~ /$EBCDIC_re/a
ABresult
0X0
100
111
254  
255 # check for anything outside the BECS EBCDIC char set
256 or $_ !~ $EBCDIC_re;
257 }
258 0 ▶=> message { "Str provided $_ contains non BECS EBCDIC chars" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
259 29 ;
260  
261 =back
262  
263 =head1 METHODS
264  
265 =head4 add_max_string_attribute
266  
267 Helper method to allow easier definition of NAB::Type::Str types that
268 are limited to a particular lengths. For example:
269  
270 __PACKAGE__->add_max_string_attribute(
271 'RecipientNumber[20]'
272 is => 'ro',
273 required => 0,
274 );
275  
276 Is equivalent to:
277  
278 subtype 'NAB::Type::RecipientNumber'
279 => as 'Maybe[NAB::Type::Str]'
280 => where {
281 ! defined( $_ )
282 or length( $_ ) <= 20
283 }
284 => message {
285 "The string provided for recipient_number"
286 . " was outside 1..20 chars"
287 }
288 ;
289  
290 __PACKAGE__->meta->add_attribute( 'recipient_number',
291 isa => 'NAB::Type::RecipientNumber',
292 predicate => "_has_recipient_number",
293 is => 'ro',
294 required => 0,
295 );
296  
297 If you provide a suffix a trigger will be created to honour the requirement.
298 For example:
299  
300 __PACKAGE__->add_max_string_attribute(
301 'reel_sequence_number[2:trim_leading_zeros]',
302 ...
303 );
304  
305 Will make the trigger remove leading zeros whenever the attribute is set or
306 updated.
307  
308 Current supported suffixes are:
309  
310 * trim_leading_zeros
311  
312 =cut
313  
314 177 ▶sub add_max_string_attribute (
Subroutine add_max_string_attribute: called CC=3 SCAR=11.0
Pod documented
315 $package,
316 $name_spec,
317 %attr_spec,
318 ) {
319 177 my ( $subtype_name, $max_length, $trim )
320 = ( $name_spec =~ /^(\w+)\[(\d+)(:[A-z-]+)?\]$/ );
321  
322 177 ▶ $subtype_name //= $name_spec;
Condition66%2/3
$subtype_name //= $name_spec
l!l&&r!l&&!r
121560
MC/DC0%0/2
$subtype_name //= $name_spec
$subtype_name $name_spec
Truth table66%2/3
A //= B
A= $subtype_nameB= $name_spec
ABresult
1X1
011
000
323 177 my $attr_name = decamelize( $subtype_name );
324  
325 subtype "NAB::Type::$subtype_name"
326 => as 'NAB::Type::Str'
327 910 ▶ => where {
Subroutine __ANON__: called CC=3 SCAR=11.0
328 910 ▶ $max_length
Branch: 1 branch
BranchTrueFalseTotal
$max_length ? : 727 183 910
Condition100%3/3
not defined $_ or length $_ <= $max_length
l!l&&r!l&&!r
269926
MC/DC100%2/2
not defined $_ or length $_ <= $max_length
not defined $_ length $_ <= $max_length
Truth table100%3/3
not(A) or B
A= not defined $_B= length $_ <= $max_length
ABresult
1X1
011
000
329 ? ( !defined( $_ ) or length( $_ ) <= $max_length )
330 : 1;
331 }
332 29 ▶ => message {
Subroutine __ANON__: called CC=2 SCAR=6.9
333 29 ▶ $_ =~ $EBCDIC_re
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /$EBCDIC_re/ ? : 3 26 29
334 ? "Str provided $_ contains non BECS EBCDIC chars"
335 : "The string provided for $attr_name was outside 1..$max_length chars"
336 }
337 177 ;
338  
339 $package->meta->add_attribute(
340 $attr_name,
341 isa => "NAB::Type::$subtype_name",
342 predicate => "_has_$attr_name",
343  
344 # trim via trigger if required
345 (
346 $trim
347 ? (
348 6 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
349 6 my ( $self, $value, $old_value ) = @_;
350  
351 6 ▶ $value =~ s/^0+// if $trim eq ':trim_leading_zeros';
Branch: 1 branch
BranchTrueFalseTotal
if $trim eq ":trim_leading_zeros" 6 0 6
352  
353 6 $self->{ $attr_name } = $value;
354  
355 } )
356 177 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
$trim ? : 4 173 177
357 ),
358  
359 %attr_spec,
360 );
361 }
362  
363 # taken from Mojo::Util - rather than pulling in the
364 # entire Mojolicious dist, we'll just "inline" it
365 282 ▶sub decamelize {
Subroutine decamelize: called CC=2 SCAR=6.9
Pod undocumented
366 282 my $str = shift;
367 282 ▶ return $str if $str !~ /^[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /^[A-Z]/ 177 105 282
368  
369 # snake_case words
370 return join '-', map {
371 105 join( '_', map { lc } grep { length } split /([A-Z]{1}[^A-Z]*)/ )
372 } split /::/, $str;
373 }
374  
375 29 1;
« NAB/BPAY/Payments/Results.pm ↑ Summary NAB/Acknowledgement.pm »