| 1 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::Constraint::Repeatable::Any; | ||||||||||||||||||||||||||||||||||
| 4 | 4 | $HTML::FormFu::Constraint::Repeatable::Any::VERSION = '2.06'; | |||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Ensure at least 1 of a repeated field is filled-in | ||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 4 | extends 'HTML::FormFu::Constraint'; | |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Scalar::Util qw( reftype ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | after BUILD => sub { | ||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 4 | $self->only_on_reps( [1] ); | |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 4 | return; | |||||||||||||||||||||||||||||||||
| 20 | 4 | }; | |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | sub process { | ||||||||||||||||||||||||||||||||||
| 23 | 34 | ▶ | my ( $self, $params ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
process: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | 34 | ▶ | return unless $self->_run_this_rep; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | # check when condition | ||||||||||||||||||||||||||||||||||
| 28 | 16 | ▶ | return if !$self->_process_when($params); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | 16 | my $field = $self->field; | |||||||||||||||||||||||||||||||||
| 31 | 16 | my $repeatable = $field->get_parent( { type => 'Repeatable' } ); | |||||||||||||||||||||||||||||||||
| 32 | 16 | my $pass; | |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | 16 | ▶ | my $original_name = $field->original_name || ''; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$field->original_name || ''
MC/DC100%1/1
$field->original_name || ''
$field->original_name
Truth table100%2/2
A
A= $field->original_name
| |||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | my @fields | ||||||||||||||||||||||||||||||||||
| 37 | 46 | = grep { $_->get_parent( { type => 'Repeatable' } ) == $repeatable } | |||||||||||||||||||||||||||||||||
| 38 | 52 | ▶ | grep { ( $_->original_name || '' ) eq $original_name } | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_->original_name || ''
MC/DC100%1/1
$_->original_name || ''
$_->original_name
Truth table100%2/2
A
A= $_->original_name
| |||||||||||||||||||||||||||||||||||
| 39 | 16 | @{ $repeatable->get_fields }; | |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | 16 | my $increment_field_names = $repeatable->increment_field_names; | |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | 16 | for my $f (@fields) { | |||||||||||||||||||||||||||||||||
| 44 | 28 | my $value; | |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 28 | ▶ | if ($increment_field_names) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 22 | $value = $self->get_nested_hash_value( $params, $f->nested_name ); | |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||||||||
| 50 | 6 | $value = _find_this_field_value( $self, $f, $repeatable, $params ); | |||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | 28 | my $ok = eval { $self->constrain_value($value) }; | |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | 28 | ▶ | 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 $@
| |||||||||||||||||||||||||||||||||||
| 56 | 12 | $pass = 1; | |||||||||||||||||||||||||||||||||
| 57 | 12 | last; | |||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | 16 | return $self->mk_errors( { pass => $pass, } ); | |||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | sub _find_this_field_value { | ||||||||||||||||||||||||||||||||||
| 65 | 6 | ▶ | my ( $self, $field, $repeatable, $params ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_find_this_field_value: called CC=7 SCAR=20.5
| |||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | 6 | my $nested_name = $field->nested_name; | |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | 6 | my $value = $self->get_nested_hash_value( $params, $nested_name ); | |||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | my @fields_with_this_name | ||||||||||||||||||||||||||||||||||
| 72 | 6 | = @{ $repeatable->get_fields( { nested_name => $nested_name } ) }; | |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | 6 | ▶ | if ( @fields_with_this_name > 1 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 75 | 6 | my $index; | |||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | 6 | ▶ | for ( my $i = 0; $i <= $#fields_with_this_name; ++$i ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 78 | 8 | ▶ | if ( $fields_with_this_name[$i] eq $field ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | 6 | $index = $i; | |||||||||||||||||||||||||||||||||
| 80 | 6 | last; | |||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | 6 | ▶ | croak 'did not find ourself - how can this happen?' | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 85 | if !defined $index; | ||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | 6 | ▶ | if ( reftype($value) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 88 | 6 | $value = $value->[$index]; | |||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||
| 90 | elsif ( $index == 0 ) { | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | # keep $value | ||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||||||||
| 95 | 0 | undef $value; | |||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | 6 | return $value; | |||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | sub constrain_value { | ||||||||||||||||||||||||||||||||||
| 103 | 28 | ▶ | my ( $self, $value ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
constrain_value: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | 28 | ▶ | return defined $value && length $value; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $value && length $value
MC/DC50%1/2
defined $value && length $value
defined $value
length $value
Truth table66%2/3
A && B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | sub _localize_args { | ||||||||||||||||||||||||||||||||||
| 109 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_localize_args: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | return | ||||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | $self->parent->label | ||||||||||||||||||||||||||||||||
|
Condition33%2/6
$self->parent->label || $self->parent->original_name || $self->parent->name
$self->parent->label or $self->parent->original_name
MC/DC0%0/3
$self->parent->label || $self->parent->original_name || $self->parent->name
$self->parent->label
$self->parent->original_name
$self->parent->name
Truth table25%1/4
A or B || C
A= $self->parent->labelB= $self->parent->original_nameC= $self->parent->name
| |||||||||||||||||||||||||||||||||||
| 113 | || $self->parent->original_name | ||||||||||||||||||||||||||||||||||
| 114 | || $self->parent->name; | ||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | 4 | 1; | |||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | =pod | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | HTML::FormFu::Constraint::Repeatable::Any - Ensure at least 1 of a repeated field is filled-in | ||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||
| 131 | =head1 VERSION | ||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||
| 133 | version 2.06 | ||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||
| 135 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | elements: | ||||||||||||||||||||||||||||||||||
| 138 | - type: Repeatable | ||||||||||||||||||||||||||||||||||
| 139 | elements: | ||||||||||||||||||||||||||||||||||
| 140 | - name: foo | ||||||||||||||||||||||||||||||||||
| 141 | constraints: | ||||||||||||||||||||||||||||||||||
| 142 | - type: Repeatable::Any | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | Ensure at least 1 of a repeated field is filled-in. | ||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | Any error will be attached to the first repetition of the field. | ||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||
| 150 | This constraint doesn't honour the C<not()> value. | ||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | Is a sub-class of, and inherits methods from L<HTML::FormFu::Constraint> | ||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||
| 156 | L<HTML::FormFu> | ||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | Carl Franks C<cfranks@cpan.org> | ||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | This library is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||
| 165 | the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | Carl Franks <cpan@fireartist.com> | ||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||
| 173 | This software is copyright (c) 2018 by Carl Franks. | ||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||
| 175 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||
| 176 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||||||||||||||||