File Coverage

blib/lib/Dancer2/Plugin/FormValidator/Factory/Messages.pm
Criterion Covered Total %
statement 40 41 97.5
branch 4 6 66.6
condition 4 6 66.6
subroutine 9 9 100.0
pod 0 1 0.0
total 57 63 90.4


line stmt bran cond sub pod time code
1              
2             use strict;
3 16     16   549 use warnings;
  16         32  
  16         431  
4 16     16   75  
  16         30  
  16         374  
5             use Moo;
6 16     16   73 use Carp;
  16         30  
  16         87  
7 16     16   4777 use Hash::MultiValue;
  16         47  
  16         928  
8 16     16   2946 use Types::Standard qw(InstanceOf ConsumerOf);
  16         14188  
  16         505  
9 16     16   108 use namespace::clean;
  16         49  
  16         143  
10 16     16   9563  
  16         35  
  16         99  
11             has config => (
12             is => 'ro',
13             isa => InstanceOf['Dancer2::Plugin::FormValidator::Config'],
14             required => 1,
15             );
16              
17             has registry => (
18             is => 'ro',
19             isa => InstanceOf['Dancer2::Plugin::FormValidator::Registry'],
20             required => 1,
21             );
22              
23             # Generates messages for each invalid field.
24             my ($self, $invalid, $profile_messages) = @_;
25              
26 14     14 0 22213 my $messages = Hash::MultiValue->new;
27             my $config = $self->config;
28 14         205 my $ucfirst = $config->ucfirst;
29 14         740 my $language = $config->language;
30 14         344  
31 14         756 for my $item (@{ $invalid }) {
32             my ($field, $name, $params) = @$item;
33 14         436  
  14         54  
34 23         514 my $validator = $self->registry->get($name);
35             my $message = $self->config->messages_validators->{$name} || $validator->message;
36 23         162  
37 23   66     242 if (defined $profile_messages) {
38             if (ref $profile_messages ne 'HASH') {
39 23 100       112 Carp::croak("Messages should be a HashRef\n")
40 6 50       19 }
41 0         0  
42             $message = $profile_messages->{$field}->{$name} || $message;
43             }
44 6   66     27  
45             # Create and add message.
46             {
47             # Cause we need this feature.
48             no warnings 'redundant';
49              
50 16     16   7366 $messages->add(
  16         287  
  16         1945  
  23         60  
51             $field,
52             sprintf(
53             $message->{$language},
54             $ucfirst ? ucfirst($field) : $field,
55 23 50       262 split(',', $params),
56             )
57             );
58             }
59             }
60              
61             return $messages->as_hashref_multi;
62             }
63 14         555  
64             1;