File Coverage

blib/lib/WWW/LogicBoxes/PhoneNumber.pm
Criterion Covered Total %
statement 35 35 100.0
branch n/a
condition n/a
subroutine 12 12 100.0
pod n/a
total 47 47 100.0


line stmt bran cond sub pod time code
1             package WWW::LogicBoxes::PhoneNumber;
2              
3 51     51   660513 use strict;
  51         122  
  51         1557  
4 51     51   281 use warnings;
  51         116  
  51         1294  
5              
6 51     51   1343 use Moose;
  51         661950  
  51         349  
7 51     51   324331 use namespace::autoclean;
  51         16876  
  51         373  
8              
9 51     51   4889 use overload '""' => \&_to_string, fallback => 1;
  51         135  
  51         563  
10              
11 51     51   5343 use WWW::LogicBoxes::Types qw( Str NumberPhone );
  51         131  
  51         398  
12              
13 51     51   512776 use Number::Phone;
  51         1496812  
  51         295  
14 51     51   1975 use Try::Tiny;
  51         114  
  51         3088  
15 51     51   336 use Carp;
  51         111  
  51         23248  
16              
17             our $VERSION = '1.10.0'; # VERSION
18             # ABSTRACT: Extendes Number::Phone to add 'number' functionatly (without country code)
19              
20             has '_number_phone_obj' => (
21             is => 'ro',
22             isa => NumberPhone,
23             required => 1,
24             );
25              
26             has 'country_code' => (
27             is => 'ro',
28             isa => Str,
29             builder => '_build_country_code',
30             lazy => 1,
31             );
32              
33             has 'number' => (
34             is => 'ro',
35             isa => Str,
36             builder => '_build_number',
37             lazy => 1,
38             );
39              
40             around BUILDARGS => sub {
41             my $orig = shift;
42             my $class = shift;
43              
44             my $args;
45             if( scalar @_ > 1 ) {
46             return $class->$orig( @_ );
47             }
48             else {
49             $args = shift;
50             }
51              
52             if( ref $args eq '' ) {
53             # Compensate for cases where the country code is present in both the
54             # country code field AND the phone number itself
55             my $number_phone;
56             for ( my $offset = 0; $offset < length $args && $offset < 4; $offset++ ) {
57             $number_phone = Number::Phone->new( substr( $args, $offset ) );
58              
59             if( $number_phone ) {
60             last;
61             }
62             }
63              
64             return $class->$orig( _number_phone_obj => $number_phone );
65             }
66             elsif( ( ref $args ) =~ m/Number::Phone/ ) {
67             return $class->$orig( _number_phone_obj => $args );
68             }
69             else {
70             croak "Invalid params passed to $class";
71             }
72             };
73              
74             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
75             sub _build_country_code {
76 3     3   6 my $self = shift;
77              
78 3         97 return $self->_number_phone_obj->country_code;
79             }
80             ## use critic
81              
82             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
83             sub _build_number {
84 3     3   6 my $self = shift;
85              
86 3         98 my $full_number = $self->_number_phone_obj->format;
87 3         111 $full_number =~ s/[^\d]*//g;
88              
89 3         94 return substr( $full_number, length( $self->country_code ) );
90             }
91             ## use critic
92              
93             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
94             sub _to_string {
95 3     3   559 my $self = shift;
96              
97 3         92 return $self->country_code . $self->number;
98             }
99             ## use critic
100              
101             __PACKAGE__->meta->make_immutable;
102              
103             1;
104              
105             __END__
106             =pod
107              
108             =head1 NAME
109              
110             WWW::LogicBoxes::PhoneNumber - PhoneNumber object with LogicBoxes Compatable Formatting
111              
112             =head1 SYNOPSIS
113              
114             use WWW::LogicBoxes::PhoneNumber;
115              
116             my $phone_number = WWW::LogicBoxes::PhoneNumber->new( '+18005551212' );
117              
118              
119             use Number::Phone;
120             use WWW::LogicBoxes::PhoneNumber;
121              
122             my $number_phone = Number::Phone->new( '+18005551212' );
123             my $phone_number = WWW::LogicBoxes::PhoneNumber->new( $number_phone );
124              
125             print "My Phone Number is $phone_number \n";
126              
127             =head1 DESCRIPTION
128              
129             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.
130              
131             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.
132              
133             =head1 ATTRIBUTES
134              
135             =head2 B<_number_phone_obj>
136              
137             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.
138              
139             There really is no reason to do anything with this attribute.
140              
141             =head2 country_code
142              
143             Lazy built country code assoicated with the phone number.
144              
145             =head2 number
146              
147             Lazy built party number (the phone number without the country code) assoicated with the phone number.
148              
149             =cut