line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FormValidator::LazyWay::Message; |
2
|
|
|
|
|
|
|
|
3
|
30
|
|
|
30
|
|
90666
|
use strict; |
|
30
|
|
|
|
|
59
|
|
|
30
|
|
|
|
|
1337
|
|
4
|
30
|
|
|
30
|
|
164
|
use warnings; |
|
30
|
|
|
|
|
54
|
|
|
30
|
|
|
|
|
907
|
|
5
|
30
|
|
|
30
|
|
13937
|
use Data::Dumper; |
|
30
|
|
|
|
|
138260
|
|
|
30
|
|
|
|
|
3652
|
|
6
|
30
|
|
|
30
|
|
243
|
use Carp; |
|
30
|
|
|
|
|
835
|
|
|
30
|
|
|
|
|
8392
|
|
7
|
30
|
|
|
30
|
|
6068
|
use UNIVERSAL::require; |
|
30
|
|
|
|
|
7814
|
|
|
30
|
|
|
|
|
277
|
|
8
|
30
|
|
|
30
|
|
807
|
use base 'FormValidator::LazyWay::Rule'; |
|
30
|
|
|
|
|
85
|
|
|
30
|
|
|
|
|
19190
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/rule config alias lang langs rule_message base_message labels/); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
46
|
|
|
46
|
1
|
821
|
my $class = shift; |
14
|
46
|
|
|
|
|
85
|
my $args; |
15
|
46
|
50
|
|
|
|
215
|
if ( ref $_[0] eq 'HASH' ) { |
16
|
0
|
|
|
|
|
0
|
$args = shift; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
else { |
19
|
46
|
|
|
|
|
475
|
my %args = @_; |
20
|
46
|
|
|
|
|
109
|
$args = \%args; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
46
|
50
|
|
|
|
208
|
croak 'you must set config' unless exists $args->{config}; |
24
|
46
|
50
|
|
|
|
203
|
croak 'you must set rule' unless exists $args->{rule}; |
25
|
|
|
|
|
|
|
|
26
|
46
|
|
|
|
|
175
|
my $self = bless $args, $class; |
27
|
|
|
|
|
|
|
|
28
|
46
|
|
|
|
|
205
|
$self->_set_lang(); |
29
|
46
|
|
|
|
|
226
|
$self->{labels} = $self->_get_label(); |
30
|
46
|
|
|
|
|
205
|
$self->{rule_message} |
31
|
|
|
|
|
|
|
= $self->_finalize_rule_message( $self->_load_rule_message() ); |
32
|
|
|
|
|
|
|
|
33
|
45
|
|
|
|
|
412
|
return $self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _get_label { |
37
|
46
|
|
|
46
|
|
104
|
my $self = shift; |
38
|
46
|
|
100
|
|
|
263
|
my $config = $self->{config}{labels} || {}; |
39
|
46
|
|
|
|
|
102
|
my $labels = {}; |
40
|
46
|
|
|
|
|
93
|
foreach my $lang ( keys %{$config} ) { |
|
46
|
|
|
|
|
189
|
|
41
|
25
|
|
|
|
|
51
|
for my $field ( keys %{ $config->{$lang} } ) { |
|
25
|
|
|
|
|
180
|
|
42
|
51
|
|
|
|
|
244
|
$labels->{$lang}{$field} = $config->{$lang}{$field}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
46
|
|
|
|
|
241
|
return $labels; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _finalize_rule_message { |
49
|
45
|
|
|
45
|
|
83
|
my $self = shift; |
50
|
45
|
|
|
|
|
88
|
my $rule_message = shift; |
51
|
|
|
|
|
|
|
|
52
|
45
|
|
|
|
|
101
|
my $message_storage = {}; |
53
|
45
|
|
|
|
|
101
|
foreach my $lang ( @{ $self->langs } ) { |
|
45
|
|
|
|
|
210
|
|
54
|
46
|
|
|
|
|
249
|
foreach my $level ( keys %{ $self->rule->setting } ) { |
|
46
|
|
|
|
|
168
|
|
55
|
52
|
|
|
|
|
687
|
foreach my $field ( keys %{ $self->rule->setting->{$level} } ) |
|
52
|
|
|
|
|
195
|
|
56
|
|
|
|
|
|
|
{ |
57
|
85
|
|
|
|
|
660
|
for my $item ( |
|
85
|
|
|
|
|
270
|
|
58
|
|
|
|
|
|
|
@{ $self->rule->setting->{$level}{$field} } ) |
59
|
|
|
|
|
|
|
{ |
60
|
98
|
|
|
|
|
6514
|
my $message = $rule_message->{$lang}{ $item->{label} }; |
61
|
98
|
|
|
|
|
144
|
foreach my $key ( keys %{ $item->{args} } ) { |
|
98
|
|
|
|
|
373
|
|
62
|
62
|
|
|
|
|
158
|
my $regexp = '\$_\[' . $key . '\]'; |
63
|
62
|
|
|
|
|
237
|
my $value = $item->{args}{$key}; |
64
|
62
|
|
|
|
|
1036
|
$message =~ s/$regexp/$value/g; |
65
|
|
|
|
|
|
|
} |
66
|
98
|
|
|
|
|
883
|
$message_storage->{$lang}{$level}{$field} |
67
|
|
|
|
|
|
|
{ $item->{label} } = $message; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
45
|
|
|
|
|
193
|
return $message_storage; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub get { |
76
|
16
|
|
|
16
|
1
|
177
|
my $self = shift; |
77
|
16
|
|
|
|
|
26
|
my $params = shift; |
78
|
16
|
|
|
|
|
76
|
return $self->rule_message->{ $params->{lang} }{ $params->{level} } |
79
|
|
|
|
|
|
|
{ $params->{field} }{ $params->{label} }; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub _set_lang { |
83
|
47
|
|
|
47
|
|
81
|
my $self = shift; |
84
|
47
|
|
100
|
|
|
221
|
$self->{lang} = $self->config->{lang} || 'en'; |
85
|
47
|
|
100
|
|
|
779
|
$self->{langs} = $self->config->{langs} || [ $self->lang ]; |
86
|
|
|
|
|
|
|
|
87
|
47
|
|
|
|
|
776
|
for my $lang ( @{ $self->{langs} } ) { |
|
47
|
|
|
|
|
152
|
|
88
|
50
|
|
|
|
|
302
|
my $pkg = __PACKAGE__ . '::' . uc $lang; |
89
|
50
|
|
|
|
|
493
|
$pkg->require; |
90
|
50
|
|
66
|
|
|
1043
|
$self->{base_message}{$lang} = { |
|
|
|
66
|
|
|
|
|
91
|
|
|
|
|
|
|
invalid => $self->config->{messages}{$lang}{invalid} |
92
|
|
|
|
|
|
|
|| $pkg->invalid(), |
93
|
|
|
|
|
|
|
missing => $self->config->{messages}{$lang}{missing} |
94
|
|
|
|
|
|
|
|| $pkg->missing(), |
95
|
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _load_rule_message { |
100
|
46
|
|
|
46
|
|
90
|
my $self = shift; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my @subs = ( |
103
|
82
|
|
|
82
|
|
767
|
sub { $self->_load_from_config(@_) }, |
104
|
71
|
|
|
71
|
|
661
|
sub { $self->_load_from_rule(@_) }, |
105
|
1
|
|
|
1
|
|
10
|
sub { $self->_loading_error(@_) }, |
106
|
46
|
|
|
|
|
503
|
); |
107
|
|
|
|
|
|
|
|
108
|
46
|
|
|
|
|
157
|
my %message = (); |
109
|
46
|
|
|
|
|
221
|
LANG: |
110
|
46
|
|
|
|
|
89
|
for my $lang ( @{ $self->langs } ) { |
111
|
48
|
|
|
|
|
410
|
$self->_loading_modules($lang); |
112
|
48
|
|
|
|
|
208
|
LABEL: |
113
|
48
|
|
|
|
|
9695
|
foreach my $label ( keys %{ $self->rule->labels } ) { |
114
|
|
|
|
|
|
|
MESSAGE: |
115
|
82
|
|
|
|
|
741
|
for my $sub (@subs) { |
116
|
154
|
|
|
|
|
447
|
my $message |
117
|
|
|
|
|
|
|
= $sub->( $lang, $label, $self->rule->labels->{$label} ); |
118
|
153
|
|
|
|
|
445
|
$message{$lang}{$label} = $message; |
119
|
153
|
100
|
|
|
|
592
|
last MESSAGE if $message; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
45
|
|
|
|
|
493
|
return \%message; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub _loading_modules { |
127
|
48
|
|
|
48
|
|
96
|
my $self = shift; |
128
|
48
|
|
|
|
|
135
|
my $lang = shift; |
129
|
48
|
|
|
|
|
90
|
for my $module ( @{ $self->rule->modules } ) { |
|
48
|
|
|
|
|
195
|
|
130
|
87
|
|
|
|
|
1833
|
my $package = $module . '::' . uc $lang; |
131
|
87
|
|
|
|
|
819
|
$package->require; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub _loading_error { |
136
|
1
|
|
|
1
|
|
1
|
my $self = shift; |
137
|
1
|
|
|
|
|
2
|
my $lang = shift; |
138
|
1
|
|
|
|
|
2
|
my $label = shift; |
139
|
1
|
|
|
|
|
33
|
croak sprintf( "You need to have rule message for lang:%s label:%s", |
140
|
|
|
|
|
|
|
$lang, $label ); |
141
|
0
|
|
|
|
|
0
|
return; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub _load_from_config { |
145
|
82
|
|
|
82
|
|
145
|
my $self = shift; |
146
|
82
|
|
|
|
|
144
|
my $lang = shift; |
147
|
82
|
|
|
|
|
132
|
my $label = shift; |
148
|
|
|
|
|
|
|
|
149
|
82
|
|
|
|
|
244
|
my $config = $self->config->{messages}; |
150
|
82
|
100
|
|
|
|
791
|
return $config->{$lang}{rule}{$label} if $config->{$lang}{rule}{$label}; |
151
|
|
|
|
|
|
|
|
152
|
74
|
|
|
|
|
304
|
my $alias = $self->rule->labels->{$label}{alias}; |
153
|
74
|
100
|
|
|
|
714
|
return unless $alias; |
154
|
4
|
100
|
|
|
|
37
|
return $config->{$lang}{rule}{$alias} if $config->{$lang}{rule}{$alias}; |
155
|
|
|
|
|
|
|
|
156
|
1
|
|
|
|
|
3
|
return; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub _load_from_rule { |
161
|
71
|
|
|
71
|
|
113
|
my $self = shift; |
162
|
71
|
|
|
|
|
528
|
my $lang = shift; |
163
|
71
|
|
|
|
|
113
|
my $label = shift; |
164
|
71
|
|
|
|
|
92
|
my $package_info = shift; |
165
|
71
|
|
|
|
|
252
|
my $package = $package_info->{package} . '::' . uc $lang; |
166
|
71
|
|
|
|
|
315
|
my $method = $package . '::' . $package_info->{method}; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
71
|
|
|
|
|
123
|
my $message; |
170
|
30
|
|
|
30
|
|
19867
|
no strict 'refs'; |
|
30
|
|
|
|
|
73
|
|
|
30
|
|
|
|
|
2401
|
|
171
|
71
|
|
|
|
|
124
|
eval { $message = $method->(); }; |
|
71
|
|
|
|
|
653
|
|
172
|
|
|
|
|
|
|
|
173
|
71
|
|
|
|
|
198
|
return $message; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
1; |