Lazy/Role/Check/MinMax.pm

filter: Statementstmt 97.1% 34 / 35 Branchbran 85.0% 17 / 20 Conditioncond 73.6% 14 / 19 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 6 / 6 Podpod 50.0% 1 / 2 totaltotal 84.9% 79 / 93 CC 19 SCAR 27.1
CC
19
Cov
88%
check
19.5
before_check
2.0
BEGIN
1.0
CRAP 15.0
« Lazy/Role/Composer.pm ↑ Summary Lazy/Role/Check/Phone.pm »
1 package Validator::Lazy::Role::Check::MinMax;
2  
3 3 our $VERSION = '0.03';
4  
5 =head1 NAME
6  
7 Validator::Lazy::Role::Check::MinMax
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( { minmax => { MinMax => { min => 2, max => 4, type => 'Str' } } } );
19 my $v = Validator::Lazy->new( { minmax => { MinMax => [ $min, $max, $type ] } } );
20  
21 my $ok = $v->check( minmax => 'xxxxx' ); # ok is false
22 say Dumper $v->errors; # [ { code => 'TOO_BIG', field => 'minmax', data => { min => 2, max => 4 } } ]
23  
24  
25 =head1 DESCRIPTION
26  
27 An internal Role for Validator::Lazy, part of Validator::Lazy package.
28  
29 Provides "MinMax" type for Validator::Lazy config.
30 Allows to check value for predefined range (from-to-valuetype).
31  
32  
33 =head1 METHODS
34  
35 =head2 C<check>
36  
37 Called from inside if Validator::Lazy->check process
38  
39 Temporary overrides internal Validator::Lazy::check method like this:
40  
41 $validator->check( $value, $param );
42  
43 $param - is a list = [ minvalue, maxvalue, valuetype ]
44 OR
45 $param - is a hash = { min => minvalue, max => maxvalue, type => Str or Int ]
46  
47 $value - your value to check
48  
49  
50 =head1 SUPPORT AND DOCUMENTATION
51  
52 After installing, you can find documentation for this module with the perldoc command.
53  
54 perldoc Validator::Lazy
55  
56 You can also look for information at:
57  
58 RT, CPAN's request tracker (report bugs here)
59 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy
60  
61 AnnoCPAN, Annotated CPAN documentation
62 http://annocpan.org/dist/Validator-Lazy
63  
64 CPAN Ratings
65 http://cpanratings.perl.org/d/Validator-Lazy
66  
67 Search CPAN
68 http://search.cpan.org/dist/Validator-Lazy/
69  
70  
71 =head1 AUTHOR
72  
73 ANTONC <antonc@cpan.org>
74  
75 =head1 LICENSE
76  
77 This program is free software; you can redistribute it and/or modify it
78 under the terms of the the Artistic License (2.0). You may obtain a
79 copy of the full license at:
80  
81 L<http://www.perlfoundation.org/artistic_license_2_0>
82  
83 =cut
84  
85 3 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 3 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 # min => ?, max => ?, type => ?
91  
92 209 ▶sub before_check {
Subroutine before_check: called CC=2 SCAR=6.9
Pod undocumented
93 209 my ( $self, $value ) = @_;
94  
95 209 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 8 201 209
96  
97 201 chomp $value;
98 201 $value =~ s/(^\s+|\s+$)//ag;
99  
100 201 return $value;
101 };
102  
103 209 ▶sub check {
Subroutine check: called CC=18 SCAR=29.7
Pod documented
104 209 my ( $self, $value, $param ) = @_;
105  
106 209 ▶ return $value unless defined $value && $param;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $param 8 201 209
Condition66%2/3
defined $value and $param
!ll&&!rl&&r
80201
MC/DC50%1/2
defined $value and $param
defined $value $param
Truth table66%2/3
A and B
A= defined $valueB= $param
ABresult
0X0
100
111
107  
108 201 ▶ if ( $param && ref $param eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param and ref $param eq "ARRAY") 171 30 201
Condition66%2/3
$param and ref $param eq "ARRAY"
!ll&&!rl&&r
030171
MC/DC50%1/2
$param and ref $param eq "ARRAY"
$param ref $param eq "ARRAY"
Truth table66%2/3
A and B
A= $paramB= ref $param eq "ARRAY"
ABresult
0X0
100
111
109 171 $param = {
110 min => $param->[0],
111 max => $param->[1],
112 type => $param->[2],
113 };
114 };
115  
116 201 ▶ my $type = $param->{type} || 'Str';
Condition100%2/2
$param->{'type'} || 'Str'
l!l
70131
MC/DC100%1/1
$param->{'type'} || 'Str'
$param->{'type'}
Truth table100%2/2
A
A= $param->{'type'}
Aresult
11
00
117 # $type ||= $value =~ /^\-?\d+(\.\d)?/ ? 'Int' : 'Str';
118  
119 201 my ( $min, $max ) = ( @$param{ qw/ min max / } );
120  
121 201 ▶ if ( $type eq 'Int' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'Int') { } 24 177 201
elsif ($type eq 'Str') { } 177 0 177
122 24 ▶ $min //= 0;
Condition100%2/2
$min //= 0
l!l
213
MC/DC100%1/1
$min //= 0
$min
Truth table100%2/2
A
A= $min
Aresult
11
00
123 24 ▶ $max //= 1000;
Condition100%2/2
$max //= 1000
l!l
213
MC/DC100%1/1
$max //= 1000
$max
Truth table100%2/2
A
A= $max
Aresult
11
00
124 24 ▶ $self->add_error( 'TOO_SMALL', { min => $min, max => $max } ) if $value < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $value < $min 4 20 24
125 24 ▶ $self->add_error( 'TOO_BIG', { min => $min, max => $max } ) if $value > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $value > $max 7 17 24
126 }
127 elsif ( $type eq 'Str' ) {
128 177 ▶ $min //= 0;
Condition100%2/2
$min //= 0
l!l
1743
MC/DC100%1/1
$min //= 0
$min
Truth table100%2/2
A
A= $min
Aresult
11
00
129 177 ▶ $max //= 64;
Condition100%2/2
$max //= 64
l!l
1743
MC/DC100%1/1
$max //= 64
$max
Truth table100%2/2
A
A= $max
Aresult
11
00
130 177 ▶ $self->add_error( 'TOO_SMALL', { min => $min, max => $max } ) if length( $value ) < $min;
Branch: 1 branch
BranchTrueFalseTotal
if length $value < $min 2 175 177
131 177 ▶ $self->add_error( 'TOO_BIG', { min => $min, max => $max } ) if length( $value ) > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $max 5 172 177
132 }
133 else {
134 0 ▶ confess 'Unknown type. Avaliable values are: Str,Int' unless $type && $type =~ /^(Int|Str)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type and $type =~ /^(Int|Str)$/u 0 0 0
Condition0%0/3
$type and $type =~ /^(Int|Str)$/u
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $type =~ /^(Int|Str)$/u
$type $type =~ /^(Int|Str)$/u
Truth table0%0/3
A and B
A= $typeB= $type =~ /^(Int|Str)$/u
ABresult
0X0
100
111
135 };
136  
137 201 return $value;
138 };
139  
140 3 1;
« Lazy/Role/Composer.pm ↑ Summary Lazy/Role/Check/Phone.pm »