File: StubCountry.pm

package info (click to toggle)
libnumber-phone-perl 4.0008-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,772 kB
  • sloc: perl: 1,954; makefile: 6
file content (115 lines) | stat: -rw-r--r-- 3,295 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package Number::Phone::StubCountry;

use strict;
use warnings;
use Number::Phone::Country;

use I18N::LangTags::Detect;
use I18N::LangTags;

use base qw(Number::Phone);
our $VERSION = '1.5001';

=head1 NAME

Number::Phone::StubCountry - Base class for auto-generated country files

=cut

sub country_code {
    my $self = shift;

    return $self->{country_code};
}

sub country {
    my $self = shift;
    if(exists($self->{country})) { return $self->{country}; }
    ref($self)=~ /::(\w+?)$/;
    return $self->{country} = $1;
}

sub raw_number {
    my $self = shift;
    $self->{number};
}

sub is_valid {
  my $self = shift;
  if(exists($self->{is_valid})) {
      return $self->{is_valid};
  }
  foreach (map { "is_$_" } qw(specialrate geographic mobile pager tollfree personal ipphone)) {
    return $self->{is_valid} = 1 if($self->$_());
  }
  return $self->{is_valid} = 0;
}

sub is_geographic   { shift()->_validator('geographic'); }
sub is_fixed_line   { shift()->_validator('fixed_line'); }
sub is_mobile       { shift()->_validator('mobile'); }
sub is_pager        { shift()->_validator('pager'); }
sub is_personal     { shift()->_validator('personal_number'); }
sub is_specialrate  { shift()->_validator('specialrate'); }
sub is_tollfree     { shift()->_validator('toll_free'); }
sub is_ipphone      { shift()->_validator('voip'); }

sub _validator {
  my($self, $validator) = @_;
  $validator = $self->{validators}->{$validator};
  return undef unless($validator);
  return $self->raw_number() =~ /^($validator)$/x ? 1 : 0;
}

sub areaname {
    my $self      = shift;
    my @languages = @_;
    if(!@languages) { # nothing specifically asked for? use the locale
        @languages = I18N::LangTags::implicate_supers(I18N::LangTags::Detect::detect());
        if(!grep { $_ eq 'en' } @languages) {
            # and fall back to English
            push @languages, 'en'
        }
    }
    my $number = $self->raw_number();
    LANGUAGE: foreach my $language (@languages) {
        next LANGUAGE unless(exists($self->{areanames}->{$language}));
        my %map = %{$self->{areanames}->{$language}};
        foreach my $prefix (map { substr($number, 0, $_) } reverse(1..length($number))) {
            return $map{$self->country_code().$prefix} if exists($map{$self->country_code().$prefix});
        }
    }
    return undef;
}

sub format {
  my $self = shift;
  my $number = $self->raw_number();
  foreach my $formatter (@{$self->{formatters}}) {
    my($leading_digits, $pattern) = map { $formatter->{$_} } qw(leading_digits pattern);
    if((!$leading_digits || $number =~ /^($leading_digits)/x) && $number =~ /^$pattern$/x) {
      my @bits = $number =~ /^$pattern$/x;
      return join(' ', '+'.$self->country_code(), @bits);
    }
  }
  # if there's no formatters defined ...
  return '+'.$self->country_code().' '.$number;
}

sub timezones {
  my $self = shift;

  # If non-geographic use the country-level timezones
  my $number = $self->is_geographic() ? $self->raw_number() : $self->country_code();

  foreach my $i (reverse (0..length($number))) {
    if (my $timezones = $self->{timezones}->{substr($number, 0, $i)}) {
      my $copy = [sort { $a cmp $b } @$timezones]; # copy the list-ref to avoid manipulation
      return $copy;
    }
  }

  return undef;
}

1;