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 52     52   556150 use strict;
  52         145  
  52         1783  
4 52     52   305 use warnings;
  52         114  
  52         1488  
5              
6 52     52   1457 use Moose;
  52         682189  
  52         414  
7 52     52   354558 use namespace::autoclean;
  52         18481  
  52         469  
8              
9 52     52   5652 use overload '""' => \&_to_string, fallback => 1;
  52         133  
  52         661  
10              
11 52     52   6571 use WWW::LogicBoxes::Types qw( Str NumberPhone );
  52         134  
  52         438  
12              
13 52     52   563537 use Number::Phone;
  52         2130269  
  52         297  
14 52     52   2342 use Try::Tiny;
  52         138  
  52         3328  
15 52     52   360 use Carp;
  52         119  
  52         27502  
16              
17             our $VERSION = '1.11.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   8 my $self = shift;
77              
78 3         101 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         101 my $full_number = $self->_number_phone_obj->format;
87 3         129 $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   777 my $self = shift;
96              
97 3         96 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