File Coverage

blib/lib/Data/FormValidator/Multi/Results.pm
Criterion Covered Total %
statement 45 47 95.7
branch 12 16 75.0
condition n/a
subroutine 11 11 100.0
pod 5 5 100.0
total 73 79 92.4


line stmt bran cond sub pod time code
1 3     3   20 use strict;
  3         6  
  3         155  
2 3     3   16 use warnings;
  3         6  
  3         74  
3              
4 3     3   2482 use UNIVERSAL;
  3         44  
  3         12  
5              
6             package Data::FormValidator::Multi::Results;
7 3     3   145 use base qw(Data::FormValidator::Results);
  3         8  
  3         1969  
8 3     3   67276 use List::Util qw(all);
  3         9  
  3         1636  
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             sub success {
45 20     20 1 648 my $self = shift;
46              
47 20 100       96 if ( $self->isa('ARRAY') ) {
48 1     1   50 return all { $_->success } @$self;
  1         5  
49             } else {
50 19 100       47 return $self->has_objects ? undef : $self->SUPER::success;
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             sub to_json {
63 6     6 1 15 my $self = shift;
64              
65 6         15 my $json = [];
66              
67 6 50       28 if ( $self->isa('ARRAY') ) {
68 0         0 foreach my $results ( @$self ) {
69 0         0 push @$json => $results->to_json;
70             }
71             } else {
72 6         33 $json = $self->profile_json;
73             }
74              
75 6         30 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             sub profile_json {
86 6     6 1 13 my $self = shift;
87              
88 6         28 my $json = {}; my $messages = $self->msgs;
  6         29  
89              
90 6         72 foreach my $field ( $self->missing, $self->invalid ) {
91 5         78 $json->{$field} = $messages->{$field};
92             }
93              
94 6         47 foreach my $field ( $self->objects ) {
95 4         10 my $results = $self->objects->{ $field };
96              
97 4 100       14 if ( ref $results eq 'ARRAY' ) { # at least one element from input array has error
98 2         8 my $errors = $json->{$field} = [];
99 2         6 foreach my $result ( @$results ) {
100             # if ( $result ) { # uhhh this returns false even when its an object?
101 4 100       16 if ( UNIVERSAL::can( $result => 'to_json' ) ) {
102 2         11 push @$errors => $result->to_json
103             } else {
104 2         7 push @$errors => undef;
105             }
106             }
107             } else {
108 2         9 $json->{$field} = $results->to_json;
109             }
110             }
111              
112 6         16 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             sub has_objects {
122 19     19 1 29 return scalar keys %{$_[0]{objects}};
  19         134  
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 if it
133             is objects, undef otherwise.
134              
135             =cut
136              
137             sub objects {
138 10 0   10 1 30 return (wantarray ? Data::FormValidator::Results::_arrayify($_[0]{objects}{$_[1]}) : $_[0]{objects}{$_[1]})
    50          
139             if (defined $_[1]);
140              
141 10 100       26 wantarray ? keys %{$_[0]{objects}} : $_[0]{objects};
  6         22  
142             }
143              
144             =head1 SEE ALSO
145              
146             =over 4
147              
148             =item *
149              
150             L
151              
152             =back
153              
154             =head1 AUTHOR
155              
156             Todd Wade
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             1;