| 1 | 443 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::Constraint; | ||||||||||||||||||||||||||||
| 4 | 443 | $HTML::FormFu::Constraint::VERSION = '2.09'; | |||||||||||||||||||||||||||
| 5 | # ABSTRACT: Constrain User Input | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 443 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 443 | ▶ | use MooseX::Attribute::Chained; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 443 | extends 'HTML::FormFu::Processor'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 443 | ▶ | use HTML::FormFu::Exception::Constraint; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 443 | ▶ | use HTML::FormFu::Util qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | DEBUG_CONSTRAINTS | ||||||||||||||||||||||||||||
| 14 | DEBUG_CONSTRAINTS_WHEN | ||||||||||||||||||||||||||||
| 15 | debug | ||||||||||||||||||||||||||||
| 16 | 443 | ); | |||||||||||||||||||||||||||
| 17 | 443 | ▶ | use Clone (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 443 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 443 | ▶ | use List::Util 1.33 qw( any all first ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 443 | ▶ | use Scalar::Util qw( reftype blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 443 | has not => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||
| 23 | 443 | has force_errors => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||
| 24 | 443 | has when => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||
| 25 | 443 | has only_on_reps => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 49 | ▶ | sub repeatable_repeat { | ||||||||||||||||||||||||||
|
Subroutine
repeatable_repeat: called CC=7 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 49 | my ( $self, $repeatable, $new_block ) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 49 | my $block_fields = $new_block->get_fields; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # rename any 'when' fields | ||||||||||||||||||||||||||||
| 33 | { | ||||||||||||||||||||||||||||
| 34 | 49 | my $when = $self->when; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 49 | ▶ | if ( my $name = $when->{field} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 37 | 1 | my $field = $repeatable->get_field_with_original_name( $name, | |||||||||||||||||||||||||||
| 38 | $block_fields ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | if ( defined $field ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | DEBUG_CONSTRAINTS && debug( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | sprintf | ||||||||||||||||||||||||||||
| 43 | "Repeatable renaming constraint 'when{field}' '%s' to '%s'", | ||||||||||||||||||||||||||||
| 44 | $name, $field->nested_name, | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | $when->{field} = $field->nested_name; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | elsif ( my $names = $when->{fields} ) { | ||||||||||||||||||||||||||||
| 51 | 0 | for my $name (@$names) { | |||||||||||||||||||||||||||
| 52 | 0 | my $field = $repeatable->get_field_with_original_name( $name, | |||||||||||||||||||||||||||
| 53 | $block_fields ); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( defined $field ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | $when->{field} = $field->nested_name; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 507 | ▶ | sub pre_process { } | ||||||||||||||||||||||||||
|
Subroutine
pre_process: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 465 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=10 SCAR=23.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 465 | my ( $self, $params ) = @_; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 465 | ▶ | return unless $self->_run_this_rep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 450 | my $value = $self->_find_field_value($params); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 450 | my @errors; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # check when condition | ||||||||||||||||||||||||||||
| 75 | 450 | ▶ | if ( !$self->_process_when($params) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 45 | ▶ | DEBUG_CONSTRAINTS && debug('fail when() check - skipping constraint'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 45 | return; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 405 | ▶ | if ( ref $value eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 14 | push @errors, eval { $self->constrain_values( $value, $params ) }; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 14 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1 | push @errors, | |||||||||||||||||||||||||||
| 85 | $self->mk_errors( | ||||||||||||||||||||||||||||
| 86 | { pass => 0, | ||||||||||||||||||||||||||||
| 87 | message => $@, | ||||||||||||||||||||||||||||
| 88 | } ); | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||
| 92 | 391 | my $ok = eval { $self->constrain_value( $value, $params ) }; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 391 | ▶ | DEBUG_CONSTRAINTS && debug( 'CONSTRAINT RETURN VALUE' => $ok ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 391 | ▶ | DEBUG_CONSTRAINTS && debug( '$@' => $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 391 | ▶ | push @errors, | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$@ or CORE::not $ok
MC/DC100%2/2
$@ or CORE::not $ok
$@
CORE::not $ok
Truth table100%3/3
A or not(B)
A= $@B= CORE::not $ok
| |||||||||||||||||||||||||||||
| 98 | $self->mk_errors( | ||||||||||||||||||||||||||||
| 99 | { pass => ( $@ || !$ok ) ? 0 : 1, | ||||||||||||||||||||||||||||
| 100 | message => $@, | ||||||||||||||||||||||||||||
| 101 | } ); | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 405 | return @errors; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 499 | ▶ | sub _run_this_rep { | ||||||||||||||||||||||||||
|
Subroutine
_run_this_rep: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 108 | 499 | my ($self) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 499 | ▶ | my $only_on_reps = $self->only_on_reps | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | or return 1; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 61 | ▶ | my $current_rep = $self->field->repeatable_count | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | or return 1; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 61 | ▶ | $only_on_reps = [$only_on_reps] | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
builtin::reftype($only_on_reps) || ''
MC/DC100%1/1
builtin::reftype($only_on_reps) || ''
builtin::reftype($only_on_reps)
Truth table100%2/2
A
A= builtin::reftype($only_on_reps)
| |||||||||||||||||||||||||||||
| 117 | if ( reftype($only_on_reps) || '' ) ne 'ARRAY'; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 61 | ▶ | return first { $current_rep == $_ } @$only_on_reps; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 450 | ▶ | sub _find_field_value { | ||||||||||||||||||||||||||
|
Subroutine
_find_field_value: called CC=8 SCAR=21.8
| |||||||||||||||||||||||||||||
| 123 | 450 | my ( $self, $params ) = @_; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 450 | my $value = $self->get_nested_hash_value( $params, $self->nested_name ); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | my @fields_with_this_name | ||||||||||||||||||||||||||||
| 128 | 450 | = @{ $self->form->get_fields( { nested_name => $self->nested_name } ) }; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 450 | ▶ | if ( @fields_with_this_name > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 10 | my $field = $self->parent; | |||||||||||||||||||||||||||
| 132 | 10 | my $index; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 10 | ▶ | for ( my $i = 0; $i <= $#fields_with_this_name; ++$i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 15 | ▶ | if ( $fields_with_this_name[$i] eq $field ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 10 | $index = $i; | |||||||||||||||||||||||||||
| 137 | 10 | last; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 10 | ▶ | croak 'did not find ourself - how can this happen?' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | if !defined $index; | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 10 | ▶ | if ( ( reftype($value) || '' ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
builtin::reftype($value) || ''
MC/DC0%0/1
builtin::reftype($value) || ''
builtin::reftype($value)
Truth table50%1/2
A
A= builtin::reftype($value)
| |||||||||||||||||||||||||||||
| 145 | 10 | $value = $value->[$index]; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | elsif ( $index == 0 ) { | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # keep $value | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | else { | ||||||||||||||||||||||||||||
| 152 | 0 | undef $value; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 450 | return $value; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 23 | ▶ | sub constrain_values { | ||||||||||||||||||||||||||
|
Subroutine
constrain_values: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 160 | 23 | my ( $self, $values, $params ) = @_; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 23 | my @errors; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 23 | for my $value (@$values) { | |||||||||||||||||||||||||||
| 165 | 45 | my $ok = eval { $self->constrain_value( $value, $params ) }; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 45 | ▶ | DEBUG_CONSTRAINTS && debug( 'CONSTRAINT RETURN VALUE' => $ok ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 45 | ▶ | DEBUG_CONSTRAINTS && debug( '$@' => $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 45 | ▶ | push @errors, | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ or CORE::not $ok
MC/DC50%1/2
$@ or CORE::not $ok
$@
CORE::not $ok
Truth table66%2/3
A or not(B)
A= $@B= CORE::not $ok
| |||||||||||||||||||||||||||||
| 171 | $self->mk_errors( | ||||||||||||||||||||||||||||
| 172 | { pass => ( $@ || !$ok ) ? 0 : 1, | ||||||||||||||||||||||||||||
| 173 | message => $@, | ||||||||||||||||||||||||||||
| 174 | } ); | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 23 | return @errors; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | sub constrain_value { | ||||||||||||||||||||||||||
|
Subroutine
constrain_value: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 181 | 0 | croak "constrain_value() should be overridden"; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 444 | ▶ | sub mk_errors { | ||||||||||||||||||||||||||
|
Subroutine
mk_errors: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 185 | 444 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 444 | my $pass = $args->{pass}; | |||||||||||||||||||||||||||
| 188 | 444 | my $message = $args->{message}; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 444 | my @errors; | |||||||||||||||||||||||||||
| 191 | 444 | ▶ | my $force = $self->force_errors || $self->parent->force_errors; | ||||||||||||||||||||||||||
|
Condition100%3/3
$self->force_errors || $self->parent->force_errors
MC/DC100%2/2
$self->force_errors || $self->parent->force_errors
$self->force_errors
$self->parent->force_errors
Truth table100%3/3
A || B
A= $self->force_errorsB= $self->parent->force_errors
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 444 | ▶ | if ( !$pass || $force ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
CORE::not $pass or $force
MC/DC100%2/2
CORE::not $pass or $force
CORE::not $pass
$force
Truth table100%3/3
not(A) or B
A= CORE::not $passB= $force
| |||||||||||||||||||||||||||||
| 194 | 165 | my $error = $self->mk_error($message); | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 165 | ▶ | $error->forced(1) if $pass; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 165 | push @errors, $error; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 444 | return @errors; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 233 | ▶ | sub mk_error { | ||||||||||||||||||||||||||
|
Subroutine
mk_error: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 205 | 233 | my ( $self, $err ) = @_; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 233 | ▶ | if ( !blessed $err || !$err->isa('HTML::FormFu::Exception::Constraint') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($err) and $err->isa("HTML::FormFu::Exception::Constraint")
MC/DC0%0/2
builtin::blessed($err) and $err->isa("HTML::FormFu::Exception::Constraint")
builtin::blessed($err)
$err->isa("HTML::FormFu::Exception::Constraint")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa("HTML::FormFu::Exception::Constraint")
| |||||||||||||||||||||||||||||
| 208 | 233 | $err = HTML::FormFu::Exception::Constraint->new; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 233 | return $err; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 561 | ▶ | sub _process_when { | ||||||||||||||||||||||||||
|
Subroutine
_process_when: called CC=23 SCAR=31.5
| |||||||||||||||||||||||||||||
| 215 | 561 | my ( $self, $params ) = @_; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # returns 1 if when condition is fulfilled or not defined | ||||||||||||||||||||||||||||
| 218 | # returns 0 if when condition is defined and not fulfilled | ||||||||||||||||||||||||||||
| 219 | # If it's a callback, return callback's return value (so 'when' | ||||||||||||||||||||||||||||
| 220 | # condition is met if callback returns a true value) | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | # get when condition | ||||||||||||||||||||||||||||
| 223 | 561 | my $when = $self->when; | |||||||||||||||||||||||||||
| 224 | 561 | ▶ | return 1 if !defined $when; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # check type of 'when' | ||||||||||||||||||||||||||||
| 227 | 132 | ▶ | croak "Parameter 'when' is not a hash ref" if ref $when ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # field or callback must be defined | ||||||||||||||||||||||||||||
| 230 | 132 | my $when_field = $when->{field}; | |||||||||||||||||||||||||||
| 231 | 132 | my $when_fields = $when->{fields}; | |||||||||||||||||||||||||||
| 232 | 132 | my $when_any_field = $when->{any_field}; | |||||||||||||||||||||||||||
| 233 | 132 | my $when_callback = $when->{callback}; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | croak | ||||||||||||||||||||||||||||
| 236 | "'field', 'fields', 'any_field' or 'callback' key must be defined in 'when'" | ||||||||||||||||||||||||||||
| 237 | 132 | ▶ | if all { !defined } $when_field, $when_fields, $when_any_field, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | $when_callback; | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # Callback will be the preferred thing | ||||||||||||||||||||||||||||
| 241 | 132 | ▶ | if ($when_callback) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | ## no critic (ProhibitNoStrict); | ||||||||||||||||||||||||||||
| 243 | 443 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 244 | 20 | return $when_callback->( $params, $self ); | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 112 | my $any; | |||||||||||||||||||||||||||
| 248 | my @when_fields_value; | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 112 | ▶ | if ($when_any_field) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 24 | ▶ | croak "'any_field' is set to an empty list" if !@$when_any_field; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 24 | $any = 1; | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 24 | @$when_fields = @$when_any_field; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 112 | ▶ | if ($when_fields) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 59 | ▶ | croak "'fields' is set to an empty list" if !@$when_fields; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 59 | for my $name (@$when_fields) { | |||||||||||||||||||||||||||
| 262 | 138 | push @when_fields_value, | |||||||||||||||||||||||||||
| 263 | $self->get_nested_hash_value( $params, $name ); | ||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | else { | ||||||||||||||||||||||||||||
| 267 | 53 | push @when_fields_value, | |||||||||||||||||||||||||||
| 268 | $self->get_nested_hash_value( $params, $when_field ); | ||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | # a field missing from the submission (e.g. an unchecked Checkbox) has | ||||||||||||||||||||||||||||
| 272 | # an undefined value, which is treated as not matching - so with 'not' | ||||||||||||||||||||||||||||
| 273 | # set, the condition is fulfilled and the constraint is applied. RT#45409 | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 112 | ▶ | DEBUG_CONSTRAINTS_WHEN | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | && debug( 'WHEN_FIELDS_VALUES' => \@when_fields_value ); | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 112 | my @values; | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 112 | ▶ | if ( defined( my $value = $when->{value} ) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 281 | 52 | push @values, $value; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | elsif ( defined( my $values = $when->{values} ) ) { | ||||||||||||||||||||||||||||
| 284 | 24 | push @values, @$values; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | # determine if condition is fulfilled | ||||||||||||||||||||||||||||
| 288 | 112 | my @ok; | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 112 | ▶ | if (@values) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | 76 | for my $value (@when_fields_value) { | |||||||||||||||||||||||||||
| 292 | push @ok, | ||||||||||||||||||||||||||||
| 293 | 104 | ▶ | ( defined $value && any { $value eq $_ } @values ) ? 1 : 0; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition100%3/3
defined $value and &any(sub {
$value eq $_;
}
, @values)
MC/DC100%2/2
defined $value and &any(sub {
$value eq $_;
}
, @values)
defined $value
&any(sub {
$value eq $_;
}
, @values)
Truth table100%3/3
A and B
A= defined $valueB= &any(sub {
$value eq $_;
}
, @values)
| |||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | else { | ||||||||||||||||||||||||||||
| 297 | 36 | for my $value (@when_fields_value) { | |||||||||||||||||||||||||||
| 298 | 87 | ▶ | push @ok, ( defined $value && $value ) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value
MC/DC100%2/2
defined $value and $value
defined $value
$value
Truth table100%3/3
A and B
A= defined $valueB= $value
| |||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 112 | ▶ | DEBUG_CONSTRAINTS_WHEN && debug( "'when' value matches" => \@ok ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | my $return | ||||||||||||||||||||||||||||
| 305 | = $any | ||||||||||||||||||||||||||||
| 306 | 37 | ▶ | ? any { $when->{not} ? !$_ : $_ } @ok | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 112 | ▶ | : all { $when->{not} ? !$_ : $_ } @ok; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 112 | ▶ | DEBUG_CONSTRAINTS_WHEN && debug( "'when' return value" => $return ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 112 | return $return; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 0 | ▶ | sub fetch_error_message { | ||||||||||||||||||||||||||
|
Subroutine
fetch_error_message: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 315 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 0 | my $error = HTML::FormFu::Exception::Constraint->new( | |||||||||||||||||||||||||||
| 318 | { form => $self->form, | ||||||||||||||||||||||||||||
| 319 | parent => $self->parent, | ||||||||||||||||||||||||||||
| 320 | processor => $self, | ||||||||||||||||||||||||||||
| 321 | } ); | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 0 | return $error->message; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 51 | ▶ | sub clone { | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 327 | 51 | my $self = shift; | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 51 | my $clone = $self->SUPER::clone(@_); | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 51 | ▶ | if ( defined( my $when = $self->when ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | 1 | $clone->when( Clone::clone $when ); | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 51 | return $clone; | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 443 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 443 | 1; | |||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | __END__ | ||||||||||||||||||||||||||||