| 1 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::Constraint::MinMaxFields; | ||||||||||||||||||||||||||||
| 4 | 8 | $HTML::FormFu::Constraint::MinMaxFields::VERSION = '2.09'; | |||||||||||||||||||||||||||
| 5 | # ABSTRACT: Min/Max Multi-field Constraint | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use MooseX::Attribute::Chained; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use MooseX::Aliases; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 8 | extends 'HTML::FormFu::Constraint'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 8 | with 'HTML::FormFu::Role::Constraint::Others'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use HTML::FormFu::Util qw( DEBUG_CONSTRAINTS debug ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 8 | has minimum => ( | |||||||||||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||||||||||||
| 18 | alias => 'min', | ||||||||||||||||||||||||||||
| 19 | traits => ['Chained'], | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 8 | has maximum => ( | |||||||||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||||||||||
| 24 | alias => 'max', | ||||||||||||||||||||||||||||
| 25 | traits => ['Chained'], | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 6 | ▶ | after BUILD => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 6 | $self->attach_errors_to_base(1); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 6 | return; | |||||||||||||||||||||||||||
| 34 | 8 | }; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 24 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=17 SCAR=28.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 24 | my ( $self, $params ) = @_; | |||||||||||||||||||||||||||
| 38 | 24 | my $count = 0; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # check when condition | ||||||||||||||||||||||||||||
| 41 | 24 | ▶ | return if !$self->_process_when($params); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # others are needed | ||||||||||||||||||||||||||||
| 44 | 23 | my $others = $self->others; | |||||||||||||||||||||||||||
| 45 | 23 | ▶ | return if !defined $others; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # get field names to check | ||||||||||||||||||||||||||||
| 48 | 23 | my @names = ( $self->nested_name ); | |||||||||||||||||||||||||||
| 49 | 23 | ▶ | push @names, ref $others ? @{$others} : $others; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # get min/max values | ||||||||||||||||||||||||||||
| 52 | 23 | ▶ | my $min | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | = defined $self->minimum | ||||||||||||||||||||||||||||
| 54 | ? $self->minimum | ||||||||||||||||||||||||||||
| 55 | : 1; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 23 | ▶ | my $max | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | = defined $self->maximum | ||||||||||||||||||||||||||||
| 59 | ? $self->maximum | ||||||||||||||||||||||||||||
| 60 | : scalar @names; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 23 | for my $name (@names) { | |||||||||||||||||||||||||||
| 63 | 86 | my $value = $self->get_nested_hash_value( $params, $name ); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 86 | ▶ | DEBUG_CONSTRAINTS && debug( OTHER_NAME => $name ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 86 | ▶ | DEBUG_CONSTRAINTS && debug( VALUE => $value ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 86 | ▶ | if ( ref $value eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 4 | my @errors = eval { $self->constrain_values( $value, $params ) }; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 4 | ▶ | if ( !@errors && !$@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@errors or $@
MC/DC0%0/2
@errors or $@
@errors
$@
Truth table33%1/3
A or B
A= @errorsB= $@
| |||||||||||||||||||||||||||||
| 72 | 4 | $count++; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||
| 76 | 82 | my $ok = eval { $self->constrain_value($value) }; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 82 | ▶ | if ( $ok && !$@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ok and not $@
MC/DC50%1/2
$ok and not $@
$ok
not $@
Truth table66%2/3
A and not(B)
A= $okB= not $@
| |||||||||||||||||||||||||||||
| 79 | 37 | $count++; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 23 | ▶ | my $pass = ( $count < $min || $count > $max ) ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$count < $min or $count > $max
MC/DC50%1/2
$count < $min or $count > $max
$count < $min
$count > $max
Truth table66%2/3
A or B
A= $count < $minB= $count > $max
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 23 | ▶ | return $self->mk_errors( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | { pass => $pass, | ||||||||||||||||||||||||||||
| 88 | failed => $pass ? [] : \@names, | ||||||||||||||||||||||||||||
| 89 | names => \@names, | ||||||||||||||||||||||||||||
| 90 | } ); | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # return true if value is defined | ||||||||||||||||||||||||||||
| 94 | 87 | ▶ | sub constrain_value { | ||||||||||||||||||||||||||
|
Subroutine
constrain_value: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 87 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 87 | ▶ | return 0 if !defined $value || $value eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $value or $value eq ""
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value
$value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 42 | return 1; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 8 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 8 | 1; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||