File Coverage

lib/Data/Validation/Filters.pm
Criterion Covered Total %
statement 50 50 100.0
branch 6 8 75.0
condition 2 3 66.6
subroutine 16 16 100.0
pod 11 11 100.0
total 85 88 96.5


line stmt bran cond sub pod time code
1             package Data::Validation::Filters;
2              
3 1     1   5 use namespace::autoclean;
  1         1  
  1         7  
4              
5 1     1   65 use Data::Validation::Constants qw( EXCEPTION_CLASS HASH TRUE );
  1         1  
  1         8  
6 1     1   246 use Data::Validation::Utils qw( load_class );
  1         1  
  1         6  
7 1     1   123 use Unexpected::Types qw( Str );
  1         1  
  1         8  
8 1     1   325 use Moo;
  1         2  
  1         5  
9              
10             has 'method' => is => 'ro', isa => Str, required => TRUE;
11              
12             has 'pattern' => is => 'ro', isa => Str;
13              
14             has 'replace' => is => 'ro', isa => Str;
15              
16             sub new_from_method {
17 15     15 1 17 my ($class, $attr) = @_;
18              
19 15 100       252 $class->can( $attr->{method} ) and return $class->new( $attr );
20              
21 1         6 return (load_class $class, 'filter', $attr->{method})->new( $attr );
22             }
23              
24             sub filter {
25             my ($self, $v) = @_; my $method = $self->method; return $self->$method( $v );
26             }
27              
28             around 'filter' => sub {
29             my ($orig, $self, $v) = @_; return defined $v ? $orig->( $self, $v ) : undef;
30             };
31              
32             # Builtin filter methods
33             sub filterEscapeHTML {
34 1     1 1 1 my ($self, $v) = @_;
35              
36 1         7 $v =~ s{ &(?!(amp|lt|gt|quot);) }{&}gmx;
37 1         4 $v =~ s{ < }{&lt;}gmx;
38 1         4 $v =~ s{ > }{&gt;}gmx;
39 1         3 $v =~ s{ \" }{&quot;}gmx;
40 1         10 return $v;
41             }
42              
43             sub filterLowerCase {
44 1     1 1 2 my ($self, $v) = @_; return lc $v;
  1         5  
45             }
46              
47             sub filterNonNumeric {
48 2     2 1 4 my ($self, $v) = @_; $v =~ s{ \D+ }{}gmx; return $v;
  2         9  
  2         8  
49             }
50              
51             sub filterReplaceRegex {
52 1     1 1 3 my ($self, $v) = @_;
53              
54 1 50       7 my $pattern = $self->pattern or return $v;
55 1 50       8 my $replace = defined $self->replace ? $self->replace : q();
56              
57 1         13 $v =~ s{ $pattern }{$replace}gmx;
58 1         5 return $v;
59             }
60              
61             sub filterTitleCase {
62 1     1 1 2 my ($self, $v) = @_; my @words = split ' ', $v, -1;
  1         4  
63              
64 1         2 return join ' ', map { ucfirst $_ } @words;
  2         9  
65             }
66              
67             sub filterTrimBoth {
68 2     2 1 4 my ($self, $v) = @_;
69              
70 2         6 $v =~ s{ \A \s+ }{}mx; $v =~ s{ \s+ \z }{}mx;
  2         7  
71 2         9 return $v;
72             }
73              
74             sub filterUpperCase {
75 2     2 1 2 my ($self, $v) = @_; return uc $v;
  2         10  
76             }
77              
78             sub filterUCFirst {
79 1     1 1 2 my ($self, $v) = @_; return ucfirst $v;
  1         8  
80             }
81              
82             sub filterWhiteSpace {
83 1     1 1 2 my ($self, $v) = @_; $v =~ s{ \s+ }{}gmx; return $v;
  1         7  
  1         6  
84             }
85              
86             sub filterZeroLength {
87 2 100 66 2 1 16 return defined $_[ 1 ] && length $_[ 1 ] ? $_[ 1 ] : undef;
88             }
89              
90             1;
91              
92             __END__
93              
94             =pod
95              
96             =encoding utf-8
97              
98             =head1 Name
99              
100             Data::Validation::Filters - Filter data values
101              
102             =head1 Synopsis
103              
104             use Data::Validation::Filters;
105              
106             %config = ( method => $method, %{ $self->filters->{ $id } || {} } );
107              
108             $filter_ref = Data::Validation::Filters->new_from_method( %config );
109              
110             $value = $filter_ref->filter_value( $value );
111              
112             =head1 Description
113              
114             Applies a single filter to a data value and returns it's possibly changed
115             value
116              
117             =head1 Configuration and Environment
118              
119             Defines the following attributes:
120              
121             =over 3
122              
123             =item C<method>
124              
125             Name of the constraint to apply. Required
126              
127             =item C<pattern>
128              
129             Used by L</isMathchingRegex> as the pattern to match the supplied value
130             against
131              
132             =item C<replace>
133              
134             The replacement value used in regular expression search and replace
135             operations
136              
137             =back
138              
139             =head1 Subroutines/Methods
140              
141             =head2 C<new_from_method>
142              
143             A class method which implements a factory pattern using the C<method> attribute
144             to select the subclass
145              
146             =head2 C<filter>
147              
148             Calls either a builtin method or an external one to filter the data value
149              
150             =head2 C<filterEscapeHTML>
151              
152             Replaces &<>" with their &xxx; equivalents
153              
154             =head2 C<filterLowerCase>
155              
156             Lower cases the data value
157              
158             =head2 C<filterNonNumeric>
159              
160             Removes all non numeric characters
161              
162             =head2 C<filterReplaceRegex>
163              
164             Matches the regular expression pattern and substitutes the replace string
165              
166             =head2 C<filterTitleCase>
167              
168             Like L</filterUCFirst> but applied to every word in the string
169              
170             =head2 C<filterTrimBoth>
171              
172             Remove all leading and trailing whitespace
173              
174             =head2 C<filterUpperCase>
175              
176             Upper cases the data value
177              
178             =head2 C<filterUCFirst>
179              
180             Upper cases the first character of the data value
181              
182             =head2 C<filterWhiteSpace>
183              
184             Removes all whitespace
185              
186             =head2 C<filterZeroLength>
187              
188             Returns undef if value is zero length
189              
190             =head1 Diagnostics
191              
192             None
193              
194             =head1 Dependencies
195              
196             =over 3
197              
198             =item L<Moo>
199              
200             =item L<Unexpected>
201              
202             =back
203              
204             =head1 Incompatibilities
205              
206             There are no known incompatibilities in this module
207              
208             =head1 Bugs and Limitations
209              
210             There are no known bugs in this module. Please report problems to
211             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Validation. Patches are welcome
212              
213             =head1 Acknowledgements
214              
215             Larry Wall - For the Perl programming language
216              
217             =head1 Author
218              
219             Peter Flanigan, C<< <pjfl@cpan.org> >>
220              
221             =head1 License and Copyright
222              
223             Copyright (c) 2016 Peter Flanigan. All rights reserved
224              
225             This program is free software; you can redistribute it and/or modify it
226             under the same terms as Perl itself. See L<perlartistic>
227              
228             This program is distributed in the hope that it will be useful,
229             but WITHOUT WARRANTY; without even the implied warranty of
230             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
231              
232             =cut
233              
234             # Local Variables:
235             # mode: perl
236             # tab-width: 3
237             # End: