File Coverage

blib/lib/DTL/Fast/Filter/Getdigit.pm
Criterion Covered Total %
statement 24 24 100.0
branch 4 6 66.6
condition 2 6 33.3
subroutine 6 6 100.0
pod 0 2 0.0
total 36 44 81.8


line stmt bran cond sub pod time code
1             package DTL::Fast::Filter::Getdigit;
2 2     2   710 use strict;
  2         4  
  2         44  
3 2     2   9 use utf8;
  2         3  
  2         8  
4 2     2   39 use warnings FATAL => 'all';
  2         4  
  2         53  
5 2     2   9 use parent 'DTL::Fast::Filter';
  2         3  
  2         8  
6              
7             $DTL::Fast::FILTER_HANDLERS{get_digit} = __PACKAGE__;
8              
9             #@Override
10             sub parse_parameters
11             {
12 4     4 0 8 my $self = shift;
13             die $self->get_parse_error("no digit number specified")
14 4 50       5 if (not scalar @{$self->{parameter}});
  4         11  
15 4         8 $self->{digit} = $self->{parameter}->[0];
16 4         10 return $self;
17             }
18              
19             #@Override
20             sub filter
21             {
22 4     4 0 8 my ($self, $filter_manager, $value, $context) = @_;
23              
24 4         12 my $digit = $self->{digit}->render($context);
25 4 50 33     44 if (
      33        
26             $value =~ /^\d+$/
27             and $digit =~ /^\d+$/
28             and $digit > 0
29             )
30             {
31 4 100       12 if (length $value >= $digit)
32             {
33 3         7 $value = substr $value, - $digit, 1;
34             }
35             else
36             {
37 1         2 $value = '';
38             }
39             }
40              
41 4         13 return $value;
42             }
43              
44             1;