File Coverage

blib/lib/HTML/FormFu/Filter/FormatNumber.pm
Criterion Covered Total %
statement 19 20 95.0
branch 1 4 25.0
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 25 30 83.3


line stmt bran cond sub pod time code
1 3     3   694 use strict;
  3         12  
  3         179  
2              
3             package HTML::FormFu::Filter::FormatNumber;
4             $HTML::FormFu::Filter::FormatNumber::VERSION = '2.07';
5             # ABSTRACT: Convert a formatted number from a known locale
6              
7 3     3   20 use Moose;
  3         5  
  3         21  
8             extends 'HTML::FormFu::Filter';
9              
10 3     3   20382 use Number::Format;
  3         18  
  3         158  
11 3     3   19 use POSIX qw( setlocale LC_NUMERIC );
  3         7  
  3         24  
12              
13             __PACKAGE__->mk_inherited_accessors(qw( locale ));
14              
15             sub filter {
16 1     1 0 3 my ( $self, $value ) = @_;
17              
18 1         4 my $backup_locale = setlocale(LC_NUMERIC);
19              
20 1 50       4 if ( my $locale = $self->locale ) {
21              
22             # if unable to set locale, this isn't a validation error that the user
23             # should see, so return the original value, rather than throwing an error
24              
25 0 0       0 setlocale( LC_NUMERIC, $locale )
26             or return $value;
27             }
28              
29 1         5 my $format = Number::Format->new;
30              
31 1         382 $value = $format->unformat_number($value);
32              
33             # restore orginal locale
34 1         223 setlocale( LC_NUMERIC, $backup_locale );
35              
36 1         6 return $value;
37             }
38              
39             __PACKAGE__->meta->make_immutable;
40              
41             1;
42              
43             __END__
44              
45             =pod
46              
47             =encoding UTF-8
48              
49             =head1 NAME
50              
51             HTML::FormFu::Filter::FormatNumber - Convert a formatted number from a known locale
52              
53             =head1 VERSION
54              
55             version 2.07
56              
57             =head1 SYNOPSIS
58              
59             This filter simply does the opposite of
60             L<HTML::FormFu::Deflator::FormatNumber>. It removes all thousands separators
61             and decimal points and returns the raw number which can be handled by perl.
62             See L<Number::Format/unformat_number> for more details.
63              
64             locale: de_DE
65             elements:
66             - type: Text
67             name: number
68             filters:
69             - type: FormatNumber
70              
71             # An input value like "13.233.444,22" will be transformed to "13233444.22"
72             # Same for "13233444,22"
73              
74             =head1 METHODS
75              
76             =head2 locale
77              
78             If no locale is found, the server's locale will be used.
79              
80             This method is a special 'inherited accessor', which means it can be set on
81             the form, a enclosing block element, the field, or this filter.
82             When the value is read, if no value is defined it automatically traverses
83             the element's hierarchy of parents, through any block elements and up to the
84             form, searching for a defined value.
85              
86             =head1 AUTHOR
87              
88             Moritz Onken, C<onken at houseofdesign.de>
89              
90             =head1 SEE ALSO
91              
92             Is a sub-class of, and inherits methods from L<HTML::FormFu::Filter>
93              
94             L<HTML::FormFu::Deflator::FormatNumber>
95              
96             L<HTML::FormFu>
97              
98             =head1 COPYRIGHT & LICENSE
99              
100             Copyright 2008 Moritz Onken, all rights reserved.
101              
102             This program is free software; you can redistribute it and/or modify it
103             under the same terms as Perl itself.
104              
105             =head1 AUTHOR
106              
107             Carl Franks <cpan@fireartist.com>
108              
109             =head1 COPYRIGHT AND LICENSE
110              
111             This software is copyright (c) 2018 by Carl Franks.
112              
113             This is free software; you can redistribute it and/or modify it under
114             the same terms as the Perl 5 programming language system itself.
115              
116             =cut