Lazy/Role/Check/RegExp.pm

filter: Statementstmt 100% 29 / 29 Branchbran 27.6% 21 / 76 Conditioncond 75.0% 6 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 6 / 6 Podpod 50.0% 1 / 2 totaltotal 51.5% 65 / 126 CC 43 SCAR 58.0
CC
43
Cov
50%
check
397.4
before_check
4.3
BEGIN
1.0
CRAP 331.6
↑ Summary Lazy/Role/Check/IP.pm »
1 package Validator::Lazy::Role::Check::RegExp;
2  
3 3 our $VERSION = '0.03';
4  
5 =head1 NAME
6  
7 Validator::Lazy::Role::Check::RegExp
8  
9  
10 =head1 VERSION
11  
12 Version 0.03
13  
14  
15 =head1 SYNOPSIS
16  
17 use Validator::Lazy;
18 my $v = Validator::Lazy->new( { regexp => { RegExp => [ list of regexps for checking ] } } );
19  
20 my $ok = $v->check( regexp => 'xxxxx' ); # true / false
21 say Dumper $v->errors; # [ { code => 'REGEXP_ERROR', field => 'regexp', data => { expr => 'expr_which_check_is_fail' } } ]
22  
23  
24 =head1 DESCRIPTION
25  
26 An internal Role for Validator::Lazy, part of Validator::Lazy package.
27  
28 Provides "RegExp" type for Validator::Lazy config.
29 Allows to check value by regular expressions.
30  
31  
32 =head1 METHODS
33  
34 =head2 C<check>
35  
36 Called from inside if Validator::Lazy->check process
37  
38 Temporary overrides internal Validator::Lazy::check method like this:
39  
40 $validator->check( $value, $param );
41  
42 $param - is a regular expression string or list of regular expressions
43 each param should be a string in format = '/your-regexp-here/aimsx'
44 all modificators can be combined with other, and all of them can be omitted
45  
46 examples:
47 '/^\d+$/' # integer
48 '^[A-Z][a-z]{1,64}$' # name
49 etc.
50  
51 $value - your value to check
52  
53  
54 =head1 SUPPORT AND DOCUMENTATION
55  
56 After installing, you can find documentation for this module with the perldoc command.
57  
58 perldoc Validator::Lazy
59  
60 You can also look for information at:
61  
62 RT, CPAN's request tracker (report bugs here)
63 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy
64  
65 AnnoCPAN, Annotated CPAN documentation
66 http://annocpan.org/dist/Validator-Lazy
67  
68 CPAN Ratings
69 http://cpanratings.perl.org/d/Validator-Lazy
70  
71 Search CPAN
72 http://search.cpan.org/dist/Validator-Lazy/
73  
74  
75 =head1 AUTHOR
76  
77 ANTONC <antonc@cpan.org>
78  
79 =head1 LICENSE
80  
81 This program is free software; you can redistribute it and/or modify it
82 under the terms of the the Artistic License (2.0). You may obtain a
83 copy of the full license at:
84  
85 L<http://www.perlfoundation.org/artistic_license_2_0>
86  
87 =cut
88  
89 3 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 3 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 # m/s/tr
95  
96 191 ▶sub before_check {
Subroutine before_check: called CC=4 SCAR=14.6
Pod undocumented
97 191 my ( $self, $value, $param ) = @_;
98  
99 191 ▶ confess 'at least one rule should be passed' unless $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $param 0 191 191
100 191 ▶ confess 'rules should be scalar or arrayref' unless ! ref $param || ref $param eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $param or ref $param eq "ARRAY" 0 191 191
Condition66%2/3
not ref $param or ref $param eq "ARRAY"
l!l&&r!l&&!r
18740
MC/DC0%0/2
not ref $param or ref $param eq "ARRAY"
not ref $param ref $param eq "ARRAY"
Truth table66%2/3
not(A) or B
A= not ref $paramB= ref $param eq "ARRAY"
ABresult
1X1
011
000
101  
102 191 return $value;
103 };
104  
105 191 ▶sub check {
Subroutine check: called CC=40 SCAR=59.8
Pod documented
106 191 my ( $self, $value, $param ) = @_;
107  
108 191 ▶ return $value unless $value && $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $value and $param 47 144 191
Condition66%2/3
$value and $param
!ll&&!rl&&r
470144
MC/DC50%1/2
$value and $param
$value $param
Truth table66%2/3
A and B
A= $valueB= $param
ABresult
0X0
100
111
109  
110 144 ▶ $param = [ $param ] unless ref $param;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $param 140 4 144
111  
112 # simple regexps
113 144 for my $exp ( @$param ) {
114 148 my( $e, $m ) = ( $exp =~ /^\/(.+)\/([aimsx]+)?$/ );
115  
116 148 ▶ confess 'cannot parse regexp = ' . $exp unless $e;
Branch: 1 branch
BranchTrueFalseTotal
unless $e 0 148 148
117  
118 # a - 5.14+ - affect which character-set rules (Unicode, etc.) are used
119 # x - Extend your pattern's legibility by permitting whitespace and comments.
120 # i - Do case-insensitive pattern matching.
121 # s - That is, change "." to match any character whatsoever, even a newline, which normally it would not match.
122 # m - That is, change "^" and "$" from matching the start of the string's first line and the end of its last line to matching the start and end of each line within the string.
123 148 ▶ $m //= '';
Condition100%2/2
$m //= ""
l!l
2146
MC/DC100%1/1
$m //= ""
$m
Truth table100%2/2
A
A= $m
Aresult
11
00
124 148 $m = join '', sort split /\B/, $m;
125  
126 # aimsx
127 148 ▶ my $ok =
Branch: 32 branches
BranchTrueFalseTotal
$m eq '' ? : 146 2 148
$m eq 'x' ? : 0 2 2
$m eq 's' ? : 0 2 2
$m eq 'sx' ? : 0 2 2
$m eq 'm' ? : 0 2 2
$m eq 'mx' ? : 0 2 2
$m eq 'ms' ? : 0 2 2
$m eq 'msx' ? : 0 2 2
$m eq 'i' ? : 1 1 2
$m eq 'ix' ? : 1 0 1
$m eq 'is' ? : 0 0 0
$m eq 'isx' ? : 0 0 0
$m eq 'im' ? : 0 0 0
$m eq 'imx' ? : 0 0 0
$m eq 'ims' ? : 0 0 0
$m eq 'imsx' ? : 0 0 0
$m eq 'a' ? : 0 0 0
$m eq 'ax' ? : 0 0 0
$m eq 'as' ? : 0 0 0
$m eq 'asx' ? : 0 0 0
$m eq 'am' ? : 0 0 0
$m eq 'amx' ? : 0 0 0
$m eq 'ams' ? : 0 0 0
$m eq 'amsx' ? : 0 0 0
$m eq 'ai' ? : 0 0 0
$m eq 'aix' ? : 0 0 0
$m eq 'ais' ? : 0 0 0
$m eq 'aisx' ? : 0 0 0
$m eq 'aim' ? : 0 0 0
$m eq 'aimx' ? : 0 0 0
$m eq 'aims' ? : 0 0 0
$m eq 'aimsx' ? : 0 0 0
128 $m eq '' ? $value =~ /$e/ :
129 $m eq 'x' ? $value =~ /$e/x :
130 $m eq 's' ? $value =~ /$e/s :
131 $m eq 'sx' ? $value =~ /$e/sx :
132 $m eq 'm' ? $value =~ /$e/m :
133 $m eq 'mx' ? $value =~ /$e/mx :
134 $m eq 'ms' ? $value =~ /$e/ms :
135 $m eq 'msx' ? $value =~ /$e/msx :
136 $m eq 'i' ? $value =~ /$e/i :
137 $m eq 'ix' ? $value =~ /$e/ix :
138 $m eq 'is' ? $value =~ /$e/is :
139 $m eq 'isx' ? $value =~ /$e/isx :
140 $m eq 'im' ? $value =~ /$e/im :
141 $m eq 'imx' ? $value =~ /$e/imx :
142 $m eq 'ims' ? $value =~ /$e/ims :
143 $m eq 'imsx' ? $value =~ /$e/imsx :
144  
145 $m eq 'a' ? $value =~ /$e/a :
146 $m eq 'ax' ? $value =~ /$e/ax :
147 $m eq 'as' ? $value =~ /$e/as :
148 $m eq 'asx' ? $value =~ /$e/asx :
149 $m eq 'am' ? $value =~ /$e/am :
150 $m eq 'amx' ? $value =~ /$e/amx :
151 $m eq 'ams' ? $value =~ /$e/ams :
152 $m eq 'amsx' ? $value =~ /$e/amsx :
153 $m eq 'ai' ? $value =~ /$e/ai :
154 $m eq 'aix' ? $value =~ /$e/aix :
155 $m eq 'ais' ? $value =~ /$e/ais :
156 $m eq 'aisx' ? $value =~ /$e/aisx :
157 $m eq 'aim' ? $value =~ /$e/aim :
158 $m eq 'aimx' ? $value =~ /$e/aimx :
159 $m eq 'aims' ? $value =~ /$e/aims :
160 $m eq 'aimsx' ? $value =~ /$e/aimsx :
161 '';
162  
163 148 ▶ unless ( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 5 143 148
164 5 $self->add_error( { exp => $exp } );
165 };
166 }
167  
168 144 return $value;
169 };
170  
171 3 1;
↑ Summary Lazy/Role/Check/IP.pm »