| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package DTL::Fast::Filter::Add; | 
| 2 | 2 |  |  | 2 |  | 738 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 45 |  | 
| 3 | 2 |  |  | 2 |  | 9 | use utf8; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 8 |  | 
| 4 | 2 |  |  | 2 |  | 40 | use warnings FATAL => 'all'; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 52 |  | 
| 5 | 2 |  |  | 2 |  | 9 | use parent 'DTL::Fast::Filter'; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 8 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | $DTL::Fast::FILTER_HANDLERS{add} = __PACKAGE__; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 2 |  |  | 2 |  | 125 | use Scalar::Util qw(looks_like_number); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 662 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | #@Override | 
| 12 |  |  |  |  |  |  | sub parse_parameters | 
| 13 |  |  |  |  |  |  | { | 
| 14 | 12 |  |  | 12 | 0 | 16 | my $self = shift; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | die $self->get_parse_error("no single arguments passed to the add ".__PACKAGE__) | 
| 17 |  |  |  |  |  |  | if ( | 
| 18 |  |  |  |  |  |  | ref $self->{parameter} ne 'ARRAY' | 
| 19 | 12 | 50 | 33 |  |  | 35 | or not scalar @{$self->{parameter}} | 
|  | 12 |  |  |  |  | 37 |  | 
| 20 |  |  |  |  |  |  | ); | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 12 |  |  |  |  | 18 | $self->{parameters} = [ @{$self->{parameter}} ]; | 
|  | 12 |  |  |  |  | 21 |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 12 |  |  |  |  | 36 | return $self; | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | #@Override | 
| 28 |  |  |  |  |  |  | sub filter | 
| 29 |  |  |  |  |  |  | { | 
| 30 | 11 |  |  | 11 | 0 | 21 | my ($self, $filter_manager, $value, $context) = @_; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 11 |  |  |  |  | 17 | my $result = $value; | 
| 33 | 11 |  |  |  |  | 17 | my $value_type = ref $value; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 11 | 100 |  |  |  | 31 | if ($value_type eq 'HASH') | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | { | 
| 37 | 1 |  |  |  |  | 4 | $result = { %$value }; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  | elsif ($value_type eq 'ARRAY') | 
| 40 |  |  |  |  |  |  | { | 
| 41 | 5 |  |  |  |  | 12 | $result = [ @$value ]; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | elsif ($value_type) # @todo here we can implement ->add interface | 
| 44 |  |  |  |  |  |  | { | 
| 45 | 0 |  |  |  |  | 0 | die $self->get_render_error("don't know how to add anything to $value_type"); | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 11 |  |  |  |  | 18 | foreach my $parameter (@{$self->{parameters}}) | 
|  | 11 |  |  |  |  | 17 |  | 
| 49 |  |  |  |  |  |  | { | 
| 50 | 11 |  |  |  |  | 26 | my $argument = $parameter->render($context); | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 11 |  |  |  |  | 22 | my $result_type = ref $result; | 
| 53 | 11 |  |  |  |  | 16 | my $argument_type = ref $argument; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 11 | 100 | 66 |  |  | 40 | if ($result_type eq 'HASH') | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | { | 
| 57 | 1 | 50 |  |  |  | 3 | if ($argument_type eq 'ARRAY') | 
|  |  | 0 |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | { | 
| 59 | 1 |  |  |  |  | 12 | %$result = (%$result, @$argument); | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  | elsif ($argument_type eq 'HASH') | 
| 62 |  |  |  |  |  |  | { | 
| 63 | 0 |  |  |  |  | 0 | %$result = (%$result, %$argument); | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | else | 
| 66 |  |  |  |  |  |  | { | 
| 67 | 0 |  |  |  |  | 0 | die $self->get_render_error("it's not possible to add a single value to a hash"); | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | elsif ($result_type eq 'ARRAY') | 
| 71 |  |  |  |  |  |  | { | 
| 72 | 5 | 100 |  |  |  | 13 | if ($argument_type eq 'ARRAY') | 
|  |  | 100 |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | { | 
| 74 | 1 |  |  |  |  | 3 | push @$result, @$argument; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | elsif ($argument_type eq 'HASH') | 
| 77 |  |  |  |  |  |  | { | 
| 78 | 1 |  |  |  |  | 5 | push @$result, (%$argument); | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | else | 
| 81 |  |  |  |  |  |  | { | 
| 82 | 3 |  |  |  |  | 8 | push @$result, $argument; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  | elsif (looks_like_number($result) and looks_like_number($argument)) | 
| 86 |  |  |  |  |  |  | { | 
| 87 | 1 |  |  |  |  | 3 | $result += $argument; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  | else | 
| 90 |  |  |  |  |  |  | { | 
| 91 | 4 |  |  |  |  | 11 | $result .= $argument; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 10 |  |  |  |  | 30 | return $result; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | 1; |