Skip to content
Browse files

Make unknown countries return something more useful than undef

  • Loading branch information...
1 parent 86d159d commit 9a7a34353d31c0bf85c224853f613405f568d38f @DrHyde DrHyde committed Sep 12, 2010
Showing with 50 additions and 14 deletions.
  1. +16 −1 lib/Number/Phone.pm
  2. +5 −4 lib/Number/Phone/Country.pm
  3. +16 −0 lib/Number/Phone/StubCountry.pm
  4. +13 −9 t/902_bugfix-rt61177.t
View
17 lib/Number/Phone.pm
@@ -5,6 +5,7 @@ use strict;
use Scalar::Util 'blessed';
use Number::Phone::Country qw(noexport uk);
+use Number::Phone::StubCountry;
our $VERSION = 1.7101;
@@ -129,10 +130,24 @@ sub new {
return undef unless($country);
$country = "NANP" if($number =~ /^\+1/);
eval "use Number::Phone::$country";
- return undef if($@);
+ return $class->_make_stub_object($number) if($@);
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
All Number::Phone classes should implement the following methods, both
View
9 lib/Number/Phone/Country.pm
@@ -2,7 +2,8 @@ package Number::Phone::Country;
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;
my $use_uk = 0;
@@ -20,7 +21,7 @@ sub import {
}
}
-my %idd_codes = (
+%idd_codes = (
# 1 => 'NANP',
20 => 'EG', 212 => 'MA', 213 => 'DZ', 216 => 'TN',
@@ -154,7 +155,7 @@ my %idd_codes = (
# - Country prefix
# - IDD prefix (for dialling from this country prefix to another)
# - NDD prefix (for dialling from one area of this country to another)
-my %prefix_codes = (
+%prefix_codes = (
'AD' => ['376', '00', undef], # Andorra
'AE' => ['971', '00', '0'], # United Arab Emirates
'AF' => [ '93', '00', '0'], # Afghanistan
@@ -404,7 +405,7 @@ sub phone2country {
sub phone2country_and_idd {
my ($phone) = @_;
- $phone =~ s/[^\d+]//g;
+ $phone =~ s/[^\+?\d+]//g;
$phone = '+1'.$phone unless(substr($phone, 0, 1) =~ /[1+]/);
$phone =~ s/\D//g;
View
16 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;
View
22 t/902_bugfix-rt61177.t
@@ -4,18 +4,22 @@ use strict;
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 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 ('', '+') {
- # can't really use a German number here, in case Number::Phone::DE
- # exists and is installed. Need a mocked country
- # FIXME
- my $object = Number::Phone->new($prefix."491774497319");
- 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?
+ my $object = Number::Phone->new($prefix."442087712924");
+ isa_ok($object, 'Number::Phone::StubCountry', "unknown countries return minimal objects".($prefix? " with a +" : ""));
+ is($object->country_code(), '44', "->country_code works");
+ is($object->format(), '+44 2087712924', "->format works");
+
}

0 comments on commit 9a7a343

Please sign in to comment.
Something went wrong with that request. Please try again.