File Coverage

blib/lib/Validator/Custom/Constraint.pm
Criterion Covered Total %
statement 173 177 97.7
branch 126 136 92.6
condition 114 114 100.0
subroutine 37 41 90.2
pod 0 34 0.0
total 450 502 89.6


line stmt bran cond sub pod time code
1             # Version 0 module
2             package Validator::Custom::Constraint;
3              
4 5     5   15 use strict;
  5         6  
  5         116  
5 5     5   16 use warnings;
  5         5  
  5         108  
6              
7 5     5   20 use Carp 'croak';
  5         6  
  5         11026  
8              
9             # Carp trust relationship
10             push @Validator::Custom::CARP_NOT, __PACKAGE__;
11              
12             my $NUM_RE = qr/^[-+]?[0-9]+(:?\.[0-9]+)?$/;
13              
14 6 100 100 6 0 33 sub ascii { defined $_[0] && $_[0] =~ /^[\x21-\x7E]+$/ ? 1 : 0 }
15              
16             sub between {
17 17     17 0 20 my ($value, $args) = @_;
18 17         19 my ($start, $end) = @$args;
19              
20            
21 17 100 100     522 croak "Constraint 'between' needs two numeric arguments"
      100        
      100        
22             unless defined($start) && $start =~ /$NUM_RE/ && defined($end) && $end =~ /$NUM_RE/;
23            
24 13 100 100     75 return 0 unless defined $value && $value =~ /$NUM_RE/;
25 10 100 100     58 return $value >= $start && $value <= $end ? 1 : 0;
26             }
27              
28 18 100   18 0 83 sub blank { defined $_[0] && $_[0] eq '' }
29              
30             sub date_to_timepiece {
31 20     20 0 18 my $value = shift;
32            
33 20         58 require Time::Piece;
34            
35             # To Time::Piece object
36 20 100       30 if (ref $value eq 'ARRAY') {
37 14         12 my $year = $value->[0];
38 14         10 my $mon = $value->[1];
39 14         8 my $mday = $value->[2];
40            
41 14 100 100     72 return [0, undef]
      100        
42             unless defined $year && defined $mon && defined $mday;
43            
44 11 100 100     68 unless ($year =~ /^[0-9]{1,4}$/ && $mon =~ /^[0-9]{1,2}$/
      100        
45             && $mday =~ /^[0-9]{1,2}$/)
46             {
47 6         18 return [0, undef];
48             }
49            
50 5         16 my $date = sprintf("%04s%02s%02s", $year, $mon, $mday);
51            
52 5         1 my $tp;
53 5         6 eval {
54 5     0   23 local $SIG{__WARN__} = sub { die @_ };
  0         0  
55 5         14 $tp = Time::Piece->strptime($date, '%Y%m%d');
56             };
57            
58 5 100       101 return $@ ? [0, undef] : [1, $tp];
59             }
60             else {
61 6 100       12 $value = '' unless defined $value;
62 6         14 $value =~ s/[^0-9]//g;
63            
64 6 100       18 return [0, undef] unless $value =~ /^[0-9]{8}$/;
65            
66 4         4 my $tp;
67 4         5 eval {
68 4     0   20 local $SIG{__WARN__} = sub { die @_ };
  0         0  
69 4         12 $tp = Time::Piece->strptime($value, '%Y%m%d');
70             };
71 4 100       86 return $@ ? [0, undef] : [1, $tp];
72             }
73             }
74              
75             sub datetime_to_timepiece {
76 26     26 0 24 my $value = shift;
77            
78 26         70 require Time::Piece;
79            
80             # To Time::Piece object
81 26 100       35 if (ref $value eq 'ARRAY') {
82 20         29 my $year = $value->[0];
83 20         17 my $mon = $value->[1];
84 20         9 my $mday = $value->[2];
85 20         17 my $hour = $value->[3];
86 20         12 my $min = $value->[4];
87 20         13 my $sec = $value->[5];
88              
89 20 100 100     166 return [0, undef]
      100        
      100        
      100        
      100        
90             unless defined $year && defined $mon && defined $mday
91             && defined $hour && defined $min && defined $sec;
92            
93 14 100 100     125 unless ($year =~ /^[0-9]{1,4}$/ && $mon =~ /^[0-9]{1,2}$/
      100        
      100        
      100        
      100        
94             && $mday =~ /^[0-9]{1,2}$/ && $hour =~ /^[0-9]{1,2}$/
95             && $min =~ /^[0-9]{1,2}$/ && $sec =~ /^[0-9]{1,2}$/)
96             {
97 9         25 return [0, undef];
98             }
99            
100 5         16 my $date = sprintf("%04s%02s%02s%02s%02s%02s",
101             $year, $mon, $mday, $hour, $min, $sec);
102 5         3 my $tp;
103 5         5 eval {
104 5     0   24 local $SIG{__WARN__} = sub { die @_ };
  0         0  
105 5         13 $tp = Time::Piece->strptime($date, '%Y%m%d%H%M%S');
106             };
107            
108 5 100       96 return $@ ? [0, undef] : [1, $tp];
109             }
110             else {
111 6 100       11 $value = '' unless defined $value;
112 6         20 $value =~ s/[^0-9]//g;
113            
114 6 100       18 return [0, undef] unless $value =~ /^[0-9]{14}$/;
115            
116 4         2 my $tp;
117 4         4 eval {
118 4     0   19 local $SIG{__WARN__} = sub { die @_ };
  0         0  
119 4         10 $tp = Time::Piece->strptime($value, '%Y%m%d%H%M%S');
120             };
121 4 100       81 return $@ ? [0, undef] : [1, $tp];
122             }
123             }
124              
125             sub decimal {
126 16     16 0 15 my ($value, $digits_tmp) = @_;
127            
128             # 桁数情報を整理
129 16         12 my $digits;
130 16 100       19 if (defined $digits_tmp) {
131 13 100       16 if (ref $digits_tmp eq 'ARRAY') {
132 9         12 $digits = $digits_tmp;
133             }
134             else {
135 4         7 $digits = [$digits_tmp, undef];
136             }
137             }
138             else {
139 3         5 $digits = [undef, undef];
140             }
141            
142             # 正規表現を作成
143 16         11 my $re;
144 16 100 100     58 if (defined $digits->[0] && defined $digits->[1]) {
    100          
    100          
145 4         80 $re = qr/^[0-9]{1,$digits->[0]}(\.[0-9]{0,$digits->[1]})?$/;
146             }
147             elsif (defined $digits->[0]) {
148 7         52 $re = qr/^[0-9]{1,$digits->[0]}(\.[0-9]*)?$/;
149             }
150             elsif (defined $digits->[1]) {
151 2         17 $re = qr/^[0-9]+(\.[0-9]{0,$digits->[1]})?$/;
152             }
153             else {
154 3         6 $re = qr/^[0-9]+(\.[0-9]*)?$/;
155             }
156            
157             # 値をチェック
158 16 100 100     88 if (defined $value && $value =~ /$re/) {
159 6         19 return 1;
160             }
161             else {
162 10         30 return 0;
163             }
164             }
165              
166             sub duplication {
167 10     10 0 17 my $values = shift;
168              
169 10 100 100     48 return 0 unless defined $values->[0] && defined $values->[1];
170 6 100       30 return $values->[0] eq $values->[1] ? [1, $values->[0]] : 0;
171             }
172              
173             sub equal_to {
174 9     9 0 13 my ($value, $target) = @_;
175            
176 9 100 100     218 croak "Constraint 'equal_to' needs a numeric argument"
177             unless defined $target && $target =~ /$NUM_RE/;
178            
179 7 100 100     44 return 0 unless defined $value && $value =~ /$NUM_RE/;
180 4 100       20 return $value == $target ? 1 : 0;
181             }
182              
183             sub greater_than {
184 13     13 0 19 my ($value, $target) = @_;
185            
186 13 100 100     252 croak "Constraint 'greater_than' needs a numeric argument"
187             unless defined $target && $target =~ /$NUM_RE/;
188            
189 11 100 100     62 return 0 unless defined $value && $value =~ /$NUM_RE/;
190 8 100       26 return $value > $target ? 1 : 0;
191             }
192              
193             sub http_url {
194 5 100 100 5 0 33 return defined $_[0] && $_[0] =~ /^s?https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+$/ ? 1 : 0;
195             }
196              
197 55 100 100 55 0 342 sub int { defined $_[0] && $_[0] =~ /^\-?[0-9]+$/ ? 1 : 0 }
198              
199             sub in_array {
200 6     6 0 8 my ($value, $args) = @_;
201 6 100       13 $value = '' unless defined $value;
202 6         7 my $match = grep { $_ eq $value } @$args;
  12         19  
203 6 100       18 return $match > 0 ? 1 : 0;
204             }
205              
206             sub length {
207 21     21 0 23 my ($value, $args) = @_;
208            
209 21 100       30 return unless defined $value;
210            
211 20         15 my $min;
212             my $max;
213 20 100       38 if(ref $args eq 'ARRAY') { ($min, $max) = @$args }
  6 100       29  
214             elsif (ref $args eq 'HASH') {
215 12         11 $min = $args->{min};
216 12         14 $max = $args->{max};
217             }
218 2         3 else { $min = $max = $args }
219            
220 20 100 100     214 croak "Constraint 'length' needs one or two arguments"
221             unless defined $min || defined $max;
222            
223 19         20 my $length = length $value;
224 19         16 my $is_valid;
225 19 100 100     62 if (defined $min && defined $max) {
    100          
    50          
226 11   100     32 $is_valid = $length >= $min && $length <= $max;
227             }
228             elsif (defined $min) {
229 4         5 $is_valid = $length >= $min;
230             }
231             elsif (defined $max) {
232 4         5 $is_valid =$length <= $max;
233             }
234            
235 19         46 return $is_valid;
236             }
237              
238             sub less_than {
239 11     11 0 13 my ($value, $target) = @_;
240            
241 11 100 100     224 croak "Constraint 'less_than' needs a numeric argument"
242             unless defined $target && $target =~ /$NUM_RE/;
243            
244 9 100 100     48 return 0 unless defined $value && $value =~ /$NUM_RE/;
245 6 100       23 return $value < $target ? 1 : 0;
246             }
247              
248             sub merge {
249 3     3 0 5 my $values = shift;
250            
251 3 100       7 $values = [$values] unless ref $values eq 'ARRAY';
252            
253 3         13 return [1, join('', @$values)];
254             }
255              
256 6 100   6 0 28 sub string { defined $_[0] && !ref $_[0] }
257 30 100   30 0 152 sub not_blank { defined $_[0] && $_[0] ne '' }
258 2     2 0 6 sub not_defined { !defined $_[0] }
259 8 100 100 8 0 59 sub not_space { defined $_[0] && $_[0] !~ '^[ \t\n\r\f]*$' ? 1 : 0 }
260              
261 10 100 100 10 0 96 sub uint { defined $_[0] && $_[0] =~ /^[0-9]+$/ ? 1 : 0 }
262              
263             sub regex {
264 7     7 0 10 my ($value, $regex) = @_;
265 7 100 100     78 defined $value && $value =~ /$regex/ ? 1 : 0;
266             }
267              
268             sub selected_at_least {
269 10     10 0 10 my ($values, $num) = @_;
270            
271 10 100       16 my $selected = ref $values ? $values : [$values];
272 10         7 $num += 0;
273 10 100       25 return scalar(@$selected) >= $num ? 1 : 0;
274             }
275              
276             sub shift_array {
277 2     2 0 3 my $values = shift;
278            
279 2 100       6 $values = [$values] unless ref $values eq 'ARRAY';
280            
281 2         18 return [1, shift @$values];
282             }
283              
284 8 100 100 8 0 53 sub space { defined $_[0] && $_[0] =~ '^[ \t\n\r\f]*$' ? 1 : 0 }
285              
286             sub to_array {
287 8     8 0 9 my $value = shift;
288            
289 8 100       18 $value = [$value] unless ref $value eq 'ARRAY';
290            
291 8         20 return [1, $value];
292             }
293              
294             sub to_array_remove_blank {
295 4     4 0 4 my $values = shift;
296            
297 4 100       8 $values = [$values] unless ref $values eq 'ARRAY';
298 4 50       5 $values = [grep { defined $_ && CORE::length $_} @$values];
  8         30  
299            
300 4         11 return [1, $values];
301             }
302              
303             sub trim {
304 3     3 0 6 my $value = shift;
305 3 50       22 $value =~ s/^[ \t\n\r\f]*(.*?)[ \t\n\r\f]*$/$1/ms if defined $value;
306 3         11 return [1, $value];
307             }
308              
309             sub trim_collapse {
310 2     2 0 4 my $value = shift;
311 2 50       7 if (defined $value) {
312 2         12 $value =~ s/[ \t\n\r\f]+/ /g;
313 2         10 $value =~ s/^[ \t\n\r\f]*(.*?)[ \t\n\r\f]*$/$1/ms;
314             }
315 2         7 return [1, $value];
316             }
317              
318             sub trim_lead {
319 2     2 0 4 my $value = shift;
320 2 50       14 $value =~ s/^[ \t\n\r\f]+(.*)$/$1/ms if defined $value;
321 2         7 return [1, $value];
322             }
323              
324             sub trim_trail {
325 2     2 0 3 my $value = shift;
326 2 50       12 $value =~ s/^(.*?)[ \t\n\r\f]+$/$1/ms if defined $value;
327 2         9 return [1, $value];
328             }
329              
330             sub trim_uni {
331 1     1 0 1 my $value = shift;
332 1 50       10 $value =~ s/^\s*(.*?)\s*$/$1/ms if defined $value;
333 1         4 return [1, $value];
334             }
335              
336             sub trim_uni_collapse {
337 1     1 0 2 my $value = shift;
338 1 50       3 if (defined $value) {
339 1         5 $value =~ s/\s+/ /g;
340 1         6 $value =~ s/^\s*(.*?)\s*$/$1/ms;
341             }
342 1         5 return [1, $value];
343             }
344              
345             sub trim_uni_lead {
346 1     1 0 2 my $value = shift;
347 1 50       14 $value =~ s/^\s+(.*)$/$1/ms if defined $value;
348 1         4 return [1, $value];
349             }
350              
351             sub trim_uni_trail {
352 1     1 0 1 my $value = shift;
353 1 50       8 $value =~ s/^(.*?)\s+$/$1/ms if defined $value;
354 1         3 return [1, $value];
355             }
356              
357             1;
358              
359             =head1 NAME
360              
361             Validator::Custom::Constraint - Constrint functions