lib/HTML/FormFu/Constraint/MinMaxFields.pm

filter: Statementstmt 100% 54 / 54 Branchbran 84.6% 22 / 26 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 87.2% 96 / 110 SCAR 29.6
CC
19
Cov
91%
process
17.8
constrain_value
3.0
BEGIN
1.0
CRAP 19.2
« lib/HTML/FormFu/Localize.pm ↑ Summary lib/HTML/FormFu/Element/Select.pm »
1 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Constraint::MinMaxFields;
4 6 $HTML::FormFu::Constraint::MinMaxFields::VERSION = '2.06';
5 # ABSTRACT: Min/Max Multi-field Constraint
6  
7 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use MooseX::Aliases;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 extends 'HTML::FormFu::Constraint';
11  
12 6 with 'HTML::FormFu::Role::Constraint::Others';
13  
14 6 ▶use HTML::FormFu::Util qw( DEBUG_CONSTRAINTS debug );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 has minimum => (
17 is => 'rw',
18 alias => 'min',
19 traits => ['Chained'],
20 );
21  
22 6 has maximum => (
23 is => 'rw',
24 alias => 'max',
25 traits => ['Chained'],
26 );
27  
28 after BUILD => sub {
29 6 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 6 $self->attach_errors_to_base(1);
32  
33 6 return;
34 6 };
35  
36 sub process {
37 24 ▶ my ( $self, $params ) = @_;
Subroutine process: called CC=17 SCAR=28.8
Pod undocumented
38 24 my $count = 0;
39  
40 # check when condition
41 24 ▶ return if !$self->_process_when($params);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_process_when($params) 1 23 24
42  
43 # others are needed
44 23 my $others = $self->others;
45 23 ▶ return if !defined $others;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $others 0 23 23
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
BranchTrueFalseTotal
ref $others ? : 22 1 23
50  
51 # get min/max values
52 23 ▶ my $min
Branch: 1 branch
BranchTrueFalseTotal
defined $self->minimum ? : 22 1 23
53 = defined $self->minimum
54 ? $self->minimum
55 : 1;
56  
57 23 ▶ my $max
Branch: 1 branch
BranchTrueFalseTotal
defined $self->maximum ? : 22 1 23
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
BranchTrueFalseTotal
DEBUG_CONSTRAINTS() and debug("OTHER_NAME", $name) 0 86 86
66 86 ▶ DEBUG_CONSTRAINTS && debug( VALUE => $value );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS() and debug("VALUE", $value) 0 86 86
67  
68 86 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 4 82 86
69 4 my @errors = eval { $self->constrain_values( $value, $params ) };
70  
71 4 ▶ if ( !@errors && !$@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@errors or $@) 4 0 4
Condition33%1/3
@errors or $@
l!l&&r!l&&!r
004
MC/DC0%0/2
@errors or $@
@errors $@
Truth table33%1/3
A or B
A= @errorsB= $@
ABresult
1X1
011
000
72 4 $count++;
73 }
74 }
75 else {
76 82 my $ok = eval { $self->constrain_value($value) };
77  
78 82 ▶ if ( $ok && !$@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and not $@) 37 45 82
Condition66%2/3
$ok and not $@
!ll&&!rl&&r
45037
MC/DC50%1/2
$ok and not $@
$ok not $@
Truth table66%2/3
A and not(B)
A= $okB= not $@
ABresult
0X0
100
111
79 37 $count++;
80 }
81 }
82 }
83  
84 23 ▶ my $pass = ( $count < $min || $count > $max ) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$count < $min || $count > $max ? : 7 16 23
Condition66%2/3
$count < $min or $count > $max
l!l&&r!l&&!r
0716
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
ABresult
1X1
011
000
85  
86 23 ▶ return $self->mk_errors(
Branch: 1 branch
BranchTrueFalseTotal
$pass ? : 16 7 23
87 { pass => $pass,
88 failed => $pass ? [] : \@names,
89 names => \@names,
90 } );
91 }
92  
93 # return true if value is defined
94 sub constrain_value {
95 87 ▶ my ( $self, $value ) = @_;
Subroutine constrain_value: called CC=3 SCAR=11.0
Pod undocumented
96  
97 87 ▶ return 0 if !defined $value || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 45 42 87
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
83742
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 ""
ABresult
1X1
011
000
98  
99 42 return 1;
100 }
101  
102 6 __PACKAGE__->meta->make_immutable;
103  
104 6 1;
105  
106 __END__
107  
108 =pod
109  
110 =encoding UTF-8
111  
112 =head1 NAME
113  
114 HTML::FormFu::Constraint::MinMaxFields - Min/Max Multi-field Constraint
115  
116 =head1 VERSION
117  
118 version 2.06
119  
120 =head1 SYNOPSIS
121  
122 type: MinMaxFields
123 name: foo
124 others: [bar, baz]
125 min: 1
126 max: 1
127  
128 =head1 DESCRIPTION
129  
130 Ensure that at least a minimum and only a maximum number of fields are
131 present.
132  
133 This constraint doesn't honour the C<not()> value.
134  
135 =head1 METHODS
136  
137 =head2 minimum
138  
139 =head2 min
140  
141 The minimum number of named fields which must be filled in.
142  
143 L</min> is an alias for L</minimum>.
144  
145 =head2 maximum
146  
147 =head2 max
148  
149 The maximum number of named fields which must be filled in.
150  
151 L</max> is an alias for L</maximum>.
152  
153 The default for max is the number of all affected fields, in other words one
154 more than the number of elements given to others.
155  
156 =head2 attach_errors_to_base
157  
158 Default Value: 1
159  
160 =head2 attach_errors_to_others
161  
162 Default Value: 0
163  
164 =head1 SEE ALSO
165  
166 Is a sub-class of, and inherits methods from
167 L<HTML::FormFu::Role::Constraint::Others>, L<HTML::FormFu::Constraint>
168  
169 L<HTML::FormFu>
170  
171 =head1 AUTHOR
172  
173 Mario Minati C<mario.minati@googlemail.com>
174  
175 =head1 LICENSE
176  
177 This library is free software, you can redistribute it and/or modify it under
178 the same terms as Perl itself.
179  
180 =head1 AUTHOR
181  
182 Carl Franks <cpan@fireartist.com>
183  
184 =head1 COPYRIGHT AND LICENSE
185  
186 This software is copyright (c) 2018 by Carl Franks.
187  
188 This is free software; you can redistribute it and/or modify it under
189 the same terms as the Perl 5 programming language system itself.
190  
191 =cut
« lib/HTML/FormFu/Localize.pm ↑ Summary lib/HTML/FormFu/Element/Select.pm »