| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mojolicious::Plugin::Vparam::Numbers; | 
| 2 | 72 |  |  | 72 |  | 2840465 | use Mojo::Base -strict; | 
|  | 72 |  |  |  |  | 9024 |  | 
|  | 72 |  |  |  |  | 472 |  | 
| 3 | 72 |  |  | 72 |  | 7002 | use Mojolicious::Plugin::Vparam::Common; | 
|  | 72 |  |  |  |  | 152 |  | 
|  | 72 |  |  |  |  | 69145 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | sub check_int($) { | 
| 6 | 227 | 100 |  | 227 | 0 | 679 | return 'Value is not defined'       unless defined $_[0]; | 
| 7 | 136 | 50 |  |  |  | 392 | return 'Value is not set'           unless length  $_[0]; | 
| 8 | 136 |  |  |  |  | 402 | return 0; | 
| 9 |  |  |  |  |  |  | } | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub check_numeric($) { | 
| 12 | 73 | 100 |  | 73 | 0 | 199 | return 'Value is not defined'       unless defined $_[0]; | 
| 13 | 64 | 50 |  |  |  | 195 | return 'Value is not set'           unless length  $_[0]; | 
| 14 | 64 |  |  |  |  | 129 | return 0; | 
| 15 |  |  |  |  |  |  | } | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub check_money($) { | 
| 18 | 9 | 50 |  | 9 | 0 | 20 | return 'Value is not defined'       unless defined $_[0]; | 
| 19 | 9 | 50 |  |  |  | 23 | return 'Value is not set'           unless length  $_[0]; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 9 |  |  |  |  | 18 | my $numeric = check_numeric $_[0]; | 
| 22 | 9 | 50 |  |  |  | 20 | return $numeric if $numeric; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 9 | 100 | 100 |  |  | 58 | return 'Invalid fractional part' | 
| 25 |  |  |  |  |  |  | if $_[0] =~ m{\.} && $_[0] !~ m{\.\d{0,2}$}; | 
| 26 | 8 |  |  |  |  | 24 | return 0; | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub check_percent($) { | 
| 30 | 5 | 50 |  | 5 | 0 | 12 | return 'Value is not defined'       unless defined $_[0]; | 
| 31 | 5 | 50 |  |  |  | 20 | return 'Value is not set'           unless length  $_[0]; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 5 |  |  |  |  | 11 | my $numeric = check_numeric $_[0]; | 
| 34 | 5 | 50 |  |  |  | 9 | return $numeric if $numeric; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 5 | 100 |  |  |  | 18 | return 'Value must be greater than 0'   unless $_[0] >= 0; | 
| 37 | 4 | 100 |  |  |  | 11 | return 'Value must be less than 100'    unless $_[0] <= 100; | 
| 38 | 3 |  |  |  |  | 9 | return 0; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub check_lon($) { | 
| 42 | 12 | 50 |  | 12 | 0 | 29 | return 'Value not defined'     unless defined $_[0]; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 12 |  |  |  |  | 27 | my $numeric = check_numeric $_[0]; | 
| 45 | 12 | 50 |  |  |  | 26 | return $numeric if $numeric; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 12 | 100 |  |  |  | 43 | return 'Value should not be less than -180°'    unless $_[0] >= -180; | 
| 48 | 11 | 100 |  |  |  | 27 | return 'Value should not be greater than 180°'  unless $_[0] <= 180; | 
| 49 | 10 |  |  |  |  | 22 | return 0; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub check_lat($) { | 
| 53 | 12 | 50 |  | 12 | 0 | 29 | return 'Value not defined'      unless defined $_[0]; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 12 |  |  |  |  | 27 | my $numeric = check_numeric $_[0]; | 
| 56 | 12 | 50 |  |  |  | 35 | return $numeric if $numeric; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 12 | 100 |  |  |  | 37 | return 'Value should not be less than -90°'     unless $_[0] >= -90; | 
| 59 | 11 | 100 |  |  |  | 24 | return 'Value should not be greater than 90°'   unless $_[0] <= 90; | 
| 60 | 10 |  |  |  |  | 23 | return 0; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub parse_int($) { | 
| 64 | 227 |  |  | 227 | 0 | 416 | my ($str) = @_; | 
| 65 | 227 | 100 |  |  |  | 557 | return undef unless defined $str; | 
| 66 | 196 |  |  |  |  | 724 | my ($int) = $str =~ m{([-+]?\d+)}; | 
| 67 | 196 |  |  |  |  | 480 | return $int; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | sub parse_number($) { | 
| 71 | 33 |  |  | 33 | 0 | 63 | my ($str) = @_; | 
| 72 | 33 | 50 |  |  |  | 73 | return undef unless defined $str; | 
| 73 | 33 |  |  |  |  | 157 | my ($number) = $str =~ m{ | 
| 74 |  |  |  |  |  |  | ( | 
| 75 |  |  |  |  |  |  | [-+]? | 
| 76 |  |  |  |  |  |  | (?: | 
| 77 |  |  |  |  |  |  | \d+(?:[\.,]\d*)? | 
| 78 |  |  |  |  |  |  | | | 
| 79 |  |  |  |  |  |  | [\.,]\d+ | 
| 80 |  |  |  |  |  |  | ) | 
| 81 |  |  |  |  |  |  | ) | 
| 82 |  |  |  |  |  |  | }x; | 
| 83 | 33 | 100 |  |  |  | 92 | return undef unless defined $number; | 
| 84 | 31 |  |  |  |  | 97 | tr{,}{.} for $number; | 
| 85 | 31 |  |  |  |  | 84 | return $number; | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub register { | 
| 89 | 74 |  |  | 74 | 0 | 228 | my ($class, $self, $app, $conf) = @_; | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | $app->vtype( | 
| 92 |  |  |  |  |  |  | int         => | 
| 93 | 227 |  |  | 227 |  | 613 | pre     => sub{ parse_int       $_[1] }, | 
| 94 | 227 |  |  | 227 |  | 469 | valid   => sub{ check_int       $_[1] }, | 
| 95 | 217 | 100 |  | 217 |  | 755 | post    => sub{ defined         $_[1]   ? 0 + $_[1] : undef }, | 
| 96 | 74 |  |  |  |  | 733 | ); | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | my %numeric = ( | 
| 99 | 11 |  |  | 11 |  | 29 | pre     => sub{ parse_number    $_[1] }, | 
| 100 | 11 |  |  | 11 |  | 27 | valid   => sub{ check_numeric   $_[1] }, | 
| 101 | 11 | 100 |  | 11 |  | 50 | post    => sub{ defined         $_[1] ? 0.0 + $_[1] : undef }, | 
| 102 | 74 |  |  |  |  | 609 | ); | 
| 103 | 74 |  |  |  |  | 511 | $app->vtype(numeric => %numeric); | 
| 104 | 74 |  |  |  |  | 490 | $app->vtype(number  => %numeric); | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | $app->vtype( | 
| 107 |  |  |  |  |  |  | money       => | 
| 108 | 9 |  |  | 9 |  | 21 | pre     => sub{ parse_number    $_[1] }, | 
| 109 | 9 |  |  | 9 |  | 19 | valid   => sub{ check_money     $_[1] }, | 
| 110 | 9 | 100 |  | 9 |  | 36 | post    => sub{ defined         $_[1] ? 0.0 + $_[1] : undef }, | 
| 111 | 74 |  |  |  |  | 702 | ); | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | $app->vtype( | 
| 114 |  |  |  |  |  |  | percent     => | 
| 115 | 5 |  |  | 5 |  | 13 | pre     => sub{ parse_number    $_[1] }, | 
| 116 | 5 |  |  | 5 |  | 12 | valid   => sub{ check_percent   $_[1] }, | 
| 117 | 5 | 100 |  | 5 |  | 19 | post    => sub{ defined         $_[1] ? 0.0 + $_[1] : undef }, | 
| 118 | 74 |  |  |  |  | 769 | ); | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | $app->vtype( | 
| 121 |  |  |  |  |  |  | lon         => | 
| 122 | 4 |  |  | 4 |  | 10 | pre     => sub{ parse_number    $_[1] }, | 
| 123 | 4 |  |  | 4 |  | 9 | valid   => sub{ check_lon       $_[1] }, | 
| 124 | 4 | 100 |  | 4 |  | 12 | post    => sub{ defined         $_[1] ? 0.0 + $_[1] : undef }, | 
| 125 | 74 |  |  |  |  | 602 | ); | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | $app->vtype( | 
| 128 |  |  |  |  |  |  | lat         => | 
| 129 | 4 |  |  | 4 |  | 11 | pre     => sub{ parse_number    $_[1] }, | 
| 130 | 4 |  |  | 4 |  | 11 | valid   => sub{ check_lat       $_[1] }, | 
| 131 | 4 | 100 |  | 4 |  | 50 | post    => sub{ defined         $_[1] ? 0.0 + $_[1] : undef }, | 
| 132 | 74 |  |  |  |  | 692 | ); | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  |  | 
| 135 | 74 |  |  |  |  | 302 | return; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | 1; |