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
|
109
|
|
|
109
|
|
55286
|
use strict; |
|
109
|
|
|
|
|
282
|
|
|
109
|
|
|
|
|
3201
|
|
6
|
109
|
|
|
109
|
|
627
|
use warnings; |
|
109
|
|
|
|
|
261
|
|
|
109
|
|
|
|
|
2928
|
|
7
|
|
|
|
|
|
|
|
8
|
109
|
|
|
109
|
|
573
|
use base 'Validation::Class::Directive'; |
|
109
|
|
|
|
|
281
|
|
|
109
|
|
|
|
|
10777
|
|
9
|
|
|
|
|
|
|
|
10
|
109
|
|
|
109
|
|
831
|
use Validation::Class::Util; |
|
109
|
|
|
|
|
315
|
|
|
109
|
|
|
|
|
834
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '7.900059'; # 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
|
320
|
|
|
320
|
0
|
1289
|
return $_registry; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub filter_alpha { |
39
|
|
|
|
|
|
|
|
40
|
15
|
|
|
15
|
0
|
58
|
$_[0] =~ s/[^A-Za-z]//g; |
41
|
15
|
|
|
|
|
38
|
return $_[0]; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub filter_alphanumeric { |
46
|
|
|
|
|
|
|
|
47
|
25
|
|
|
25
|
0
|
132
|
$_[0] =~ s/[^A-Za-z0-9]//g; |
48
|
25
|
|
|
|
|
84
|
return $_[0]; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub filter_autocase { |
53
|
|
|
|
|
|
|
|
54
|
3
|
|
|
3
|
0
|
18
|
$_[0] =~ s/(^[a-z]|\b[a-z])/\u$1/g; |
55
|
3
|
|
|
|
|
7
|
return $_[0]; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub filter_capitalize { |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
1
|
0
|
4
|
$_[0] = ucfirst $_[0]; |
62
|
1
|
|
|
|
|
25
|
$_[0] =~ s/\.\s+([a-z])/\. \U$1/g; |
63
|
1
|
|
|
|
|
5
|
return $_[0]; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub filter_currency { |
68
|
|
|
|
|
|
|
|
69
|
2
|
100
|
|
2
|
0
|
15
|
my $n = $_[0] =~ /^(?:[^\d\-]+)?([\-])/ ? 1 : 0; |
70
|
2
|
|
|
|
|
23
|
$_[0] =~ s/[^0-9\.\,]+//g; |
71
|
2
|
100
|
|
|
|
12
|
return $n ? "-$_[0]" : "$_[0]"; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub filter_decimal { |
76
|
|
|
|
|
|
|
|
77
|
2
|
100
|
|
2
|
0
|
24
|
my $n = $_[0] =~ /^(?:[^\d\-]+)?([\-])/ ? 1 : 0; |
78
|
2
|
|
|
|
|
14
|
$_[0] =~ s/[^0-9\.]+//g; |
79
|
2
|
100
|
|
|
|
35
|
return $n ? "-$_[0]" : "$_[0]"; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub filter_lowercase { |
84
|
|
|
|
|
|
|
|
85
|
17
|
|
|
17
|
0
|
77
|
return lc $_[0]; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub filter_numeric { |
90
|
|
|
|
|
|
|
|
91
|
17
|
100
|
|
17
|
0
|
102
|
my $n = $_[0] =~ /^(?:[^\d\-]+)?([\-])/ ? 1 : 0; |
92
|
17
|
|
|
|
|
77
|
$_[0] =~ s/[^0-9]+//g; |
93
|
17
|
100
|
|
|
|
92
|
return $n ? "-$_[0]" : "$_[0]"; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub filter_strip { |
98
|
|
|
|
|
|
|
|
99
|
241
|
|
|
241
|
0
|
787
|
$_[0] =~ s/\s+/ /g; |
100
|
241
|
|
|
|
|
514
|
$_[0] =~ s/^\s+//; |
101
|
241
|
|
|
|
|
509
|
$_[0] =~ s/\s+$//; |
102
|
241
|
|
|
|
|
623
|
return $_[0]; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub filter_titlecase { |
107
|
|
|
|
|
|
|
|
108
|
9
|
|
|
9
|
0
|
66
|
return join( " ", map { ucfirst $_ } (split( /\s/, lc $_[0] )) ); |
|
11
|
|
|
|
|
66
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub filter_trim { |
113
|
|
|
|
|
|
|
|
114
|
241
|
|
|
241
|
0
|
799
|
$_[0] =~ s/^\s+//g; |
115
|
241
|
|
|
|
|
715
|
$_[0] =~ s/\s+$//g; |
116
|
241
|
|
|
|
|
623
|
return $_[0]; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub filter_uppercase { |
121
|
|
|
|
|
|
|
|
122
|
3
|
|
|
3
|
0
|
30
|
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
|
560
|
|
|
560
|
0
|
1646
|
my ($self, $proto, $field, $param) = @_; |
137
|
|
|
|
|
|
|
|
138
|
560
|
100
|
|
|
|
1598
|
if ($proto->validated == 2) { |
139
|
329
|
|
|
|
|
1129
|
$self->execute_filtering($proto, $field, $param, 'post'); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
560
|
|
|
|
|
1603
|
return $self; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub before_validation { |
147
|
|
|
|
|
|
|
|
148
|
560
|
|
|
560
|
0
|
1691
|
my ($self, $proto, $field, $param) = @_; |
149
|
|
|
|
|
|
|
|
150
|
560
|
|
|
|
|
1949
|
$self->execute_filtering($proto, $field, $param, 'pre'); |
151
|
|
|
|
|
|
|
|
152
|
560
|
|
|
|
|
1955
|
return $self; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub normalize { |
157
|
|
|
|
|
|
|
|
158
|
1009
|
|
|
1009
|
0
|
2513
|
my ($self, $proto, $field, $param) = @_; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# by default fields should have a filters directive |
161
|
|
|
|
|
|
|
# unless already specified |
162
|
|
|
|
|
|
|
|
163
|
1009
|
100
|
|
|
|
2716
|
if (! defined $field->{filters}) { |
164
|
|
|
|
|
|
|
|
165
|
167
|
|
|
|
|
571
|
$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
|
842
|
|
|
|
|
2499
|
$self->execute_filtering($proto, $field, $param, 'pre'); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
1009
|
|
|
|
|
2983
|
return $self; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub execute_filtering { |
183
|
|
|
|
|
|
|
|
184
|
1726
|
|
|
1726
|
0
|
4040
|
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
|
|
|
6557
|
defined $param |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
191
|
|
|
|
|
|
|
; |
192
|
|
|
|
|
|
|
|
193
|
1427
|
|
|
|
|
3309
|
my $filtering = $field->{filtering}; |
194
|
|
|
|
|
|
|
|
195
|
1427
|
50
|
|
|
|
3349
|
$field->{filtering} = $proto->filtering unless defined $field->{filtering}; |
196
|
|
|
|
|
|
|
|
197
|
1427
|
100
|
66
|
|
|
5412
|
if ($field->{filtering} eq $state && $state ne 'off') { |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
my @filters = isa_arrayref($field->{filters}) ? |
200
|
1111
|
100
|
|
|
|
3321
|
@{$field->{filters}} : ($field->{filters}); |
|
1090
|
|
|
|
|
2710
|
|
201
|
|
|
|
|
|
|
|
202
|
1111
|
|
|
|
|
2257
|
my $values = $param; |
203
|
|
|
|
|
|
|
|
204
|
1111
|
100
|
|
|
|
2737
|
foreach my $value (isa_arrayref($param) ? @{$param} : ($param)) { |
|
7
|
|
|
|
|
19
|
|
205
|
|
|
|
|
|
|
|
206
|
1118
|
100
|
|
|
|
2593
|
next if ! $value; |
207
|
|
|
|
|
|
|
|
208
|
1095
|
|
|
|
|
2550
|
foreach my $filter (@filters) { |
209
|
|
|
|
|
|
|
|
210
|
572
|
100
|
|
|
|
1612
|
$filter = $proto->filters->get($filter) |
211
|
|
|
|
|
|
|
unless isa_coderef($filter); |
212
|
|
|
|
|
|
|
|
213
|
572
|
50
|
|
|
|
1374
|
next if ! $filter; |
214
|
|
|
|
|
|
|
|
215
|
572
|
|
|
|
|
1268
|
$value = $filter->($value); |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
1111
|
|
|
|
|
2837
|
my $name = $field->name; |
222
|
|
|
|
|
|
|
|
223
|
1111
|
|
|
|
|
2794
|
$proto->params->add($name, $param); |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
1427
|
|
|
|
|
3695
|
$field->{filtering} = $filtering; |
228
|
|
|
|
|
|
|
|
229
|
1427
|
|
|
|
|
2922
|
return $self; |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
1; |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
__END__ |