lib/HTML/FormFu/Role/Element/Group.pm

filter: Statementstmt 89.8% 160 / 178 Branchbran 77.7% 70 / 90 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 95.4% 21 / 22 Podpod n/a totaltotal 87.0% 262 / 301 SCAR 43.1
CC
64
Cov
86%
_parse_option_hashref
41.0
__ANON__
13.0
options
7.1
CRAP 74.8
« lib/HTML/FormFu/Constraint.pm ↑ Summary lib/HTML/FormFu/Model/HashRef.pm »
1 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Role::Element::Group;
4 68 $HTML::FormFu::Role::Element::Group::VERSION = '2.06';
5 # ABSTRACT: Role for grouped form fields
6  
7 68 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 68 with 'HTML::FormFu::Role::Element::Field',
10 'HTML::FormFu::Role::Element::SingleValueField',
11 'HTML::FormFu::Role::Element::ProcessOptionsFromModel',
12 'HTML::FormFu::Role::Element::Coercible';
13  
14 68 ▶use HTML::FormFu::Attribute qw( mk_output_accessors );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 68 ▶use HTML::FormFu::Util qw( append_xml_attribute literal xml_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 68 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 68 ▶use List::Util 1.33 qw( none );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 68 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 68 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 68 has empty_first => ( is => 'rw', traits => ['Chained'] );
22  
23 68 __PACKAGE__->mk_output_accessors(qw( empty_first_label ));
24  
25 has _options => (
26 is => 'rw',
27 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 68 lazy => 1,
29 isa => 'ArrayRef',
30 );
31  
32 68 my @ALLOWED_OPTION_KEYS = qw(
33 group
34 value
35 value_xml
36 value_loc
37 label
38 label_xml
39 label_loc
40 attributes
41 attrs
42 attributes_xml
43 attrs_xml
44 container_attributes
45 container_attrs
46 container_attributes_xml
47 container_attrs_xml
48 label_attributes
49 label_attrs
50 label_attributes_xml
51 label_attrs_xml
52 );
53  
54 after BUILD => sub {
55 301 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
56  
57 301 $self->container_attributes( {} );
58  
59 301 return;
60 68 };
61  
62 after process => sub {
63 259 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
64  
65 259 $self->_process_options_from_model;
66  
67 259 return;
68 68 };
69  
70 sub options {
71 239 ▶ my ( $self, $arg ) = @_;
Subroutine options: called CC=7 SCAR=19.6
72 239 my ( @options, @new );
73  
74 239 ▶ return $self->_options if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 25 214 239
75  
76 214 ▶ croak "options argument must be a single array-ref" if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 214 214
77  
78 214 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 214 0 214
79 214 ▶ croak "options argument must be an array-ref"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($arg) ne "ARRAY" 0 214 214
80 if reftype($arg) ne 'ARRAY';
81  
82 214 @options = @$arg;
83  
84 214 ▶ if ( $self->empty_first ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->empty_first) 6 208 214
85 6 push @new, $self->_get_empty_first_option;
86 }
87  
88 214 for my $item (@options) {
89 3848 push @new, $self->_parse_option($item);
90 }
91 }
92  
93 212 $self->_options( \@new );
94  
95 212 return $self;
96 }
97  
98 sub _get_empty_first_option {
99 20 ▶ my ($self) = @_;
Subroutine _get_empty_first_option: called CC=2 SCAR=6.9
100  
101 20 ▶ my $label = $self->empty_first_label || '';
Condition100%2/2
$self->empty_first_label || ''
l!l
416
MC/DC100%1/1
$self->empty_first_label || ''
$self->empty_first_label
Truth table100%2/2
A
A= $self->empty_first_label
Aresult
11
00
102  
103 return {
104 20 value => '',
105 label => $label,
106 attributes => {},
107 container_attributes => {},
108 label_attributes => {},
109 };
110 }
111  
112 sub _parse_option {
113 3870 ▶ my ( $self, $item ) = @_;
Subroutine _parse_option: called CC=3 SCAR=11.3
114  
115 3870 ▶ if ( reftype($item) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($item) eq 'HASH') { } 38 3832 3870
elsif (builtin::reftype($item) eq 'ARRAY') { } 3832 0 3832
116 38 return $self->_parse_option_hashref($item);
117 }
118 elsif ( reftype($item) eq 'ARRAY' ) {
119 return {
120 3832 value => $item->[0],
121 label => $item->[1],
122 attributes => {},
123 container_attributes => {},
124 label_attributes => {},
125 };
126 }
127 else {
128 0 croak "each options argument must be a hash-ref or array-ref";
129 }
130 }
131  
132 sub _parse_option_hashref {
133 38 ▶ my ( $self, $item ) = @_;
Subroutine _parse_option_hashref: called CC=32 SCAR=37.1
134  
135 # sanity check options
136 38 my @keys = keys %$item;
137  
138 38 for my $key (@keys) {
139 croak "unknown option argument: '$key'"
140 84 ▶ if none { $key eq $_ } @ALLOWED_OPTION_KEYS;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $key eq $_; } , @ALLOWED_OPTION_KEYS) 1 83 84
141  
142 83 my $short = $key;
143  
144 83 ▶ if ( $short =~ s/attributes/attrs/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short =~ s/attributes/attrs/) 20 63 83
145 20 for my $cmp (@keys) {
146 75 ▶ next if $cmp eq $key;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp eq $key 20 55 75
147  
148 55 ▶ croak "cannot use both '$key' and '$short' arguments"
Branch: 1 branch
BranchTrueFalseTotal
if $cmp eq $short 1 54 55
149 if $cmp eq $short;
150 }
151 }
152 }
153  
154 36 ▶ if ( exists $item->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"group"}) 11 25 36
155 11 my @group = @{ $item->{group} };
156 11 my @new;
157 11 for my $groupitem (@group) {
158 22 push @new, $self->_parse_option($groupitem);
159 }
160 11 $item->{group} = \@new;
161 }
162  
163 36 ▶ if ( !exists $item->{attributes} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $item->{"attributes"}) 26 10 36
164 $item->{attributes}
165 = exists $item->{attrs}
166 ? $item->{attrs}
167 26 ▶ : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $item->{'attrs'} ? : 1 25 26
168 }
169  
170 36 ▶ if ( exists $item->{attributes_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $item->{'attributes_xml'}) { } 3 33 36
elsif (exists $item->{'attrs_xml'}) { } 1 32 33
171 3 for my $key ( keys %{ $item->{attributes_xml} } ) {
172 $item->{attributes}{$key}
173 3 = literal( $item->{attributes_xml}{$key} );
174 }
175 }
176 elsif ( exists $item->{attrs_xml} ) {
177 1 for my $key ( keys %{ $item->{attrs_xml} } ) {
178 1 $item->{attributes}{$key} = literal( $item->{attrs_xml}{$key} );
179 }
180 }
181  
182 36 ▶ if ( !exists $item->{container_attributes} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $item->{"container_attributes"}) 32 4 36
183 $item->{container_attributes}
184 = exists $item->{container_attrs}
185 ? $item->{container_attrs}
186 32 ▶ : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $item->{'container_attrs'} ? : 0 32 32
187 }
188  
189 36 ▶ if ( exists $item->{container_attributes_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $item->{'container_attributes_xml'}) { } 0 36 36
elsif (exists $item->{'container_attrs_xml'}) { } 0 36 36
190 0 for my $key ( keys %{ $item->{container_attributes_xml} } ) {
191 $item->{container_attributes}{$key}
192 0 = literal( $item->{container_attributes_xml}{$key} );
193 }
194 }
195 elsif ( exists $item->{container_attrs_xml} ) {
196 0 for my $key ( keys %{ $item->{container_attrs_xml} } ) {
197 $item->{container_attributes}{$key}
198 0 = literal( $item->{container_attrs_xml}{$key} );
199 }
200 }
201  
202 36 ▶ if ( !exists $item->{label_attributes} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $item->{"label_attributes"}) 34 2 36
203 $item->{label_attributes}
204 = exists $item->{label_attrs}
205 ? $item->{label_attrs}
206 34 ▶ : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $item->{'label_attrs'} ? : 0 34 34
207 }
208  
209 36 ▶ if ( exists $item->{label_attributes_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $item->{'label_attributes_xml'}) { } 0 36 36
elsif (exists $item->{'label_attrs_xml'}) { } 0 36 36
210 0 for my $key ( keys %{ $item->{label_attributes_xml} } ) {
211 $item->{label_attributes}{$key}
212 0 = literal( $item->{label_attributes_xml}{$key} );
213 }
214 }
215 elsif ( exists $item->{label_attrs_xml} ) {
216 0 for my $key ( keys %{ $item->{label_attrs_xml} } ) {
217 $item->{label_attributes}{$key}
218 0 = literal( $item->{label_attrs_xml}{$key} );
219 }
220 }
221  
222 36 ▶ if ( defined $item->{label_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $item->{'label_xml'}) { } 2 34 36
elsif (defined $item->{'label_loc'}) { } 1 33 34
223 2 $item->{label} = literal( $item->{label_xml} );
224 }
225 elsif ( defined $item->{label_loc} ) {
226 1 $item->{label} = $self->form->localize( $item->{label_loc} );
227 }
228  
229 36 ▶ if ( defined $item->{value_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $item->{'value_xml'}) { } 2 34 36
elsif (defined $item->{'value_loc'}) { } 0 34 34
230 2 $item->{value} = literal( $item->{value_xml} );
231 }
232 elsif ( defined $item->{value_loc} ) {
233 0 $item->{value} = $self->form->localize( $item->{value_loc} );
234 }
235  
236 36 ▶ if ( !defined $item->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item->{"value"}) 12 24 36
237 12 $item->{value} = '';
238 }
239  
240 36 return $item;
241 }
242  
243 sub values {
244 55 ▶ my ( $self, $arg ) = @_;
Subroutine values: called CC=5 SCAR=16.3
245  
246 55 ▶ croak "values argument must be a single array-ref of values" if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 55 55
247  
248 55 my @values;
249  
250 55 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 55 0 55
251 55 ▶ croak "values argument must be an array-ref"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($arg) ne "ARRAY" 0 55 55
252 if reftype($arg) ne 'ARRAY';
253  
254 55 @values = @$arg;
255 }
256  
257 my @new = map {
258 55 { value => $_,
259 label => ucfirst $_,
260 attributes => {},
261 container_attributes => {},
262 label_attributes => {},
263 }
264 } @values;
265  
266 55 ▶ if ( $self->empty_first ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->empty_first) 14 41 55
267 14 unshift @new, $self->_get_empty_first_option;
268 }
269  
270 55 $self->_options( \@new );
271  
272 55 return $self;
273 }
274  
275 sub value_range {
276 3 ▶ my ( $self, $arg ) = @_;
Subroutine value_range: called CC=5 SCAR=16.6
277 3 my (@values);
278  
279 3 ▶ croak "value_range argument must be a single array-ref of values"
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 3 3
280 if @_ > 2;
281  
282 3 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 3 0 3
283 3 ▶ croak "value_range argument must be an array-ref"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($arg) ne "ARRAY" 0 3 3
284 if reftype($arg) ne 'ARRAY';
285  
286 3 @values = @$arg;
287 }
288  
289 3 ▶ croak "range must contain at least 2 values" if @values < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @values < 2 0 3 3
290  
291 3 my $end = pop @values;
292 3 my $start = pop @values;
293  
294 3 return $self->values( [ @values, $start .. $end ] );
295 }
296  
297 before prepare_attrs => sub {
298 475 ▶ my ( $self, $render ) = @_;
Subroutine __ANON__: called CC=13 SCAR=25.7
299  
300 475 my $submitted = $self->form->submitted;
301 475 my $default = $self->default;
302  
303 475 ▶ my $value
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name ? : 475 0 475
304 = defined $self->name
305 ? $self->get_nested_hash_value( $self->form->input, $self->nested_name )
306 : undef;
307  
308 475 ▶ if ( ( reftype($value) || '' ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($value) || "") eq "ARRAY") 6 469 475
Condition100%2/2
builtin::reftype($value) || ''
l!l
6469
MC/DC100%1/1
builtin::reftype($value) || ''
builtin::reftype($value)
Truth table100%2/2
A
A= builtin::reftype($value)
Aresult
11
00
309 6 my $elems
310 = $self->form->get_fields( { nested_name => $self->nested_name } );
311 6 ▶ if ($#$elems) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elems->$#*) 4 2 6
312  
313 # There are multiple fields with the same name; assume
314 # none are multi-value fields, i.e. only one selected
315 # option per field. (Otherwise it might be ambiguous
316 # which option came from which field.)
317 4 for ( 0 .. @$elems - 1 ) {
318 8 ▶ if ( $self == $elems->[$_] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self == $elems->[$_]) 4 4 8
319  
320 # Use the value of the option actually selected in
321 # this group.
322 4 $value = $value->[$_];
323 }
324 }
325 }
326 }
327  
328 475 ▶ if ( !$submitted && defined $default ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $submitted and defined $default) 187 288 475
Condition100%3/3
not $submitted and defined $default
!ll&&!rl&&r
19989187
MC/DC100%2/2
not $submitted and defined $default
not $submitted defined $default
Truth table100%3/3
not(A) and B
A= not $submittedB= defined $default
ABresult
0X0
100
111
329 187 for my $deflator ( @{ $self->_deflators } ) {
330 1 $default = $deflator->process($default);
331 }
332 }
333  
334 475 for my $option ( @{ $render->{options} } ) {
335 8122 ▶ if ( exists $option->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{'group'}) { } 29 8093 8122
336 29 for my $item ( @{ $option->{group} } ) {
337 58 $self->_prepare_attrs( $submitted, $value, $default, $item );
338 }
339 }
340 else {
341 8093 $self->_prepare_attrs( $submitted, $value, $default, $option );
342 }
343 }
344  
345 475 return;
346 68 };
347  
348 around render_data_non_recursive => sub {
349 475 ▶ my ( $orig, $self, $args ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
350  
351 475 ▶ my $render = $self->$orig(
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 0 475 475
352 { options => Clone::clone( $self->_options ),
353 $args ? %$args : (),
354 } );
355  
356 475 $self->_quote_options( $render->{options} );
357  
358 475 return $render;
359 68 };
360  
361 sub _quote_options {
362 504 ▶ my ( $self, $options ) = @_;
Subroutine _quote_options: called CC=3 SCAR=11.0
363  
364 504 foreach my $opt (@$options) {
365 8180 $opt->{label} = xml_escape( $opt->{label} );
366 8180 $opt->{value} = xml_escape( $opt->{value} );
367 8180 $opt->{attributes} = xml_escape( $opt->{attributes} );
368 8180 $opt->{label_attributes} = xml_escape( $opt->{label_attributes} );
369 $opt->{container_attributes}
370 8180 = xml_escape( $opt->{container_attributes} );
371  
372 8180 ▶ if ( exists $opt->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"group"}) 29 8151 8180
373 29 $self->_quote_options( $opt->{group} );
374 }
375 }
376 }
377  
378 around clone => sub {
379 0 ▶ my ( $orig, $self ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
380  
381 0 my $clone = $self->$orig(@_);
382  
383 0 $clone->_options( Clone::clone( $self->_options ) );
384  
385 0 return $clone;
386 68 };
387  
388 68 1;
389  
390 __END__
391  
392 =pod
393  
394 =encoding UTF-8
395  
396 =head1 NAME
397  
398 HTML::FormFu::Role::Element::Group - Role for grouped form fields
399  
400 =head1 VERSION
401  
402 version 2.06
403  
404 =head1 DESCRIPTION
405  
406 Base class for L<HTML::FormFu::Element::Checkboxgroup>,
407 L<HTML::FormFu::Element::Radiogroup>, and
408 L<HTML::FormFu::Element::Select> fields.
409  
410 =head1 METHODS
411  
412 =head2 options
413  
414 Arguments: none
415  
416 Arguments: \@options
417  
418 ---
419 elements:
420 - type: Select
421 name: foo
422 options:
423 - [ 01, January ]
424 - [ 02, February ]
425 - value: 03
426 label: March
427 attributes:
428 style: highlighted
429 - [ 04, April ]
430  
431 If passed no arguments, it returns an arrayref of the currently set options.
432  
433 Use to set the list of items in the select menu / radiogroup.
434  
435 Its arguments must be an array-ref of items. Each item may be an array ref
436 of the form C<[ $value, $label ]> or a hash-ref of the form
437 C<< { value => $value, label => $label } >>. Each hash-ref may also have an
438 C<attributes> key.
439  
440 Passing an item containing a C<group> key will, for
441 L<Select fields|HTML::FormFu::Element::Select>, create an optgroup. And for
442 L<Radiogroup fields|HTML::FormFu::Element::Radiogroup> or
443 L<Checkboxgroup fields|HTML::FormFu::Element::Checkboxgroup>, create a
444 sub-group of radiobuttons or checkboxes with a new C<span> block, with the
445 classname C<subgroup>.
446  
447 An example of Select optgroups:
448  
449 ---
450 elements:
451 - type: Select
452 name: foo
453 options:
454 - label: "group 1"
455 group:
456 - [1a, 'item 1a']
457 - [1b, 'item 1b']
458 - label: "group 2"
459 group:
460 - [2a, 'item 2a']
461 - [2b, 'item 2b']
462  
463 When using the hash-ref construct, the C<label_xml> and C<label_loc>
464 variants of C<label> are supported, as are the C<value_xml> and C<value_loc>
465 variants of C<value>, the C<attributes_xml> variant of C<attributes> and the
466 C<label_attributes_xml> variant of C<label_attributes>.
467  
468 C<container_attributes> or C<container_attributes_xml> is used by
469 L<HTML::FormFu::Element::Checkboxgroup> and
470 L<HTML::FormFu::Element::Radiogroup> for the c<span> surrounding each
471 item's input and label. It is ignored by L<HTML::FormFu::Element::Select>
472 elements.
473  
474 C<label_attributes> / C<label_attributes_xml> is used by
475 L<HTML::FormFu::Element::Checkboxgroup> and
476 L<HTML::FormFu::Element::Radiogroup> for the c<label> tag of each item.
477 It is ignored by L<HTML::FormFu::Element::Select> elements.
478  
479 =head2 values
480  
481 Arguments: \@values
482  
483 ---
484 elements:
485 - type: Radiogroup
486 name: foo
487 values:
488 - jan
489 - feb
490 - mar
491 - apr
492  
493 A more concise alternative to L</options>. Use to set the list of values in
494 the select menu / radiogroup.
495  
496 Its arguments must be an array-ref of values. The labels used are the
497 result of C<ucfirst($value)>.
498  
499 =head2 value_range
500  
501 Arguments: \@values
502  
503 ---
504 elements:
505 - type: Select
506 name: foo
507 value_range:
508 - ""
509 - 1
510 - 12
511  
512 Similar to L</values>, but the last 2 values are expanded to a range. Any
513 preceding values are used literally, allowing the common empty first item
514 in select menus.
515  
516 =head2 empty_first
517  
518 If true, then a blank option will be inserted at the start of the option list
519 (regardless of whether L</options>, L</values> or L</value_range> was used to
520 populate the options). See also L</empty_first_label>.
521  
522 =head2 empty_first_label
523  
524 =head2 empty_first_label_xml
525  
526 =head2 empty_first_label_loc
527  
528 If L</empty_first> is true, and L</empty_first_label> is set, this value will
529 be used as the label for the first option - so only the first option's value
530 will be empty.
531  
532 =head1 SEE ALSO
533  
534 Is a sub-class of, and inherits methods from
535 L<HTML::FormFu::Role::Element::Field>, L<HTML::FormFu::Element>
536  
537 L<HTML::FormFu>
538  
539 =head1 AUTHOR
540  
541 Carl Franks, C<cfranks@cpan.org>
542  
543 =head1 LICENSE
544  
545 This library is free software, you can redistribute it and/or modify it under
546 the same terms as Perl itself.
547  
548 =head1 AUTHOR
549  
550 Carl Franks <cpan@fireartist.com>
551  
552 =head1 COPYRIGHT AND LICENSE
553  
554 This software is copyright (c) 2018 by Carl Franks.
555  
556 This is free software; you can redistribute it and/or modify it under
557 the same terms as the Perl 5 programming language system itself.
558  
559 =cut
« lib/HTML/FormFu/Constraint.pm ↑ Summary lib/HTML/FormFu/Model/HashRef.pm »