| 1 | package Chloro::Role::Form; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 11 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use Chloro::Error::Form; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use Chloro::ErrorMessage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use Chloro::Result::Field; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 11 | ▶ | use Chloro::Result::Group; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 11 | ▶ | use Chloro::ResultSet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 11 | ▶ | use Chloro::Types qw( HashRef ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use List::AllUtils qw( all ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 11 | ▶ | use MooseX::Params::Validate qw( validated_list ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 30 | ▶ | sub fields { | ||||||||||||||||||||||||||
|
Subroutine
fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 30 | my $self = shift; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 30 | return $self->meta()->fields(); | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 27 | ▶ | sub groups { | ||||||||||||||||||||||||||
|
Subroutine
groups: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 27 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 27 | return $self->meta()->groups(); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 22 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 22 | my ($params) = validated_list( | |||||||||||||||||||||||||||
| 35 | \@_, | ||||||||||||||||||||||||||||
| 36 | params => { isa => HashRef }, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 22 | my %results; | |||||||||||||||||||||||||||
| 40 | 22 | for my $field ( $self->fields() ) { | |||||||||||||||||||||||||||
| 41 | 52 | $results{ $field->name() } | |||||||||||||||||||||||||||
| 42 | = $self->_result_for_field( $field, $params ); | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 22 | for my $group ( $self->groups() ) { | |||||||||||||||||||||||||||
| 46 | 5 | for my $result ( $self->_results_for_group( $group, $params ) ) { | |||||||||||||||||||||||||||
| 47 | 8 | $results{ $result->prefix() } = $result; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | my @form_errors = map { Chloro::Error::Form->new( message => $_ ) } | |||||||||||||||||||||||||||
| 52 | map { | ||||||||||||||||||||||||||||
| 53 | 22 | ▶ | ref $_ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | ? $_ | ||||||||||||||||||||||||||||
| 55 | : Chloro::ErrorMessage->new( | ||||||||||||||||||||||||||||
| 56 | text => $_, | ||||||||||||||||||||||||||||
| 57 | category => 'invalid', | ||||||||||||||||||||||||||||
| 58 | ) | ||||||||||||||||||||||||||||
| 59 | } $self->_validate_form( $params, \%results ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 22 | return $self->_make_resultset( $params, \%results, \@form_errors ); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 22 | ▶ | sub _make_resultset { | ||||||||||||||||||||||||||
|
Subroutine
_make_resultset: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 66 | 22 | my $params = shift; | |||||||||||||||||||||||||||
| 67 | 22 | my $results = shift; | |||||||||||||||||||||||||||
| 68 | 22 | my $form_errors = shift; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 22 | return $self->_resultset_class()->new( | |||||||||||||||||||||||||||
| 71 | params => $params, | ||||||||||||||||||||||||||||
| 72 | results => $results, | ||||||||||||||||||||||||||||
| 73 | form_errors => $form_errors, | ||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 22 | ▶ | sub _resultset_class { | ||||||||||||||||||||||||||
|
Subroutine
_resultset_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 22 | return 'Chloro::ResultSet'; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 80 | ▶ | sub _result_for_field { | ||||||||||||||||||||||||||
|
Subroutine
_result_for_field: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 82 | 80 | my $self = shift; | |||||||||||||||||||||||||||
| 83 | 80 | my $field = shift; | |||||||||||||||||||||||||||
| 84 | 80 | my $params = shift; | |||||||||||||||||||||||||||
| 85 | 80 | my $prefix = shift; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 80 | my @return = $self->_validate_field( $field, $params, $prefix ); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 80 | my ( $value, $names, $errors ); | |||||||||||||||||||||||||||
| 90 | 80 | ▶ | if (@return) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 76 | ( $value, $names, $errors ) = @return; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||
| 94 | 4 | $names = []; | |||||||||||||||||||||||||||
| 95 | 4 | $errors = []; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | return Chloro::Result::Field->new( | ||||||||||||||||||||||||||||
| 99 | field => $field, | ||||||||||||||||||||||||||||
| 100 | param_names => $names, | ||||||||||||||||||||||||||||
| 101 | errors => [ | ||||||||||||||||||||||||||||
| 102 | map { | ||||||||||||||||||||||||||||
| 103 | 8 | Chloro::Error::Field->new( field => $field, message => $_ ) | |||||||||||||||||||||||||||
| 104 | 80 | ▶ | } @{$errors} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | ], | ||||||||||||||||||||||||||||
| 106 | ( defined $value ? ( value => $value ) : () ), | ||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 80 | ▶ | sub _validate_field { | ||||||||||||||||||||||||||
|
Subroutine
_validate_field: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 111 | 80 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 80 | my $field = shift; | |||||||||||||||||||||||||||
| 113 | 80 | my $params = shift; | |||||||||||||||||||||||||||
| 114 | 80 | my $prefix = shift; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 80 | my $extractor = $field->extractor(); | |||||||||||||||||||||||||||
| 117 | 80 | my ( $value, @names ) = $self->$extractor( $params, $prefix, $field ); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 80 | ▶ | $value = $field->generate_default( $params, $prefix ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $value and $field->has_default
MC/DC100%2/2
not defined $value and $field->has_default
not defined $value
$field->has_default
Truth table100%3/3
not(A) and B
A= not defined $valueB= $field->has_default
| |||||||||||||||||||||||||||||
| 120 | if !defined $value && $field->has_default(); | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # A missing boolean should be treated as false (an unchecked checkbox does | ||||||||||||||||||||||||||||
| 123 | # not show up in user-submitted parameters). | ||||||||||||||||||||||||||||
| 124 | 80 | ▶ | if ( _value_is_empty($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 10 | ▶ | if ( $field->type()->is_a_type_of('Bool') ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 126 | 1 | $value = 0; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | elsif ( !$field->is_required() ) { | ||||||||||||||||||||||||||||
| 129 | 4 | return; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 76 | my $validator = $field->validator(); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 76 | my @errors; | |||||||||||||||||||||||||||
| 136 | 76 | ▶ | if ( $field->is_required() && _value_is_empty($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$field->is_required and _value_is_empty($value)
MC/DC100%2/2
$field->is_required and _value_is_empty($value)
$field->is_required
_value_is_empty($value)
Truth table100%3/3
A and B
A= $field->is_requiredB= _value_is_empty($value)
| |||||||||||||||||||||||||||||
| 137 | 5 | push @errors, | |||||||||||||||||||||||||||
| 138 | Chloro::ErrorMessage->new( | ||||||||||||||||||||||||||||
| 139 | text => 'The ' . $field->human_name() . ' field is required.', | ||||||||||||||||||||||||||||
| 140 | category => 'missing', | ||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||
| 144 | # The validate() method returns false on valid (bah) | ||||||||||||||||||||||||||||
| 145 | 71 | ▶ | if ( $field->type()->validate($value) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # XXX - we are ignoring the Moose-returned message for now, because | ||||||||||||||||||||||||||||
| 148 | # it's not at all end user friendly. | ||||||||||||||||||||||||||||
| 149 | 2 | push @errors, | |||||||||||||||||||||||||||
| 150 | Chloro::ErrorMessage->new( | ||||||||||||||||||||||||||||
| 151 | text => 'The ' | ||||||||||||||||||||||||||||
| 152 | . $field->human_name() | ||||||||||||||||||||||||||||
| 153 | . ' field did not contain a valid value.', | ||||||||||||||||||||||||||||
| 154 | category => 'invalid', | ||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | elsif ( my $msg | ||||||||||||||||||||||||||||
| 158 | = $self->$validator( $value, $params, $prefix, $field ) ) { | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 1 | ▶ | push @errors, ref $msg | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | ? $msg | ||||||||||||||||||||||||||||
| 162 | : Chloro::ErrorMessage->new( | ||||||||||||||||||||||||||||
| 163 | text => $msg, | ||||||||||||||||||||||||||||
| 164 | category => 'invalid', | ||||||||||||||||||||||||||||
| 165 | ); | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 76 | return ( $value, \@names, \@errors ); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||||
| 173 | 77 | ▶ | sub _extract_field_value { | ||||||||||||||||||||||||||
|
Subroutine
_extract_field_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 174 | 77 | my $self = shift; | |||||||||||||||||||||||||||
| 175 | 77 | my $params = shift; | |||||||||||||||||||||||||||
| 176 | 77 | my $prefix = shift; | |||||||||||||||||||||||||||
| 177 | 77 | my $field = shift; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 77 | my $key = join q{.}, grep {defined} $prefix, $field->name(); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 77 | return ( $params->{$key}, $key ); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 67 | ▶ | sub _errors_for_field_value { | ||||||||||||||||||||||||||
|
Subroutine
_errors_for_field_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # my $self = shift; | ||||||||||||||||||||||||||||
| 187 | # my $value = shift; | ||||||||||||||||||||||||||||
| 188 | # my $params = shift; | ||||||||||||||||||||||||||||
| 189 | # my $prefix = shift; | ||||||||||||||||||||||||||||
| 190 | # my $field = shift; | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 67 | return; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | ## use critic | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 5 | ▶ | sub _results_for_group { | ||||||||||||||||||||||||||
|
Subroutine
_results_for_group: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 197 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 198 | 5 | my $group = shift; | |||||||||||||||||||||||||||
| 199 | 5 | my $params = shift; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 5 | my $keys = $params->{ $group->repetition_key() }; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 9 | return map { $self->_result_for_group_by_key( $group, $params, $_ ) } | |||||||||||||||||||||||||||
| 204 | 5 | ▶ | grep { defined && length } ref $keys ? @{$keys} : $keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $_ && length $_
MC/DC0%0/2
defined $_ && length $_
defined $_
length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 9 | ▶ | sub _result_for_group_by_key { | ||||||||||||||||||||||||||
|
Subroutine
_result_for_group_by_key: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 208 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 209 | 9 | my $group = shift; | |||||||||||||||||||||||||||
| 210 | 9 | my $params = shift; | |||||||||||||||||||||||||||
| 211 | 9 | my $key = shift; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 9 | my $prefix = join q{.}, $group->name(), $key; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 9 | my $checker = $group->is_empty_checker(); | |||||||||||||||||||||||||||
| 216 | 9 | ▶ | return if $self->$checker( $params, $prefix, $group ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 8 | my %results; | |||||||||||||||||||||||||||
| 219 | 8 | for my $field ( $group->fields() ) { | |||||||||||||||||||||||||||
| 220 | 28 | $results{ $field->name() } | |||||||||||||||||||||||||||
| 221 | = $self->_result_for_field( $field, $params, $prefix ); | ||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 8 | return Chloro::Result::Group->new( | |||||||||||||||||||||||||||
| 225 | group => $group, | ||||||||||||||||||||||||||||
| 226 | key => $key, | ||||||||||||||||||||||||||||
| 227 | prefix => $prefix, | ||||||||||||||||||||||||||||
| 228 | results => \%results, | ||||||||||||||||||||||||||||
| 229 | ); | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||||
| 233 | 9 | ▶ | sub _group_is_empty { | ||||||||||||||||||||||||||
|
Subroutine
_group_is_empty: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 234 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 235 | 9 | my $params = shift; | |||||||||||||||||||||||||||
| 236 | 9 | my $prefix = shift; | |||||||||||||||||||||||||||
| 237 | 9 | my $group = shift; | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 13 | ▶ | return all { !( defined $params->{$_} && length $params->{$_} ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Condition66%2/3
defined $params->{$_} && length $params->{$_}
MC/DC50%1/2
defined $params->{$_} && length $params->{$_}
defined $params->{$_}
length $params->{$_}
Truth table66%2/3
A && B
A= defined $params->{$_}B= length $params->{$_}
| |||||||||||||||||||||||||||||
| 240 | 9 | map { join q{.}, $prefix, $_->name() } $group->fields(); | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | ## use critic | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 17 | ▶ | sub _validate_form { } | ||||||||||||||||||||||||||
|
Subroutine
_validate_form: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 130 | ▶ | sub _value_is_empty { | ||||||||||||||||||||||||||
|
Subroutine
_value_is_empty: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 247 | 130 | ▶ | return defined $_[0] && length $_[0] ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_[0] and length $_[0]
MC/DC100%2/2
defined $_[0] and length $_[0]
defined $_[0]
length $_[0]
Truth table100%3/3
A and B
A= defined $_[0]B= length $_[0]
| |||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 11 | 1; | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | # ABSTRACT: A role for form classes | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | __END__ | ||||||||||||||||||||||||||||