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   645 use warnings;
  16         34  
  16         443  
4 16     16   76  
  16         35  
  16         381  
5             use Moo;
6 16     16   77 use Carp;
  16         36  
  16         83  
7 16     16   4691 use Hash::MultiValue;
  16         55  
  16         965  
8 16     16   2959 use Types::Standard qw(InstanceOf ConsumerOf);
  16         14313  
  16         607  
9 16     16   108 use namespace::clean;
  16         45  
  16         148  
10 16     16   9578  
  16         38  
  16         106  
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 23656 my $messages = Hash::MultiValue->new;
27             my $config = $self->config;
28 14         281 my $ucfirst = $config->ucfirst;
29 14         779 my $language = $config->language;
30 14         347  
31 14         771 for my $item (@{ $invalid }) {
32             my ($field, $name, $params) = @$item;
33 14         419  
  14         62  
34 23         484 my $validator = $self->registry->get($name);
35             my $message = $self->config->messages_validators->{$name} || $validator->message;
36 23         168  
37 23   66     262 if (defined $profile_messages) {
38             if (ref $profile_messages ne 'HASH') {
39 23 100       108 Carp::croak("Messages should be a HashRef\n")
40 6 50       15 }
41 0         0  
42             $message = $profile_messages->{$field}->{$name} || $message;
43             }
44 6   66     24  
45             # Create and add message.
46             {
47             # Cause we need this feature.
48             no warnings 'redundant';
49              
50 16     16   7799 $messages->add(
  16         251  
  16         2037  
  23         57  
51             $field,
52             sprintf(
53             $message->{$language},
54             $ucfirst ? ucfirst($field) : $field,
55 23 50       321 split(',', $params),
56             )
57             );
58             }
59             }
60              
61             return $messages->as_hashref_multi;
62             }
63 14         622  
64             1;