| 1 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | |||||||||||||||
| 4 | 3 | ▶ | use UNIVERSAL; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | package Data::FormValidator::Multi::Results; | ||||||||||||||
| 7 | 3 | ▶ | use base qw(Data::FormValidator::Results); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use List::Util qw(all); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | =encoding utf8 | ||||||||||||||
| 11 | |||||||||||||||
| 12 | =head1 NAME | ||||||||||||||
| 13 | |||||||||||||||
| 14 | Data::FormValidator::Multi::Results - Provide a multidimensional hash or array of DFVM results | ||||||||||||||
| 15 | |||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||
| 17 | |||||||||||||||
| 18 | # run the check on the data | ||||||||||||||
| 19 | my $results = $dfv->check( $data ); | ||||||||||||||
| 20 | |||||||||||||||
| 21 | if ( ! $results->success ) { | ||||||||||||||
| 22 | $c->stash->{json}{errors} = $results->to_json; | ||||||||||||||
| 23 | return; | ||||||||||||||
| 24 | } | ||||||||||||||
| 25 | |||||||||||||||
| 26 | # handle valid data | ||||||||||||||
| 27 | |||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||
| 29 | |||||||||||||||
| 30 | Results of the check performed by Data::FormValidator::Multi | ||||||||||||||
| 31 | |||||||||||||||
| 32 | =head1 METHODS | ||||||||||||||
| 33 | |||||||||||||||
| 34 | =head2 success | ||||||||||||||
| 35 | |||||||||||||||
| 36 | If this is an array of results, return true if all of the elements in the array | ||||||||||||||
| 37 | are valid. | ||||||||||||||
| 38 | |||||||||||||||
| 39 | If DF::Multi found invalid sub elements, return false. Otherwise, return | ||||||||||||||
| 40 | the parent class result of success. | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =cut | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 20 | ▶ | sub success { | ||||||||||||
|
Subroutine
success: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 45 | 20 | my $self = shift; | |||||||||||||
| 46 | |||||||||||||||
| 47 | 20 | ▶ | if ( $self->isa('ARRAY') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | 1 | ▶ | return all { $_->success } @$self; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 49 | } else { | ||||||||||||||
| 50 | 19 | ▶ | return $self->has_objects ? undef : $self->SUPER::success; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | } | ||||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =head2 to_json | ||||||||||||||
| 55 | |||||||||||||||
| 56 | If this is an array of results, call to_json on each element and return an array | ||||||||||||||
| 57 | of the results. Otherwise, return a data structure that represents the invalid | ||||||||||||||
| 58 | (if any) data in the object. | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =cut | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 6 | ▶ | sub to_json { | ||||||||||||
|
Subroutine
to_json: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||
| 63 | 6 | my $self = shift; | |||||||||||||
| 64 | |||||||||||||||
| 65 | 6 | my $json = []; | |||||||||||||
| 66 | |||||||||||||||
| 67 | 6 | ▶ | if ( $self->isa('ARRAY') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 0 | foreach my $results ( @$self ) { | |||||||||||||
| 69 | 0 | push @$json => $results->to_json; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | } else { | ||||||||||||||
| 72 | 6 | $json = $self->profile_json; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 6 | return $json; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | =head2 profile_json | ||||||||||||||
| 79 | |||||||||||||||
| 80 | Build a hash with invalid field names as keys and that field's errors as the | ||||||||||||||
| 81 | value. Iterate over the invalid nested objects and call to_json on them. | ||||||||||||||
| 82 | |||||||||||||||
| 83 | =cut | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 6 | ▶ | sub profile_json { | ||||||||||||
|
Subroutine
profile_json: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||
| 86 | 6 | my $self = shift; | |||||||||||||
| 87 | |||||||||||||||
| 88 | 6 | my $json = {}; my $messages = $self->msgs; | |||||||||||||
| 89 | |||||||||||||||
| 90 | 6 | foreach my $field ( $self->missing, $self->invalid ) { | |||||||||||||
| 91 | 5 | $json->{$field} = $messages->{$field}; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 6 | foreach my $field ( $self->objects ) { | |||||||||||||
| 95 | 4 | my $results = $self->objects->{ $field }; | |||||||||||||
| 96 | |||||||||||||||
| 97 | 4 | ▶ | if ( ref $results eq 'ARRAY' ) { # at least one element from input array has error | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | 2 | my $errors = $json->{$field} = []; | |||||||||||||
| 99 | 2 | foreach my $result ( @$results ) { | |||||||||||||
| 100 | # if ( $result ) { # uhhh this returns false even when its an object? | ||||||||||||||
| 101 | 4 | ▶ | if ( UNIVERSAL::can( $result => 'to_json' ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | 2 | push @$errors => $result->to_json | |||||||||||||
| 103 | } else { | ||||||||||||||
| 104 | 2 | push @$errors => undef; | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | } | ||||||||||||||
| 107 | } else { | ||||||||||||||
| 108 | 2 | $json->{$field} = $results->to_json; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 6 | return $json; | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =head2 has_objects | ||||||||||||||
| 116 | |||||||||||||||
| 117 | This method returns true if the results contain objects fields. | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =cut | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 19 | ▶ | sub has_objects { | ||||||||||||
|
Subroutine
has_objects: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 122 | 19 | return scalar keys %{$_[0]{objects}}; | |||||||||||||
| 123 | |||||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =head2 objects( [field] ) | ||||||||||||||
| 127 | |||||||||||||||
| 128 | In list context, it returns the list of fields which are objects. | ||||||||||||||
| 129 | In a scalar context, it returns an hash reference which contains the objects | ||||||||||||||
| 130 | fields and their values. | ||||||||||||||
| 131 | |||||||||||||||
| 132 | If called with an argument, it returns the value of that C<field> if it | ||||||||||||||
| 133 | is objects, undef otherwise. | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =cut | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 10 | ▶ | sub objects { | ||||||||||||
|
Subroutine
objects: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||
| 138 | 10 | ▶ | return (wantarray ? Data::FormValidator::Results::_arrayify($_[0]{objects}{$_[1]}) : $_[0]{objects}{$_[1]}) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 139 | if (defined $_[1]); | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 10 | ▶ | wantarray ? keys %{$_[0]{objects}} : $_[0]{objects}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | } | ||||||||||||||
| 143 | |||||||||||||||
| 144 | =head1 SEE ALSO | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =over 4 | ||||||||||||||
| 147 | |||||||||||||||
| 148 | =item * | ||||||||||||||
| 149 | |||||||||||||||
| 150 | L<Data::FormValidator::Results> | ||||||||||||||
| 151 | |||||||||||||||
| 152 | =back | ||||||||||||||
| 153 | |||||||||||||||
| 154 | =head1 AUTHOR | ||||||||||||||
| 155 | |||||||||||||||
| 156 | Todd Wade <waveright@gmail.com> | ||||||||||||||
| 157 | |||||||||||||||
| 158 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 159 | |||||||||||||||
| 160 | This software is copyright (c) 2016 by Todd Wade. | ||||||||||||||
| 161 | |||||||||||||||
| 162 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 163 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 164 | |||||||||||||||
| 165 | =cut | ||||||||||||||
| 166 | |||||||||||||||
| 167 | 3 | 1; | |||||||||||||