Skip to content

Commit

Permalink
Make unknown countries return something more useful than undef
Browse files Browse the repository at this point in the history
  • Loading branch information
DrHyde committed Sep 12, 2010
1 parent 86d159d commit 9a7a343
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
17 changes: 16 additions & 1 deletion lib/Number/Phone.pm
Expand Up @@ -5,6 +5,7 @@ use strict;
use Scalar::Util 'blessed'; use Scalar::Util 'blessed';


use Number::Phone::Country qw(noexport uk); use Number::Phone::Country qw(noexport uk);
use Number::Phone::StubCountry;


our $VERSION = 1.7101; our $VERSION = 1.7101;


Expand Down Expand Up @@ -129,10 +130,24 @@ sub new {
return undef unless($country); return undef unless($country);
$country = "NANP" if($number =~ /^\+1/); $country = "NANP" if($number =~ /^\+1/);
eval "use Number::Phone::$country"; eval "use Number::Phone::$country";
return undef if($@); return $class->_make_stub_object($number) if($@);
return "Number::Phone::$country"->new($number); return "Number::Phone::$country"->new($number);
} }


sub _make_stub_object {
my $class = shift;
my $number = shift;
my $self = {
country => 'STUBFORCOUNTRYWITHNOMODULE',
country_idd_code => ''.Number::Phone::Country::country_code(Number::Phone::Country::phone2country($number)),
country_code => ''.Number::Phone::Country::phone2country($number),
number => $number
};
# use Data::Dumper; local $Data::Dumper::Indent = 1;
# print Dumper($self);
bless($self, 'Number::Phone::StubCountry');
}

=head1 METHODS =head1 METHODS
All Number::Phone classes should implement the following methods, both All Number::Phone classes should implement the following methods, both
Expand Down
9 changes: 5 additions & 4 deletions lib/Number/Phone/Country.pm
Expand Up @@ -2,7 +2,8 @@ package Number::Phone::Country;


use strict; use strict;


use vars qw($VERSION); # *_codes are global so we can mock in some tests
use vars qw($VERSION %idd_codes %prefix_codes);
$VERSION = 1.6001; $VERSION = 1.6001;
my $use_uk = 0; my $use_uk = 0;


Expand All @@ -20,7 +21,7 @@ sub import {
} }
} }


my %idd_codes = ( %idd_codes = (
# 1 => 'NANP', # 1 => 'NANP',


20 => 'EG', 212 => 'MA', 213 => 'DZ', 216 => 'TN', 20 => 'EG', 212 => 'MA', 213 => 'DZ', 216 => 'TN',
Expand Down Expand Up @@ -154,7 +155,7 @@ my %idd_codes = (
# - Country prefix # - Country prefix
# - IDD prefix (for dialling from this country prefix to another) # - IDD prefix (for dialling from this country prefix to another)
# - NDD prefix (for dialling from one area of this country to another) # - NDD prefix (for dialling from one area of this country to another)
my %prefix_codes = ( %prefix_codes = (
'AD' => ['376', '00', undef], # Andorra 'AD' => ['376', '00', undef], # Andorra
'AE' => ['971', '00', '0'], # United Arab Emirates 'AE' => ['971', '00', '0'], # United Arab Emirates
'AF' => [ '93', '00', '0'], # Afghanistan 'AF' => [ '93', '00', '0'], # Afghanistan
Expand Down Expand Up @@ -404,7 +405,7 @@ sub phone2country {


sub phone2country_and_idd { sub phone2country_and_idd {
my ($phone) = @_; my ($phone) = @_;
$phone =~ s/[^\d+]//g; $phone =~ s/[^\+?\d+]//g;
$phone = '+1'.$phone unless(substr($phone, 0, 1) =~ /[1+]/); $phone = '+1'.$phone unless(substr($phone, 0, 1) =~ /[1+]/);
$phone =~ s/\D//g; $phone =~ s/\D//g;


Expand Down
16 changes: 16 additions & 0 deletions lib/Number/Phone/StubCountry.pm
@@ -0,0 +1,16 @@
package Number::Phone::StubCountry;

use strict;
use warnings;
use base qw(Number::Phone);

sub country_code { return shift()->{country_idd_code} }
sub format {
my $self = shift;
my $number = $self->{number};
my $cc = $self->country_code();
$number =~ s/^(\+$cc)/$1 /;
return $number
}

1;
22 changes: 13 additions & 9 deletions t/902_bugfix-rt61177.t
Expand Up @@ -4,18 +4,22 @@ use strict;


use Number::Phone; use Number::Phone;


use Test::More tests => 6; use Test::More tests => 9;

use Number::Phone::Country;


ok(Number::Phone->new("442087712924")->country_code() == 44, "known countries return objects"); ok(Number::Phone->new("442087712924")->country_code() == 44, "known countries return objects");
ok(Number::Phone->new("+442087712924")->country_code() == 44, "known countries with a + return objects"); ok(Number::Phone->new("+442087712924")->country_code() == 44, "known countries with a + return objects");
ok(Number::Phone->new("+442087712924")->format() eq '+44 20 87712924' , "known countries with a + return objects");

# let's break the UK
$Number::Phone::Country::idd_codes{'44'} = 'MOCK';
$Number::Phone::Country::prefix_codes{'MOCK'} = ['44', '00', undef];


foreach my $prefix ('', '+') { foreach my $prefix ('', '+') {
# can't really use a German number here, in case Number::Phone::DE my $object = Number::Phone->new($prefix."442087712924");
# exists and is installed. Need a mocked country isa_ok($object, 'Number::Phone::StubCountry', "unknown countries return minimal objects".($prefix? " with a +" : ""));
# FIXME is($object->country_code(), '44', "->country_code works");
my $object = Number::Phone->new($prefix."491774497319"); is($object->format(), '+44 2087712924', "->format works");
isa_ok($object, 'Number::Phone', "unknown countries return minimal objects".($prefix? " with a +" : ""));
ok($object->country_code() == 49, "->country_code works");

# FIXME test other stuff like format(). what else?
} }

0 comments on commit 9a7a343

Please sign in to comment.