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
|
|
44465
|
use strict; |
|
109
|
|
|
|
|
224
|
|
|
109
|
|
|
|
|
2634
|
|
6
|
109
|
|
|
109
|
|
491
|
use warnings; |
|
109
|
|
|
|
|
218
|
|
|
109
|
|
|
|
|
2406
|
|
7
|
|
|
|
|
|
|
|
8
|
109
|
|
|
109
|
|
497
|
use base 'Validation::Class::Directive'; |
|
109
|
|
|
|
|
216
|
|
|
109
|
|
|
|
|
8788
|
|
9
|
|
|
|
|
|
|
|
10
|
109
|
|
|
109
|
|
655
|
use Validation::Class::Util; |
|
109
|
|
|
|
|
243
|
|
|
109
|
|
|
|
|
642
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '7.900058'; # 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
|
1078
|
return $_registry; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub filter_alpha { |
39
|
|
|
|
|
|
|
|
40
|
15
|
|
|
15
|
0
|
44
|
$_[0] =~ s/[^A-Za-z]//g; |
41
|
15
|
|
|
|
|
29
|
return $_[0]; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub filter_alphanumeric { |
46
|
|
|
|
|
|
|
|
47
|
25
|
|
|
25
|
0
|
120
|
$_[0] =~ s/[^A-Za-z0-9]//g; |
48
|
25
|
|
|
|
|
66
|
return $_[0]; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub filter_autocase { |
53
|
|
|
|
|
|
|
|
54
|
3
|
|
|
3
|
0
|
14
|
$_[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
|
|
|
|
|
40
|
$_[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
|
11
|
my $n = $_[0] =~ /^(?:[^\d\-]+)?([\-])/ ? 1 : 0; |
70
|
2
|
|
|
|
|
11
|
$_[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
|
20
|
my $n = $_[0] =~ /^(?:[^\d\-]+)?([\-])/ ? 1 : 0; |
78
|
2
|
|
|
|
|
13
|
$_[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
|
55
|
return lc $_[0]; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub filter_numeric { |
90
|
|
|
|
|
|
|
|
91
|
17
|
100
|
|
17
|
0
|
96
|
my $n = $_[0] =~ /^(?:[^\d\-]+)?([\-])/ ? 1 : 0; |
92
|
17
|
|
|
|
|
71
|
$_[0] =~ s/[^0-9]+//g; |
93
|
17
|
100
|
|
|
|
70
|
return $n ? "-$_[0]" : "$_[0]"; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub filter_strip { |
98
|
|
|
|
|
|
|
|
99
|
241
|
|
|
241
|
0
|
710
|
$_[0] =~ s/\s+/ /g; |
100
|
241
|
|
|
|
|
476
|
$_[0] =~ s/^\s+//; |
101
|
241
|
|
|
|
|
450
|
$_[0] =~ s/\s+$//; |
102
|
241
|
|
|
|
|
577
|
return $_[0]; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub filter_titlecase { |
107
|
|
|
|
|
|
|
|
108
|
9
|
|
|
9
|
0
|
35
|
return join( " ", map { ucfirst $_ } (split( /\s/, lc $_[0] )) ); |
|
11
|
|
|
|
|
52
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub filter_trim { |
113
|
|
|
|
|
|
|
|
114
|
241
|
|
|
241
|
0
|
786
|
$_[0] =~ s/^\s+//g; |
115
|
241
|
|
|
|
|
668
|
$_[0] =~ s/\s+$//g; |
116
|
241
|
|
|
|
|
501
|
return $_[0]; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub filter_uppercase { |
121
|
|
|
|
|
|
|
|
122
|
3
|
|
|
3
|
0
|
10
|
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
|
1396
|
my ($self, $proto, $field, $param) = @_; |
137
|
|
|
|
|
|
|
|
138
|
560
|
100
|
|
|
|
1379
|
if ($proto->validated == 2) { |
139
|
329
|
|
|
|
|
1053
|
$self->execute_filtering($proto, $field, $param, 'post'); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
560
|
|
|
|
|
1374
|
return $self; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub before_validation { |
147
|
|
|
|
|
|
|
|
148
|
560
|
|
|
560
|
0
|
1399
|
my ($self, $proto, $field, $param) = @_; |
149
|
|
|
|
|
|
|
|
150
|
560
|
|
|
|
|
1844
|
$self->execute_filtering($proto, $field, $param, 'pre'); |
151
|
|
|
|
|
|
|
|
152
|
560
|
|
|
|
|
1596
|
return $self; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub normalize { |
157
|
|
|
|
|
|
|
|
158
|
1009
|
|
|
1009
|
0
|
2198
|
my ($self, $proto, $field, $param) = @_; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# by default fields should have a filters directive |
161
|
|
|
|
|
|
|
# unless already specified |
162
|
|
|
|
|
|
|
|
163
|
1009
|
100
|
|
|
|
2407
|
if (! defined $field->{filters}) { |
164
|
|
|
|
|
|
|
|
165
|
167
|
|
|
|
|
515
|
$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
|
|
|
|
|
2408
|
$self->execute_filtering($proto, $field, $param, 'pre'); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
1009
|
|
|
|
|
2523
|
return $self; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub execute_filtering { |
183
|
|
|
|
|
|
|
|
184
|
1726
|
|
|
1726
|
0
|
3824
|
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
|
|
|
6600
|
defined $param |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
191
|
|
|
|
|
|
|
; |
192
|
|
|
|
|
|
|
|
193
|
1427
|
|
|
|
|
3152
|
my $filtering = $field->{filtering}; |
194
|
|
|
|
|
|
|
|
195
|
1427
|
50
|
|
|
|
3272
|
$field->{filtering} = $proto->filtering unless defined $field->{filtering}; |
196
|
|
|
|
|
|
|
|
197
|
1427
|
100
|
66
|
|
|
5280
|
if ($field->{filtering} eq $state && $state ne 'off') { |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
my @filters = isa_arrayref($field->{filters}) ? |
200
|
1111
|
100
|
|
|
|
3173
|
@{$field->{filters}} : ($field->{filters}); |
|
1090
|
|
|
|
|
2487
|
|
201
|
|
|
|
|
|
|
|
202
|
1111
|
|
|
|
|
1946
|
my $values = $param; |
203
|
|
|
|
|
|
|
|
204
|
1111
|
100
|
|
|
|
2574
|
foreach my $value (isa_arrayref($param) ? @{$param} : ($param)) { |
|
7
|
|
|
|
|
14
|
|
205
|
|
|
|
|
|
|
|
206
|
1118
|
100
|
|
|
|
2299
|
next if ! $value; |
207
|
|
|
|
|
|
|
|
208
|
1095
|
|
|
|
|
2457
|
foreach my $filter (@filters) { |
209
|
|
|
|
|
|
|
|
210
|
572
|
100
|
|
|
|
1264
|
$filter = $proto->filters->get($filter) |
211
|
|
|
|
|
|
|
unless isa_coderef($filter); |
212
|
|
|
|
|
|
|
|
213
|
572
|
50
|
|
|
|
1221
|
next if ! $filter; |
214
|
|
|
|
|
|
|
|
215
|
572
|
|
|
|
|
1115
|
$value = $filter->($value); |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
1111
|
|
|
|
|
2646
|
my $name = $field->name; |
222
|
|
|
|
|
|
|
|
223
|
1111
|
|
|
|
|
2479
|
$proto->params->add($name, $param); |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
1427
|
|
|
|
|
3312
|
$field->{filtering} = $filtering; |
228
|
|
|
|
|
|
|
|
229
|
1427
|
|
|
|
|
2597
|
return $self; |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
1; |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
__END__ |