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   2106 use warnings;
  4         10  
  4         101  
4 4     4   86  
  4         7  
  4         91  
5             use Moo;
6 4     4   19 use utf8;
  4         16  
  4         22  
7 4     4   1140 use namespace::clean;
  4         11  
  4         35  
8 4     4   115  
  4         8  
  4         21  
9             with 'Dancer2::Plugin::FormValidator::Role::Validator';
10              
11             use constant {
12             UNICODE => 'u',
13 4         1651 ASCII => 'a',
14             };
15 4     4   971  
  4         7  
16             has encoding => (
17             is => 'rw',
18             default => ASCII,
19             );
20              
21             my $encoding = $_[0]->encoding;
22              
23 3     3 0 73 if ($encoding eq UNICODE) {
24             return {
25 3 100       12 en => '%s must contain only alphabetical symbols',
26             ru => '%s должно содержать только символы алфавита',
27 1         16 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         17 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     30 }
46 3         13 else {
47 3         13 $regex = qr/^[[:alpha:]]+$/a;
48             $self->encoding(ASCII);
49             }
50 3         10  
51 3         11 if ($self->_field_defined_and_non_empty($field, $input)) {
52             return $input->{$field} =~ $regex;
53             }
54 6 50       22  
55 6         76 return 1;
56             }
57              
58 0           1;