File Coverage

blib/lib/Number/Phone/NO.pm
Criterion Covered Total %
statement 60 67 89.5
branch 16 26 61.5
condition 19 42 45.2
subroutine 14 16 87.5
pod 5 5 100.0
total 114 156 73.0


line stmt bran cond sub pod time code
1             package Number::Phone::NO;
2             {
3             $Number::Phone::NO::VERSION = '0.11';
4             }
5              
6             # ABSTRACT: Number::Phone country package for NO (Norway)
7              
8 4     4   1230141 use warnings;
  4         11  
  4         377  
9 4     4   26 use strict;
  4         7  
  4         225  
10 4     4   26 use Carp;
  4         9  
  4         459  
11              
12 4     4   24 use base qw(Number::Phone::StubCountry::NO);
  4         10  
  4         5134  
13 4     4   363985 use Scalar::Util 'blessed';
  4         9  
  4         237  
14 4     4   23 use Number::Phone 2.2;
  4         124  
  4         31  
15 4     4   61365 use Number::Phone::NO::Data;
  4         14  
  4         919  
16              
17              
18             my $cache = {};
19 3     3 1 43 sub country_code { 47; }
20              
21             sub subscriber {
22 5     5 1 21 my $self = shift;
23 5 100 66     45 unless (blessed($self) && $self->isa(__PACKAGE__)) {
24 2         820 carp "Calling as package sub is deprecated, call as object methods";
25 2         25 $self = __PACKAGE__->new($self)
26             }
27 5 50 66     1259 return unless ($self and blessed($self) and $self->isa(__PACKAGE__));
      66        
28 4         14 my $parsed_number = $self->{number};
29 4         16 $parsed_number =~ s/[^0-9+]//g; # strip non-digits/plusses
30 4         9 $parsed_number =~ s/^\+47//; # remove leading +47
31 4         60 return $parsed_number;
32             }
33             sub regulator {
34 0     0 1 0 'Post- og teletilsynet, http://www.npt.no/';
35             }
36             foreach my $is (qw(
37             fixed_line geographic network_service tollfree corporate
38             personal pager mobile specialrate adult allocated ipphone
39             )) {
40 4     4   37 no strict 'refs'; ## no critic
  4         8  
  4         603  
41             *{__PACKAGE__."::is_$is"} = sub {
42 10     10   9619 my $self = shift;
43 10 100 66     73 unless (blessed($self) && $self->isa(__PACKAGE__)) {
44 3         988 carp "Calling as package sub is deprecated, call as object methods";
45 3         23 $self = __PACKAGE__->new($self)
46             }
47 10 50 33     3291 return unless ($self and blessed($self) and $self->isa(__PACKAGE__));
      33        
48 10 50 33     66 $self && $cache->{$self->{number}} ? $cache->{$self->{number}}->{"is_$is"} : undef;
49             }
50             }
51              
52             foreach my $method (qw(operator areacode areaname location )) {
53 4     4   19 no strict 'refs'; ## no critic
  4         9  
  4         1637  
54             *{__PACKAGE__."::$method"} = sub {
55 0     0   0 my $self = shift;
56 0 0 0     0 unless (blessed($self) && $self->isa(__PACKAGE__)) {
57 0         0 carp "Calling as package sub is deprecated, call as object methods";
58 0         0 $self = __PACKAGE__->new($self)
59             }
60 0 0 0     0 return unless ($self and blessed($self) and $self->isa(__PACKAGE__));
      0        
61 0         0 return $cache->{$self->{number}}->{$method};
62             }
63             }
64              
65             sub is_valid {
66 13     13 1 10881 my ($number) = @_;
67 13 50 66     233 return 1 if (
      66        
68             blessed($number)
69             && $number->isa(__PACKAGE__)
70             && $cache->{$number}
71             );
72 13 100       73 $number = $number->{number} if blessed($number);
73              
74 13 100       67 return 1 if($cache->{$number}->{is_valid});
75              
76 11         46 my $parsed_number = $number;
77 11         39 $parsed_number =~ s/[^0-9+]//g; # strip non-digits/plusses
78 11         33 $parsed_number =~ s/^\+47//; # remove leading +47
79              
80              
81             # a norwegian phone number can be one of two forms:
82             # +4712345678
83             # 12345678
84              
85 11         49 my $bucket = Number::Phone::NO::Data::lookup($parsed_number);
86              
87 11         2939 $cache->{$number} = $bucket;
88              
89 11         484 return $cache->{$number}->{is_valid};
90             }
91              
92             sub format {
93 3     3 1 3008 my $self = shift;
94 3 50 33     36 $self = (blessed($self) && $self->isa(__PACKAGE__)) ?
95             $self :
96             __PACKAGE__->new($self);
97 3         14 my $nr = $self->subscriber();
98 3         21 my @digits = split(//, $nr);
99 3 100 100     17 my $format = ($self->is_mobile || $self->is_specialrate || $self->is_tollfree
100             ? "%d%d%d %d%d %d%d%d"
101             : "%d%d %d%d %d%d %d%d");
102 3 50       3059 $format = "%d"x scalar(@digits) unless (scalar(@digits) == 8);
103              
104             # warn "format: $format, " . join(", ", @digits);
105 3         10 return '+'.country_code() . " " . sprintf($format, @digits);
106             }
107             1; # Magic true value required at end of module
108              
109             __END__