File Coverage

blib/lib/Rose/HTML/Form/Field/PhoneNumber/US/Split.pm
Criterion Covered Total %
statement 31 33 93.9
branch 4 6 66.6
condition 0 6 0.0
subroutine 10 10 100.0
pod 5 5 100.0
total 50 60 83.3


line stmt bran cond sub pod time code
1              
2             use strict;
3 2     2   1019  
  2         4  
  2         49  
4             use Carp();
5 2     2   8  
  2         4  
  2         28  
6             use Rose::HTML::Form::Field::Text;
7 2     2   8  
  2         4  
  2         21  
8             use base qw(Rose::HTML::Form::Field::Compound
9 2         721 Rose::HTML::Form::Field::PhoneNumber::US);
10 2     2   40  
  2         6  
11             # Multiple inheritence never quite works out the way I want it to...
12             Rose::HTML::Form::Field::PhoneNumber::US->import_methods
13             (
14             'validate',
15             'inflate_value',
16             'deflate_value',
17             );
18              
19             Rose::HTML::Form::Field::Compound->import_methods
20             (
21             'name',
22             );
23              
24             our $VERSION = '0.606';
25              
26             {
27             my($self) = shift;
28              
29 1     1 1 2 $self->add_fields
30             (
31 1         13 area_code => { type => 'text', size => 3, maxlength => 3, class => 'area-code' },
32             exchange => { type => 'text', size => 3, maxlength => 3, class => 'exchange' },
33             number => { type => 'text', size => 4, maxlength => 4, class => 'number' },
34             );
35             }
36              
37             {
38             my($self, $value) = @_;
39              
40             return undef unless(defined $value);
41 17     17 1 28  
42             my $phone = $self->inflate_value($value);
43 17 100       49  
44             unless($phone)
45 5         17 {
46             $phone =~ s/[- ]+//g;
47 5 50       11  
48             no warnings;
49 0         0 return
50             {
51 2     2   15 area_code => substr($phone, 0, 3) || '',
  2         3  
  2         601  
52             exchange => substr($phone, 3, 6) || '',
53             number => substr($phone, 6, 4) || '',
54 0   0     0 }
      0        
      0        
55             }
56              
57             $phone =~ /^(\d{3})-(\d{3})-(\d{4})$/;
58              
59              
60 5         19 return
61             {
62             area_code => $1,
63             exchange => $2,
64             number => $3,
65 5         44 };
66             }
67              
68             {
69             my($self) = shift;
70             return join('-', map { defined($_) ? $_ : '' }
71             map { $self->field($_)->internal_value } qw(area_code exchange number));
72             }
73 1     1 1 2  
74 3 50       11 {
75 1         3 my($self) = shift;
  3         9  
76              
77             return '<span class="phone">' .
78             $self->field('area_code')->html_field . '-' .
79             $self->field('exchange')->html_field . '-' .
80 2     2 1 381 $self->field('number')->html_field .
81             '</span>';
82 2         7 }
83              
84             {
85             my($self) = shift;
86              
87             return '<span class="phone">' .
88             $self->field('area_code')->xhtml_field . '-' .
89             $self->field('exchange')->xhtml_field . '-' .
90             $self->field('number')->xhtml_field .
91 1     1 1 3 '</span>';
92             }
93 1         5  
94             1;
95              
96              
97             =head1 NAME
98              
99             Rose::HTML::Form::Field::PhoneNumber::US::Split - Compound field for US phone numbers with separate fields for area code, exchange, and number.
100              
101             =head1 SYNOPSIS
102              
103             $field =
104             Rose::HTML::Form::Field::PhoneNumber::US::Split->new(
105             label => 'Phone',
106             name => 'phone',
107             default => '123-321-1234');
108              
109             print $field->field('area_code')->internal_value; # "123"
110              
111             $field->input_value('555-5555');
112              
113             # "Phone number must be 10 digits, including area code"
114             $field->validate or warn $field->error;
115              
116             $field->input_value('(555) 456-7890');
117              
118             print $field->field('exchange')->internal_value; # "456"
119             print $field->internal_value; # "555-456-7890"
120              
121             print $field->html;
122             ...
123              
124             =head1 DESCRIPTION
125              
126             L<Rose::HTML::Form::Field::PhoneNumber::US::Split> is a compound field that contains three separate text fields for US phone numbers: one each for area code, exchange, and number. It inherits from both L<Rose::HTML::Form::Field::PhoneNumber::US> and L<Rose::HTML::Form::Field::Compound>. It overrides the following methods: L<build_field()|Rose::HTML::Form::Field::Compound/build_field>, L<coalesce_value()|Rose::HTML::Form::Field::Compound/coalesce_value>, L<decompose_value()|Rose::HTML::Form::Field::Compound/decompose_value>, L<html_field()|Rose::HTML::Form::Field/html_field>, and L<xhtml_field()|Rose::HTML::Form::Field/xhtml_field>.
127              
128             This is a good example of a compound field that combines separate fields into a single value through simple concatenation (plus a separator character). By inheriting from L<Rose::HTML::Form::Field::PhoneNumber::US>, it gets the validation and inflate/deflate features "for free", leaving it to concentrate on the coalesce/decompose features and the building and printing of the separate fields that make up the compound field.
129              
130             It is important that this class inherits from L<Rose::HTML::Form::Field::Compound>. See the L<Rose::HTML::Form::Field::Compound> documentation for more information.
131              
132             =head1 SEE ALSO
133              
134             Other examples of custom fields:
135              
136             =over 4
137              
138             =item L<Rose::HTML::Form::Field::Email>
139              
140             A text field that only accepts valid email addresses.
141              
142             =item L<Rose::HTML::Form::Field::Time>
143              
144             Uses inflate/deflate to coerce input into a fixed format.
145              
146             =item L<Rose::HTML::Form::Field::DateTime>
147              
148             Uses inflate/deflate to convert input to a L<DateTime> object.
149              
150             =item L<Rose::HTML::Form::Field::DateTime::Range>
151              
152             A compound field whose internal value consists of more than one object.
153              
154             =item L<Rose::HTML::Form::Field::DateTime::Split::MonthDayYear>
155              
156             A compound field that uses inflate/deflate convert input from multiple subfields into a L<DateTime> object.
157              
158             =item L<Rose::HTML::Form::Field::DateTime::Split::MDYHMS>
159              
160             A compound field that includes other compound fields and uses inflate/deflate convert input from multiple subfields into a L<DateTime> object.
161              
162             =back
163              
164             =head1 AUTHOR
165              
166             John C. Siracusa (siracusa@gmail.com)
167              
168             =head1 LICENSE
169              
170             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.