« blib/lib/WWW/LogicBoxes/Role/Command/Domain/Registration.pm
↑ Summary
blib/lib/WWW/LogicBoxes/DomainAvailability.pm »
| 1 | package WWW::LogicBoxes::PhoneNumber; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 48 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 48 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 48 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 48 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 48 | ▶ | use overload '""' => \&_to_string, fallback => 1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 48 | ▶ | use WWW::LogicBoxes::Types qw( Str NumberPhone ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 48 | ▶ | use Number::Phone; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 48 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 48 | our $VERSION = '1.2.0'; # VERSION | |||||||||||||
| 17 | # ABSTRACT: Extendes Number::Phone to add 'number' functionatly (without country code) | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 48 | has '_number_phone_obj' => ( | |||||||||||||
| 20 | is => 'ro', | ||||||||||||||
| 21 | isa => NumberPhone, | ||||||||||||||
| 22 | required => 1, | ||||||||||||||
| 23 | ); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 48 | has 'country_code' => ( | |||||||||||||
| 26 | is => 'ro', | ||||||||||||||
| 27 | isa => Str, | ||||||||||||||
| 28 | builder => '_build_country_code', | ||||||||||||||
| 29 | lazy => 1, | ||||||||||||||
| 30 | ); | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 48 | has 'number' => ( | |||||||||||||
| 33 | is => 'ro', | ||||||||||||||
| 34 | isa => Str, | ||||||||||||||
| 35 | builder => '_build_number', | ||||||||||||||
| 36 | lazy => 1, | ||||||||||||||
| 37 | ); | ||||||||||||||
| 38 | |||||||||||||||
| 39 | around BUILDARGS => sub { | ||||||||||||||
| 40 | 3 | ▶ | my $orig = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||
| 41 | 3 | my $class = shift; | |||||||||||||
| 42 | |||||||||||||||
| 43 | 3 | my $args; | |||||||||||||
| 44 | 3 | ▶ | if( scalar @_ > 1 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 1 | return $class->$orig( @_ ); | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | else { | ||||||||||||||
| 48 | 2 | $args = shift; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 2 | ▶ | if( ref $args eq '' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 52 | 1 | return $class->$orig( _number_phone_obj => Number::Phone->new( $args ) ); | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | elsif( ( ref $args ) =~ m/Number::Phone/ ) { | ||||||||||||||
| 55 | 1 | return $class->$orig( _number_phone_obj => $args ); | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | else { | ||||||||||||||
| 58 | 0 | croak "Invalid params passed to $class"; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | 48 | }; | |||||||||||||
| 61 | |||||||||||||||
| 62 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||
| 63 | sub _build_country_code { | ||||||||||||||
| 64 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_country_code: called CC=1 SCAR=0.0
| |||||||||||||||
| 65 | |||||||||||||||
| 66 | 3 | return $self->_number_phone_obj->country_code; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | ## use critic | ||||||||||||||
| 69 | |||||||||||||||
| 70 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||
| 71 | sub _build_number { | ||||||||||||||
| 72 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_number: called CC=1 SCAR=0.0
| |||||||||||||||
| 73 | |||||||||||||||
| 74 | 3 | my $full_number = $self->_number_phone_obj->format; | |||||||||||||
| 75 | 3 | $full_number =~ s/[^\d]*//g; | |||||||||||||
| 76 | |||||||||||||||
| 77 | 3 | return substr( $full_number, length( $self->country_code ) ); | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | ## use critic | ||||||||||||||
| 80 | |||||||||||||||
| 81 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||
| 82 | sub _to_string { | ||||||||||||||
| 83 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_to_string: called CC=1 SCAR=0.0
| |||||||||||||||
| 84 | |||||||||||||||
| 85 | 3 | return $self->country_code . $self->number; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | ## use critic | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 48 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 90 | |||||||||||||||
| 91 | 48 | 1; | |||||||||||||
| 92 | |||||||||||||||
| 93 | __END__ | ||||||||||||||
| 94 | =pod | ||||||||||||||
| 95 | |||||||||||||||
| 96 | =head1 NAME | ||||||||||||||
| 97 | |||||||||||||||
| 98 | WWW::LogicBoxes::PhoneNumber - PhoneNumber object with LogicBoxes Compatable Formatting | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =head1 SYNOPSIS | ||||||||||||||
| 101 | |||||||||||||||
| 102 | use WWW::LogicBoxes::PhoneNumber; | ||||||||||||||
| 103 | |||||||||||||||
| 104 | my $phone_number = WWW::LogicBoxes::PhoneNumber->new( '+18005551212' ); | ||||||||||||||
| 105 | |||||||||||||||
| 106 | |||||||||||||||
| 107 | use Number::Phone; | ||||||||||||||
| 108 | use WWW::LogicBoxes::PhoneNumber; | ||||||||||||||
| 109 | |||||||||||||||
| 110 | my $number_phone = Number::Phone->new( '+18005551212' ); | ||||||||||||||
| 111 | my $phone_number = WWW::LogicBoxes::PhoneNumber->new( $number_phone ); | ||||||||||||||
| 112 | |||||||||||||||
| 113 | print "My Phone Number is $phone_number \n"; | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =head1 DESCRIPTION | ||||||||||||||
| 116 | |||||||||||||||
| 117 | While L<Number::Phone> is an excellent library, there is a bit of inconsistency when it comes to international phone numbers and how to get the full number back out from the L<Number::Phone> object. The purpose of this object is to abstract away those difficulties and provide a uniform way to work with phone numbers. | ||||||||||||||
| 118 | |||||||||||||||
| 119 | L<LogicBoxes|http://www.logicboxes.com> is rather picky when it comes to phone numbers and this library exists to address that. While you certainly can use this object as a consumer it should be noted that coercions and overloads exist so that it's not needed. Anywhere that excepts a PhoneNumber will take a string or Number::Phone object and automagically convert it into a WWW::LogicBoxes::PhoneNumber. When used as a string the full phone number is returned. | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =head1 ATTRIBUTES | ||||||||||||||
| 122 | |||||||||||||||
| 123 | =head2 B<_number_phone_obj> | ||||||||||||||
| 124 | |||||||||||||||
| 125 | This private attribute contains an instance of L<Number::Phone>. While you certainly can set it in the call to new, it's not nessicary as an around BUILDARGS will take the values passed to new and ensure this attribute is set. | ||||||||||||||
| 126 | |||||||||||||||
| 127 | There really is no reason to do anything with this attribute. | ||||||||||||||
| 128 | |||||||||||||||
| 129 | =head2 country_code | ||||||||||||||
| 130 | |||||||||||||||
| 131 | Lazy built country code assoicated with the phone number. | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =head2 number | ||||||||||||||
| 134 | |||||||||||||||
| 135 | Lazy built party number (the phone number without the country code) assoicated with the phone number. | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =cut | ||||||||||||||