File Coverage

blib/lib/Business/cXML/Address/Number.pm
Criterion Covered Total %
statement 43 43 100.0
branch 4 4 100.0
condition n/a
subroutine 12 12 100.0
pod 4 4 100.0
total 63 63 100.0


line stmt bran cond sub pod time code
1             =encoding utf-8
2              
3             =head1 NAME
4              
5             Business::cXML::Address::Number - cXML contact phone/fax number
6              
7             =head1 SYNOPSIS
8              
9             use Business::cXML::Address::Number;
10              
11             =head1 DESCRIPTION
12              
13             Object representation of a cXML phone or fax number. You can specify which
14             when calling C<new()> by passing I<C<$node>> set to C<Phone> (default) or
15             C<Fax>. Alternatively, you can also specify it with a C<_nodeName> in
16             C<new()>'s I<C<$properties>>.
17              
18             Specifically B<not> implemented are Faxes containing a URL or e-mail address
19             instead of a telephone number.
20              
21             =head1 METHODS
22              
23             See L<Business::cXML::Object/COMMON METHODS>, plus the following:
24              
25             =over
26              
27             =cut
28              
29 10     10   138 use 5.014;
  10         32  
30 10     10   41 use strict;
  10         17  
  10         352  
31              
32             use base qw(Business::cXML::Object);
33 10     10   47  
  10         23  
  10         684  
34             use constant NODENAME => 'Phone';
35 10     10   74 use constant PROPERTIES => (
  10         15  
  10         664  
36 10         622 name => undef,
37             country_iso => 'US',
38             country_code => '1',
39             area_code => '',
40             number => '',
41             extension => undef,
42             );
43 10     10   58  
  10         15  
44             use Business::cXML::Utils qw(to_numeric);
45 10     10   3391 use XML::LibXML::Ferry;
  10         45  
  10         620  
46 10     10   951  
  10         45598  
  10         3848  
47             my ($self, $val) = @_;
48             $val = $val->textContent;
49 75     75   1455 return to_numeric($val);
50 75         395 }
51 75         221  
52             my ($self, $el) = @_;
53            
54             $el->ferry($self, {
55 32     32 1 74 TelephoneNumber => {
56             CountryCode => {
57 32         357 isoCountryCode => 'country_iso',
58             __text => [ 'country_code', \&_numeric ],
59             },
60             AreaOrCityCode => [ 'area_code', \&_numeric ],
61             Number => [ 'number', \&_numeric ],
62             Extension => [ 'extension', \&_numeric ],
63             },
64             URL => '__UNIMPLEMENTED', # Fax only
65             Email => '__UNIMPLEMENTED', # Fax only
66             });
67             }
68              
69             my ($self, $doc) = @_;
70             my $node = $doc->create($self->{_nodeName}, undef, name => $self->{name});
71              
72             my $el = $node->add('TelephoneNumber');
73 19     19 1 137 $el->add('CountryCode', $self->{country_code}, isoCountryCode => $self->{country_iso});
74 19         65 $el->add('AreaOrCityCode', $self->{area_code});
75             $el->add('Number', $self->{number});
76 19         447 $el->add('Extension', $self->{extension}) if $self->{extension};
77 19         524  
78 19         972 return $node;
79 19         677 }
80 19 100       635  
81             =item C<B<toString>()>
82 19         231  
83             Returns the string representation of the phone number in C<C-AAA-NNN-NNNN>
84             format, without the extension. Only North-American formatting is currently
85             implemented.
86              
87             =cut
88              
89             my ($self) = @_;
90             my $n = $self->{number};
91             $n = join('-', unpack('A3A4', $n)) if (length($self->{number}) == 7);
92             return $self->{country_code} . '-' . $self->{area_code} . '-' . $n;
93             }
94 2     2 1 1156  
95 2         5 =item C<B<fromString>( I<$string> )>
96 2 100       15  
97 2         12 Extracts country code, area code and number from I<C<$string>>. (Not the
98             extension.) Non-numeric characters are safely discarded. Only North-American
99             formatting is currently implemented, with the benefit that the initial C<1> is
100             optional.
101              
102             Note: processing starts from the left, so any custom extension suffixes are
103             safely ignored.
104              
105             =cut
106              
107             my ($self, $str) = @_;
108             $str = to_numeric($str);
109             my ($area, $number) = $str =~ m/^1?(\d{3})(\d{7})/x;
110             $self->{country_code} = '1';
111             $self->{area_code} = $area;
112             $self->{number} = $number;
113 1     1 1 11 }
114 1         5  
115 1         8 =back
116 1         4  
117 1         3 =head1 PROPERTY METHODS
118 1         3  
119             See L<Business::cXML::Object/PROPERTY METHODS> for how the following operate.
120              
121             =over
122              
123             =item C<B<name>>
124              
125             Optional, name of this phone or fax number (i.e. C<work>). Without a name,
126             this represents a C<TelephoneNumber> without a wrapper C<Phone>/C<Fax>.
127              
128             =item C<B<country_iso>>
129              
130             2-letter ISO country code
131              
132             =item C<B<country_code>>
133              
134             Country code (default: C<1>)
135              
136             =item C<B<area_code>>
137              
138             Area code
139              
140             =item C<B<number>>
141              
142             Number
143              
144             =item C<B<extension>>
145              
146             Optional, extension
147              
148             =back
149              
150             =head1 AUTHOR
151              
152             Stéphane Lavergne L<https://github.com/vphantom>
153              
154             =head1 ACKNOWLEDGEMENTS
155              
156             Graph X Design Inc. L<https://www.gxd.ca/> sponsored this project.
157              
158             =head1 COPYRIGHT & LICENSE
159              
160             Copyright (c) 2017-2018 Stéphane Lavergne L<https://github.com/vphantom>
161              
162             Permission is hereby granted, free of charge, to any person obtaining a copy
163             of this software and associated documentation files (the "Software"), to deal
164             in the Software without restriction, including without limitation the rights
165             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
166             copies of the Software, and to permit persons to whom the Software is
167             furnished to do so, subject to the following conditions:
168              
169             The above copyright notice and this permission notice shall be included in all
170             copies or substantial portions of the Software.
171              
172             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
173             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
175             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
176             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
177             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
178             SOFTWARE.
179              
180             =cut
181              
182             1;