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