File Coverage

blib/lib/String/Validator/Common.pm
Criterion Covered Total %
statement 88 88 100.0
branch 18 18 100.0
condition n/a
subroutine 24 24 100.0
pod 16 16 100.0
total 146 146 100.0


line stmt bran cond sub pod time code
1             package String::Validator::Common;
2             $String::Validator::Common::VERSION = '2.00';
3             # ABSTRACT: Base Module for creating new String::Validator Modules.
4              
5 8     8   571595 use 5.008;
  8         94  
6 8     8   40 use strict;
  8         15  
  8         145  
7 8     8   41 use warnings;
  8         14  
  8         207  
8 8     8   37 no warnings 'uninitialized';
  8         20  
  8         278  
9 8     8   3732 use Data::Printer;
  8         216451  
  8         59  
10              
11             my $common_messages = {
12             common_strings_not_match => "Strings don\'t match.",
13             common_tooshort => "Does not meet requirement: Min Length ",
14             common_toolong => " Does not meet requirement: Max Length ",
15             };
16              
17             sub _Messages {
18 9     9   20 my $messages = {} ;
19 9         23 for my $msg ( $common_messages, @_) {
20 27         41 for my $c ( keys %{$msg} ) {
  27         96  
21 34         75 $messages->{$c} = $msg->{$c};
22             }
23             }
24 9         22 return $messages;
25             }
26              
27             sub new {
28 9     9 1 3686 my $class = shift;
29 9         27 my $self = {@_};
30 9         47 bless $self, $class;
31 9         50 $self->{class} = $class;
32             $self->{messages}
33 9         45 = _Messages( $self->{language}, $self->{custom_messages} );
34 9         40 $self->_Init();
35 9         156 return $self;
36             }
37              
38             sub IncreaseErr {
39 12     12 1 26 my $self = shift;
40 12         51 $self->{errstring} .= "@_\n";
41 12         30 $self->{error}++;
42             }
43              
44             # Every time we get a new request
45             # these values need to be reset from the
46             # previous request.
47             sub _Init {
48 33     33   62 my $self = shift;
49 33         75 $self->{errstring} = '';
50 33         59 $self->{error} = 0;
51 33         70 $self->{string} = '';
52             }
53              
54             sub Start {
55 24     24 1 376 my ( $self, $string1, $string2 ) = @_;
56 24         79 $self->_Init();
57              
58             # String comparison, must not fail if no string2 is provided.
59             # string2 is also available for destructive operations.
60             # Failing the string match alse necessitates immediate
61             # error return as the other tests are meaningless as
62             # we cannot know if either or neither string is the password.
63 8     8   2755 no warnings 'uninitialized';
  8         18  
  8         4311  
64 24 100       110 if ( 0 == length $string2 ) { }
    100          
65             elsif ( $string1 ne $string2 ) {
66 6         23 $self->IncreaseErr( $self->{messages}{common_strings_not_match} );
67 6         18 return 99;
68             }
69 18         45 $self->{string} = $string1;
70 18         60 return 0;
71             }
72              
73             sub Length {
74 13     13 1 33 my $self = shift;
75 13         42 my $string = $self->{string};
76 13         31 my $strlen = length( $self->{string} );
77 13 100       45 if ( $strlen < $self->{min_len} ) {
78             $self->IncreaseErr(
79 3         16 $self->{messages}{common_tooshort} . $self->{min_len} );
80 3         15 return $self->{error};
81             }
82 10 100       30 if ( $self->{max_len} ) {
83 8 100       29 if ( $strlen > $self->{max_len} ) {
84             $self->IncreaseErr(
85 3         22 $self->{messages}{common_toolong} . $self->{max_len} );
86 3         15 return $self->{error};
87             }
88             }
89 7         27 return 0;
90             }
91              
92             sub CheckCommon {
93 13     13 1 50 my ( $self, $string1, $string2 ) = @_;
94 13 100       46 if ( $self->Start( $string1, $string2 ) ) {
95 1         4 return $self->{error};
96             }
97 12 100       40 if ( $self->Length ) { return $self->{error} }
  5         38  
98 7         42 return 0;
99             }
100              
101             # Check serves as example, but more importantly lets
102             # us test the module.
103             # Takes 2 strings and runs Start.
104             # If the strings match it returns 0, else 1.
105             sub Check {
106 7     7 1 21 my ( $self, $string1, $string2 ) = @_;
107 7         21 my $started = $self->Start( $string1, $string2 );
108 7         24 return $self->{error};
109             }
110              
111             sub Errcnt {
112 2     2 1 4 my $self = shift;
113 2         7 return $self->{error};
114             }
115              
116             sub Errstr {
117 6     6 1 16 my $self = shift;
118 6         52 return $self->{errstring};
119             }
120              
121             sub IsNot_Valid {
122 3     3 1 11 ( my $self, my $string1, my $string2 ) = @_;
123 3 100       11 if ( $self->Check( $string1, $string2 ) ) { return $self->{errstring} }
  1         9  
124 2         8 else { return 0 }
125             }
126              
127             sub Is_Valid {
128 3     3 1 13 ( my $self, my $string1, my $string2 ) = @_;
129 3 100       44 if ( $self->Check( $string1, $string2 ) ) { return 0 }
  1         4  
130 2         9 else { return 1 }
131             }
132              
133             sub String {
134 6     6 1 17 my $self = shift;
135 6         29 return $self->{string};
136             }
137              
138             # The lowercase version of methods.
139 1     1 1 2 sub errcnt { my $self = shift; $self->Errcnt( @_) }
  1         4  
140 1     1 1 2 sub errstr { my $self = shift; $self->Errstr( @_) }
  1         4  
141 1     1 1 1218 sub isnot_valid { my $self = shift; $self->IsNot_Valid( @_) }
  1         6  
142 1     1 1 564 sub is_valid { my $self = shift; $self->Is_Valid( @_ ) }
  1         8  
143 2     2 1 4 sub string { my $self = shift; $self->String( @_ ) }
  2         5  
144              
145              
146             1; # End of String::Validator::Common
147              
148             __END__