File Coverage

blib/lib/Dancer2/Plugin/FormValidator/Validator/Alpha.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 4     4   2490 use warnings;
  4         10  
  4         115  
4 4     4   87  
  4         56  
  4         95  
5             use Moo;
6 4     4   20 use utf8;
  4         18  
  4         25  
7 4     4   1245 use namespace::clean;
  4         7  
  4         42  
8 4     4   128  
  4         10  
  4         31  
9             with 'Dancer2::Plugin::FormValidator::Role::Validator';
10              
11             use constant {
12             UNICODE => 'u',
13 4         1768 ASCII => 'a',
14             };
15 4     4   1111  
  4         7  
16             has encoding => (
17             is => 'rw',
18             default => ASCII,
19             );
20              
21             my $encoding = $_[0]->encoding;
22              
23 3     3 0 80 if ($encoding eq UNICODE) {
24             return {
25 3 100       13 en => '%s must contain only alphabetical symbols',
26             ru => '%s должно содержать только символы алфавита',
27 1         17 de => '%s darf nur alphabetische Zeichen enthalten',
28             };
29             }
30              
31             return {
32             en => '%s must contain only latin alphabetical symbols',
33             ru => '%s должно содержать только символы латинского алфавита',
34 2         20 de => '%s darf nur lateinische Zeichen enthalten',
35             };
36             }
37              
38             my ($self, $field, $input, $encoding) = @_;
39              
40             my $regex;
41 6     6 0 19  
42             if (defined $encoding and $encoding eq UNICODE) {
43 6         13 $regex = qr/^[[:alpha:]]+$/;
44             $self->encoding(UNICODE);
45 6 100 66     39 }
46 3         14 else {
47 3         15 $regex = qr/^[[:alpha:]]+$/a;
48             $self->encoding(ASCII);
49             }
50 3         15  
51 3         16 if ($self->_field_defined_and_non_empty($field, $input)) {
52             return $input->{$field} =~ $regex;
53             }
54 6 50       27  
55 6         87 return 1;
56             }
57              
58 0           1;