| 1 | package WWW::FieldValidator; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = "1.19"; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # The following constants represent the validator types that can be used to | ||||||||||||||||||||||||||||
| 10 | # validate the specified user input. | ||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use constant WELL_FORMED_EMAIL => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use constant MIN_STR_LENGTH => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use constant MAX_STR_LENGTH => 5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use constant REGEX_MATCH => 3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use constant USER_DEFINED_SUB => 4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Create a new Validator object. | ||||||||||||||||||||||||||||
| 18 | sub new { | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # The type of validation the instance will use | ||||||||||||||||||||||||||||
| 22 | 2 | my $validatorType = shift; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # The error feedback to return if the test data does not pass validation | ||||||||||||||||||||||||||||
| 25 | 2 | my $feedback = shift; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | my $self = {}; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | bless($self, $class); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | $self->{feedback} = $feedback; | |||||||||||||||||||||||||||
| 32 | 2 | $self->{validatorType} = $validatorType; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | if ($validatorType == MIN_STR_LENGTH) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Set min length to 1 by default | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $minLength = shift || 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 1
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 38 | 0 | $self->{minLength} = $minLength; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | elsif ($validatorType == REGEX_MATCH) { | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | my $regex = shift; | |||||||||||||||||||||||||||
| 44 | 1 | $self->{regex} = $regex; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | elsif ($validatorType == USER_DEFINED_SUB) { | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | my $subRef = shift; | |||||||||||||||||||||||||||
| 50 | 0 | $self->{usub} = $subRef; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | elsif ($validatorType == MAX_STR_LENGTH) { | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Set max_length to 30 by default | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my $maxLength = shift || 30; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 30
MC/DC0%0/1
shift() || 30
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 57 | 0 | $self->{maxLength} = $maxLength; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # is_optional - if true, field is only validated if it's not empty | ||||||||||||||||||||||||||||
| 61 | 2 | ▶ | my $isOptional = shift || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 62 | 2 | $self->{isOptional} = $isOptional; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | return $self; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 68 | # These methods should really be treated as private because the Form module | ||||||||||||||||||||||||||||
| 69 | # handles calling these methods, for most purposes all you will need to know | ||||||||||||||||||||||||||||
| 70 | # how to do is to instantiate FieldValidators. | ||||||||||||||||||||||||||||
| 71 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Checks to see what the validation type of the instance is and conditionally | ||||||||||||||||||||||||||||
| 74 | # calls the appropriate validation method. | ||||||||||||||||||||||||||||
| 75 | sub validate { | ||||||||||||||||||||||||||||
| 76 | 13 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=8 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 13 | my $input = shift; # User entered data | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # If the field is optional and is empty, don't validate | ||||||||||||||||||||||||||||
| 80 | 13 | ▶ | return 1 if ($self->{isOptional} && !$input); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"isOptional"} and not $input
MC/DC0%0/2
$self->{"isOptional"} and not $input
$self->{"isOptional"}
not $input
Truth table33%1/3
A and not(B)
A= $self->{"isOptional"}B= not $input
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 13 | ▶ | if ($self->{validatorType} == WELL_FORMED_EMAIL) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | return $self->_validateEmail($input); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | elsif ($self->{validatorType} == MIN_STR_LENGTH) { | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | return $self->_validateStrLength($input, $self->{minLength}); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | elsif ($self->{validatorType} == REGEX_MATCH) { | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 11 | return $self->_validateRegex($input, $self->{regex}); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | elsif ($self->{validatorType} == USER_DEFINED_SUB) { | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | return $self->{usub}($input); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | elsif ($self->{validatorType} == MAX_STR_LENGTH) { | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | return $self->_validateMaxStrLength($input, $self->{maxLength}); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub getFeedback { | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
getFeedback: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 0 | return $self->{feedback}; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 4 | *get_feedback = \&getFeedback; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Checks to see if input is a well formed email address. | ||||||||||||||||||||||||||||
| 116 | # TODO: Maybe use Email::Valid for this? | ||||||||||||||||||||||||||||
| 117 | sub _validateEmail { | ||||||||||||||||||||||||||||
| 118 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_validateEmail: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 119 | 2 | ▶ | my $input = shift || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 120 | 2 | return ($input =~ /^[\w\-\.\+]+@([\w\-]+)(\.([\w\-]+))+$/); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Checks to see if input is a minimum string length. | ||||||||||||||||||||||||||||
| 124 | sub _validateStrLength { | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_validateStrLength: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 126 | 0 | my $input = shift; | |||||||||||||||||||||||||||
| 127 | 0 | return (length($input) >= $self->{minLength}); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | sub _validateMaxStrLength { | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_validateMaxStrLength: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 132 | 0 | my $input = shift; | |||||||||||||||||||||||||||
| 133 | 0 | return (length($input) <= $self->{maxLength}); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Checks to see if input matches the specified pattern. | ||||||||||||||||||||||||||||
| 137 | sub _validateRegex { | ||||||||||||||||||||||||||||
| 138 | 11 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_validateRegex: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 139 | 11 | ▶ | my $input = shift // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 11 | return ($input =~ /$self->{regex}/); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 4 | 1; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||