line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: Filters Directive for Validation Class Field Definitions |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Validation::Class::Directive::Filters; |
4
|
|
|
|
|
|
|
|
5
|
108
|
|
|
108
|
|
71786
|
use strict; |
|
108
|
|
|
|
|
251
|
|
|
108
|
|
|
|
|
2887
|
|
6
|
108
|
|
|
108
|
|
542
|
use warnings; |
|
108
|
|
|
|
|
274
|
|
|
108
|
|
|
|
|
2863
|
|
7
|
|
|
|
|
|
|
|
8
|
108
|
|
|
108
|
|
529
|
use base 'Validation::Class::Directive'; |
|
108
|
|
|
|
|
194
|
|
|
108
|
|
|
|
|
7560
|
|
9
|
|
|
|
|
|
|
|
10
|
108
|
|
|
108
|
|
583
|
use Validation::Class::Util; |
|
108
|
|
|
|
|
282
|
|
|
108
|
|
|
|
|
780
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '7.900057'; # VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $_registry = { |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
alpha => \&filter_alpha, |
17
|
|
|
|
|
|
|
alphanumeric => \&filter_alphanumeric, |
18
|
|
|
|
|
|
|
autocase => \&filter_autocase, |
19
|
|
|
|
|
|
|
capitalize => \&filter_capitalize, |
20
|
|
|
|
|
|
|
currency => \&filter_currency, |
21
|
|
|
|
|
|
|
decimal => \&filter_decimal, |
22
|
|
|
|
|
|
|
lowercase => \&filter_lowercase, |
23
|
|
|
|
|
|
|
numeric => \&filter_numeric, |
24
|
|
|
|
|
|
|
strip => \&filter_strip, |
25
|
|
|
|
|
|
|
titlecase => \&filter_titlecase, |
26
|
|
|
|
|
|
|
trim => \&filter_trim, |
27
|
|
|
|
|
|
|
uppercase => \&filter_uppercase |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
}; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub registry { |
33
|
|
|
|
|
|
|
|
34
|
316
|
|
|
316
|
0
|
1227
|
return $_registry; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub filter_alpha { |
39
|
|
|
|
|
|
|
|
40
|
15
|
|
|
15
|
0
|
53
|
$_[0] =~ s/[^A-Za-z]//g; |
41
|
15
|
|
|
|
|
41
|
return $_[0]; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub filter_alphanumeric { |
46
|
|
|
|
|
|
|
|
47
|
25
|
|
|
25
|
0
|
121
|
$_[0] =~ s/[^A-Za-z0-9]//g; |
48
|
25
|
|
|
|
|
82
|
return $_[0]; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub filter_autocase { |
53
|
|
|
|
|
|
|
|
54
|
3
|
|
|
3
|
0
|
19
|
$_[0] =~ s/(^[a-z]|\b[a-z])/\u$1/g; |
55
|
3
|
|
|
|
|
9
|
return $_[0]; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub filter_capitalize { |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
1
|
0
|
8
|
$_[0] = ucfirst $_[0]; |
62
|
1
|
|
|
|
|
17
|
$_[0] =~ s/\.\s+([a-z])/\. \U$1/g; |
63
|
1
|
|
|
|
|
10
|
return $_[0]; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub filter_currency { |
68
|
|
|
|
|
|
|
|
69
|
2
|
100
|
|
2
|
0
|
10
|
my $n = $_[0] =~ /^(?:[^\d\-]+)?([\-])/ ? 1 : 0; |
70
|
2
|
|
|
|
|
12
|
$_[0] =~ s/[^0-9\.\,]+//g; |
71
|
2
|
100
|
|
|
|
10
|
return $n ? "-$_[0]" : "$_[0]"; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub filter_decimal { |
76
|
|
|
|
|
|
|
|
77
|
2
|
100
|
|
2
|
0
|
11
|
my $n = $_[0] =~ /^(?:[^\d\-]+)?([\-])/ ? 1 : 0; |
78
|
2
|
|
|
|
|
12
|
$_[0] =~ s/[^0-9\.]+//g; |
79
|
2
|
100
|
|
|
|
11
|
return $n ? "-$_[0]" : "$_[0]"; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub filter_lowercase { |
84
|
|
|
|
|
|
|
|
85
|
17
|
|
|
17
|
0
|
61
|
return lc $_[0]; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub filter_numeric { |
90
|
|
|
|
|
|
|
|
91
|
17
|
100
|
|
17
|
0
|
71
|
my $n = $_[0] =~ /^(?:[^\d\-]+)?([\-])/ ? 1 : 0; |
92
|
17
|
|
|
|
|
63
|
$_[0] =~ s/[^0-9]+//g; |
93
|
17
|
100
|
|
|
|
77
|
return $n ? "-$_[0]" : "$_[0]"; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub filter_strip { |
98
|
|
|
|
|
|
|
|
99
|
241
|
|
|
241
|
0
|
671
|
$_[0] =~ s/\s+/ /g; |
100
|
241
|
|
|
|
|
464
|
$_[0] =~ s/^\s+//; |
101
|
241
|
|
|
|
|
442
|
$_[0] =~ s/\s+$//; |
102
|
241
|
|
|
|
|
674
|
return $_[0]; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub filter_titlecase { |
107
|
|
|
|
|
|
|
|
108
|
9
|
|
|
9
|
0
|
50
|
return join( " ", map { ucfirst $_ } (split( /\s/, lc $_[0] )) ); |
|
11
|
|
|
|
|
68
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub filter_trim { |
113
|
|
|
|
|
|
|
|
114
|
241
|
|
|
241
|
0
|
632
|
$_[0] =~ s/^\s+//g; |
115
|
241
|
|
|
|
|
600
|
$_[0] =~ s/\s+$//g; |
116
|
241
|
|
|
|
|
576
|
return $_[0]; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub filter_uppercase { |
121
|
|
|
|
|
|
|
|
122
|
3
|
|
|
3
|
0
|
12
|
return uc $_[0]; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
has 'mixin' => 1; |
127
|
|
|
|
|
|
|
has 'field' => 1; |
128
|
|
|
|
|
|
|
has 'multi' => 1; |
129
|
|
|
|
|
|
|
has 'dependencies' => sub {{ |
130
|
|
|
|
|
|
|
normalization => ['filtering'], |
131
|
|
|
|
|
|
|
validation => [] |
132
|
|
|
|
|
|
|
}}; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub after_validation { |
135
|
|
|
|
|
|
|
|
136
|
555
|
|
|
555
|
0
|
1141
|
my ($self, $proto, $field, $param) = @_; |
137
|
|
|
|
|
|
|
|
138
|
555
|
100
|
|
|
|
1750
|
if ($proto->validated == 2) { |
139
|
325
|
|
|
|
|
1652
|
$self->execute_filtering($proto, $field, $param, 'post'); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
555
|
|
|
|
|
1791
|
return $self; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub before_validation { |
147
|
|
|
|
|
|
|
|
148
|
555
|
|
|
555
|
0
|
1111
|
my ($self, $proto, $field, $param) = @_; |
149
|
|
|
|
|
|
|
|
150
|
555
|
|
|
|
|
1493
|
$self->execute_filtering($proto, $field, $param, 'pre'); |
151
|
|
|
|
|
|
|
|
152
|
555
|
|
|
|
|
1836
|
return $self; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub normalize { |
157
|
|
|
|
|
|
|
|
158
|
1002
|
|
|
1002
|
0
|
1840
|
my ($self, $proto, $field, $param) = @_; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# by default fields should have a filters directive |
161
|
|
|
|
|
|
|
# unless already specified |
162
|
|
|
|
|
|
|
|
163
|
1002
|
100
|
|
|
|
2640
|
if (! defined $field->{filters}) { |
164
|
|
|
|
|
|
|
|
165
|
151
|
|
|
|
|
481
|
$field->{filters} = []; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# run any existing filters on instantiation |
170
|
|
|
|
|
|
|
# if the field is set to pre-filter |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
else { |
173
|
|
|
|
|
|
|
|
174
|
851
|
|
|
|
|
2341
|
$self->execute_filtering($proto, $field, $param, 'pre'); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
1002
|
|
|
|
|
3276
|
return $self; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub execute_filtering { |
183
|
|
|
|
|
|
|
|
184
|
1726
|
|
|
1726
|
0
|
3085
|
my ($self, $proto, $field, $param, $state) = @_; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
return unless $state && |
187
|
|
|
|
|
|
|
($proto->filtering eq 'pre' || $proto->filtering eq 'post') && |
188
|
|
|
|
|
|
|
defined $field->{filters} && |
189
|
|
|
|
|
|
|
defined $field->{filtering} && |
190
|
1726
|
100
|
100
|
|
|
7912
|
defined $param |
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
191
|
|
|
|
|
|
|
; |
192
|
|
|
|
|
|
|
|
193
|
1424
|
|
|
|
|
2646
|
my $filtering = $field->{filtering}; |
194
|
|
|
|
|
|
|
|
195
|
1424
|
50
|
|
|
|
3431
|
$field->{filtering} = $proto->filtering unless defined $field->{filtering}; |
196
|
|
|
|
|
|
|
|
197
|
1424
|
100
|
66
|
|
|
6335
|
if ($field->{filtering} eq $state && $state ne 'off') { |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
my @filters = isa_arrayref($field->{filters}) ? |
200
|
1110
|
100
|
|
|
|
3450
|
@{$field->{filters}} : ($field->{filters}); |
|
1089
|
|
|
|
|
2628
|
|
201
|
|
|
|
|
|
|
|
202
|
1110
|
|
|
|
|
1780
|
my $values = $param; |
203
|
|
|
|
|
|
|
|
204
|
1110
|
100
|
|
|
|
3057
|
foreach my $value (isa_arrayref($param) ? @{$param} : ($param)) { |
|
7
|
|
|
|
|
16
|
|
205
|
|
|
|
|
|
|
|
206
|
1117
|
100
|
|
|
|
2610
|
next if ! $value; |
207
|
|
|
|
|
|
|
|
208
|
1094
|
|
|
|
|
2361
|
foreach my $filter (@filters) { |
209
|
|
|
|
|
|
|
|
210
|
572
|
100
|
|
|
|
1521
|
$filter = $proto->filters->get($filter) |
211
|
|
|
|
|
|
|
unless isa_coderef($filter); |
212
|
|
|
|
|
|
|
|
213
|
572
|
50
|
|
|
|
1439
|
next if ! $filter; |
214
|
|
|
|
|
|
|
|
215
|
572
|
|
|
|
|
1178
|
$value = $filter->($value); |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
1110
|
|
|
|
|
3306
|
my $name = $field->name; |
222
|
|
|
|
|
|
|
|
223
|
1110
|
|
|
|
|
3301
|
$proto->params->add($name, $param); |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
1424
|
|
|
|
|
2979
|
$field->{filtering} = $filtering; |
228
|
|
|
|
|
|
|
|
229
|
1424
|
|
|
|
|
2717
|
return $self; |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
1; |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
__END__ |