DrHyde / perl-modules-Number-Phone

Number::Phone and friends
24 stars 32 forks source link

Support international prefixes #39

Open lejeunerenard opened 9 years ago

lejeunerenard commented 9 years ago

Currently when using the international prefixes 00 (ITU-T) or 011 (NANP), validation fails.

I propose one of two options:

  1. The simplest solution is to just remove the two common international prefixes (00 & 011) when creating a new number.
  2. The ideal solution is to also validate the international prefix. As there many prefixes (see the Wikipedia article) this would be no small task.

I recommend and would be willing to implement No 1.

References:

ITU-T International prefix definition ( Section 12 on page 14 in English version ) NANP International prefix definition via Wikipedia

DrHyde commented 9 years ago

You mean that you want to be able to do this ...

my $number = Number::Phone->new('011447979866975')

instead of ...

my $number = Number::Phone->new('+447979866975')

?

I don't like this. The various parsers scattered throughout the code are already complex and confusing, and I don't want to add more complexity, confusion, and possibility of inconsistency.

I think it would be better to have a class method on Number::Phone that you would invoke in a sub-class something like this ...

my $e123 = Number::Phone::NANP->dialstring_to_e123('01144...')

whose results you could pass to the constructor. Of course, you have to know where you're dialling from so that you can pick the right subclass, but I think that's a reasonable restriction. And it beats a partial implementation that will only work for some countries, and (if we're not careful) only some variations of the multitudinous constructors.

lejeunerenard commented 9 years ago

I agree 100%. Leaving it up to the developer to figure out where the number is coming from is reasonable, as long as the class method is named the same for all sub-classes.

Would it make sense to create a dialstring_to_e123() on the parent class Number::Phone that simply dies with an error like dialstring_to_e123() not implemented for __PACKAGE__? Similar to https://github.com/DrHyde/perl-modules-Number-Phone/blob/master/lib/Number/Phone.pm#L189. Or would it be better to infer the country with Number::Phone::Country::phone2country?

I can take a stab at making a PR for at least the Number::Phone::NANP sub-class if you'd like. Just let me know.

DrHyde commented 9 years ago

I suspect that it's best to put all the code in the Number::Phone base class, but only allow it to be invoked on a subclass. That way, the code only has to be written once for all countries. It should be pretty generic, as the country_code method will give you enough information to look up the idd code (using Number::Phone::Country::idd_code) that you need to strip off.

DrHyde commented 6 years ago

@lejeunerenard FYI I've merged @mimeraynham's PR referenced above.