File Coverage

blib/lib/Dancer2/Plugin/FormValidator/Validator/AlphaNum.pm
Criterion Covered Total %
statement 31 32 96.8
branch 5 6 83.3
condition 2 3 66.6
subroutine 8 8 100.0
pod 0 2 0.0
total 46 51 90.2


line stmt bran cond sub pod time code
1              
2             use strict;
3 2     2   1174 use warnings;
  2         6  
  2         60  
4 2     2   10  
  2         9  
  2         43  
5             use Moo;
6 2     2   9 use utf8;
  2         5  
  2         10  
7 2     2   576 use namespace::clean;
  2         22  
  2         12  
8 2     2   67  
  2         4  
  2         8  
9             with 'Dancer2::Plugin::FormValidator::Role::Validator';
10              
11             use constant {
12             UNICODE => 'u',
13 2         689 ASCII => 'a',
14             };
15 2     2   465  
  2         3  
16             has encoding => (
17             is => 'rw',
18             default => ASCII,
19             );
20              
21             my $encoding = $_[0]->encoding;
22              
23 2     2 0 14 if ($encoding eq UNICODE) {
24             return {
25 2 100       10 en => '%s must contain only alphabetical symbols and/or numbers 0-9',
26             ru => '%s должно содержать только символы алфавита или/и цифры 0-9',
27 1         9 de => '%s darf nur alphabetische Symbole und/oder Zahlen 0-9 enthalten',
28             };
29             }
30              
31             return {
32             en => '%s must contain only latin alphabetical symbols',
33             ru => '%s должно содержать только символы латинского алфавита',
34 1         9 de => '%s darf nur lateinische Zeichen enthalten',
35             };
36             }
37              
38             my ($self, $field, $input, $encoding) = @_;
39              
40             my $regex;
41 4     4 0 15  
42             if (defined $encoding and $encoding eq UNICODE) {
43 4         6 $regex = qr/^\w+$/;
44             $self->encoding(UNICODE);
45 4 100 66     19 }
46 2         8 else {
47 2         9 $regex = qr/^\w+$/a;
48             $self->encoding(ASCII);
49             }
50 2         7  
51 2         7 if ($self->_field_defined_and_non_empty($field, $input)) {
52             return $input->{$field} =~ $regex;
53             }
54 4 50       14  
55 4         46 return 1;
56             }
57              
58 0           1;