blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/MouseX/Types/Common.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 72 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 257 CC 13 SCAR 27.3
CC
13
Cov
0%
_date
42.0
_time
20.0
_json
12.0
CRAP 15.3
1 package Dwarf::Plugin::MouseX::Types::Common;
2 use Dwarf::Pragma;
3 use Dwarf::Util qw/add_method encode_utf8 decode_utf8/;
4 use Email::Valid;
5 use Email::Valid::Loose;
6 use JSON;
7 use Mouse;
8 use Mouse::Util::TypeConstraints;
9 use Regexp::Common qw/URI/;
10  
11 sub init {
12 my ($class, $c, $conf) = @_;
13 $conf ||= {};
14  
15 subtype URL
16 => as 'Str'
17 => where { $_ =~ /($RE{URI}{HTTP}{-scheme =>'(https|http)'})/o };
18  
19 subtype Email
20 => as 'Str'
21 => where { Email::Valid::Loose->address(encode_utf8 $_) };
22  
23 subtype ASCII
24 => as 'Str'
25 => where { $_ =~ /^[\x21-\x7E]+$/ };
26  
27 subtype Hiragana
28 => as 'Str'
29 => where { _delsp($_) =~ /^\p{InHiragana}+$/ };
30  
31 subtype Katakana
32 => as 'Str'
33 => where { _delsp($_) =~ /^\p{InKatakana}+$/ };
34  
35 subtype Date
36 => as 'Str'
37 => where { _date(split /-/, $_) };
38  
39 subtype Time
40 => as 'Str'
41 => where { _time(split /:/, $_); };
42  
43 subtype DateTime
44 => as 'Str'
45 => where { _datetime($_) };
46  
47 subtype JTel
48 => as 'Str'
49 => where { $_ =~ /^0\d+\-?\d+\-?\d+$/ };
50  
51 subtype JZip
52 => as 'Str'
53 => where { $_ =~ /^\d{3}\-\d{4}$/ };
54  
55 subtype CreditcardNumber
56 => as 'Str'
57 => where { $_ =~ /\A[0-9]{14,16}\z/ };
58  
59 subtype CreditcardExpire
60 => as 'Str'
61 => where { $_ =~ /^\d{2}\/\d{2}$/ };
62  
63 subtype CreditcardSecurity
64 => as 'Str'
65 => where { $_ =~ /\A[0-9]{3,4}\z/ };
66  
67 subtype JSON
68 => as 'Str'
69 => where { _json($_) };
70  
71 subtype Base64JPEG
72 => as 'Str'
73 => where { _base64_type($_, 'jpeg') };
74  
75 subtype Base64PNG
76 => as 'Str'
77 => where { _base64_type($_, 'png') };
78  
79 subtype Base64GIF
80 => as 'Str'
81 => where { _base64_type($_, 'gif') };
82 }
83  
84 sub _datetime {
85 my $str = shift;
86 return 0 unless $str =~ /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/;
87 my @a = split / /, $str;
88 return _date(split /-/, $a[0]) && _time(split /:/, $a[1]);
89 }
90  
91 sub _date {
92 my ($y, $m, $d) = @_;
93  
94 return 0 if ( !$y or !$m or !$d );
95  
96 if ($d > 31 or $d < 1 or $m > 12 or $m < 1 or $y == 0) {
97 return 0;
98 }
99 if ($d > 30 and ($m == 4 or $m == 6 or $m == 9 or $m == 11)) {
100 return 0;
101 }
102 if ($d > 29 and $m == 2) {
103 return 0;
104 }
105 if ($m == 2 and $d > 28 and !($y % 4 == 0 and ($y % 100 != 0 or $y % 400 == 0))){
106 return 0;
107 }
108 return 1;
109 }
110  
111 sub _time {
112 my ($h, $m, $s) = @_;
113  
114 return 0 if (!defined($h) or !defined($m));
115 return 0 if ("$h" eq "" or "$m" eq "");
116 $s ||= 0; # optional
117  
118 if ( $h > 23 or $h < 0 or $m > 59 or $m < 0 or $s > 59 or $s < 0 ) {
119 return 0;
120 }
121  
122 return 1;
123 }
124  
125 sub _json {
126 my $value = $_;
127 return 1 unless defined $value;
128 my $data = eval { decode_json encode_utf8 $value };
129 if ($@) {
130 warn $@;
131 warn $value;
132 return 0;
133 }
134 return 1;
135 }
136  
137 sub _base64_type {
138 my ($value, $expected) = @_;
139  
140 my $filetype = '';
141  
142 my $decoded = decode_base64($value);
143 my $type = image_type(\$decoded);
144  
145 if (my $error = $type->{error}) {
146 return 0;
147 }
148  
149 $filetype = lc $type->{file_type};
150 return $filetype =~ /^$expected$/i;
151 };
152  
153 sub _delsp {
154 my $x = $_;
155 $x =~ s/\s//g;
156 return $x;
157 }
158  
159 1;