File Coverage

blib/lib/CGI/Untaint/CountyStateProvince/GB.pm
Criterion Covered Total %
statement 33 33 100.0
branch 11 12 91.6
condition 3 6 50.0
subroutine 8 8 100.0
pod 2 2 100.0
total 57 61 93.4


line stmt bran cond sub pod time code
1             package CGI::Untaint::CountyStateProvince::GB;
2              
3 4     4   1496575 use warnings;
  4         13  
  4         260  
4 4     4   25 use strict;
  4         8  
  4         111  
5 4     4   2664 use Locale::SubCountry;
  4         461237  
  4         2373  
6 4     4   50 use Carp;
  4         20  
  4         570  
7              
8             # use base qw(CGI::Untaint::object CGI::Untaint::CountyStateProvince);
9 4     4   29 use base 'CGI::Untaint::object';
  4         7  
  4         2482  
10              
11             =head1 NAME
12              
13             CGI::Untaint::CountyStateProvince::GB - Add British counties to CGI::Untaint::CountyStateProvince tables
14              
15             =head1 VERSION
16              
17             Version 0.13
18              
19             =cut
20              
21             our $VERSION = '0.13';
22              
23             our %counties = (
24             'aberdeenshire' => 1,
25             'anglesey' => 1,
26             'angus' => 1,
27             'avon' => 1,
28             'ayrshire' => 1,
29             'bedfordshire' => 1,
30             'berkshire' => 1,
31             'blaenau gwent' => 1,
32             'brecknockshire' => 1,
33             'bridgend' => 1,
34             'buckinghamshire' => 1,
35             'borders' => 1,
36             'cardinganshire' => 1,
37             'caerphilly' => 1,
38             'cambridgeshire' => 1,
39             'cardiff' => 1,
40             'carmarthenshire' => 1,
41             'ceredigion' => 1,
42             'channel islands' => 1,
43             'cheshire' => 1,
44             'clackmannanshire' => 1,
45             'clywd' => 1,
46             'county durham' => 1,
47             'county tyrone' => 1,
48             'conwy' => 1,
49             'cornwall' => 1,
50             'cumbria' => 1,
51             'derbyshire' => 1,
52             'denbighshire' => 1,
53             'devon' => 1,
54             'dorset' => 1,
55             'dumfries and galloway' => 1,
56             'dyfed' => 1,
57             'east lothian' => 1,
58             'east sussex' => 1,
59             'east yorkshire' => 1,
60             'essex' => 1,
61             'fife' => 1,
62             'flintshire' => 1,
63             'glamorgan' => 1,
64             'gloucestershire' => 1,
65             'grampian' => 1,
66             'gwent' => 1,
67             'gwynedd' => 1,
68             'hampshire' => 1,
69             'hertfordshire' => 1,
70             'herefordshire' => 1,
71             'isle of man' => 1,
72             'isle of wight' => 1,
73             'kent' => 1,
74             'lancashire' => 1,
75             'leicestershire' => 1,
76             'lincolnshire' => 1,
77             'london' => 1,
78             'manchester' => 1,
79             'merioneth' => 1,
80             'merseyside' => 1,
81             'mid lothian' => 1,
82             'middlesex' => 1, # Doesn't exist anymore, but people like it
83             'monmouthshire' => 1,
84             'montgomeryshire' => 1,
85             'north yorkshire' => 1,
86             'norfolk' => 1,
87             'northamptonshire' => 1,
88             'northern ireland' => 1,
89             'northumberland' => 1,
90             'nottinghamshire' => 1,
91             'oxfordshire' => 1,
92             'pembrokeshire' => 1,
93             'powys' => 1,
94             'radnorshire' => 1,
95             'renfrewshire' => 1,
96             'shropshire' => 1,
97             'somerset' => 1,
98             'south yorkshire' => 1,
99             'staffordshire' => 1,
100             'strathclyde' => 1,
101             'suffolk' => 1,
102             'surrey' => 1,
103             'tayside' => 1,
104             'teesside' => 1,
105             'tyne and wear' => 1,
106             'warwickshire' => 1,
107             'west dunbartonshire' => 1,
108             'west lothian' => 1,
109             'west midlands' => 1,
110             'west sussex' => 1,
111             'west yorkshire' => 1,
112             'wiltshire' => 1,
113             'worcestershire' => 1,
114             );
115              
116             our %abbreviations = (
117             'beds' => 'bedfordshire',
118             'bucks' => 'buckinghamshire',
119             'cambs' => 'cambridgeshire',
120             'cleveland' => 'teesside',
121             'co durham' => 'county durham',
122             'durham' => 'county durham',
123             'east yorks' => 'east yorkshire',
124             'glasgow' => 'west lothian',
125             'gloucester' => 'gloucestershire',
126             'great london' => 'london',
127             'greater london' => 'london',
128             'hants' => 'hampshire',
129             'herts' => 'hertfordshire',
130             'lancs' => 'lancashire',
131             'lancs.' => 'lancashire',
132             'Lancs.' => 'lancashire',
133             'middx' => 'middlesex',
134             'n yorkshire' => 'north yorkshire',
135             'northants' => 'northamptonshire',
136             'notts' => 'nottinghamshire',
137             'oxon' => 'oxfordshire',
138             'greater manchester' => 'manchester',
139             's yorkshire' => 'south yorkshire',
140             's glos' => 'gloucestershire',
141             'south gloster' => 'gloucestershire',
142             'vale of glamorgan' => 'glamorgan',
143             'westmidlands' => 'west midlands',
144             'west mids' => 'west midlands',
145             'west yorks' => 'west yorkshire',
146             );
147              
148             =head1 SYNOPSIS
149              
150             Adds a list of British counties to the list of counties/states/provinces
151             which are known by the CGI::Untaint::CountyStateProvince validator so that
152             an HTML form sent by CGI contains a valid county.
153              
154             You must include CGI::Untaint::CountyStateProvince::GB after including
155             CGI::Untaint, otherwise it won't work.
156              
157             use CGI::Info;
158             use CGI::Untaint;
159             use CGI::Untaint::CountyStateProvince::GB;
160              
161             my $info = CGI::Info->new();
162             my $u = CGI::Untaint->new($info->params());
163             # Succeeds if state = 'Kent', fails if state = 'Queensland';
164             $u->extract(-as_CountyStateProvince => 'state');
165             # ...
166              
167             =cut
168              
169             =head1 SUBROUTINES/METHODS
170              
171             =head2 is_valid
172              
173             Validates the data. See CGI::Untaint::is_valid.
174              
175             =cut
176              
177             sub is_valid {
178 28     28 1 144 my $self = shift;
179              
180 28         73 my $value = lc($self->value());
181 28         198 $value =~ s/^\s+|\s+$//g; # Trim whitespace
182              
183 28 100       149 if($value =~ /([a-z][a-z\s]+)/) {
184 26         93 $value = $1;
185             } else {
186 2         8 return 0;
187             }
188              
189 26 100       97 if(exists($abbreviations{$value})) {
190 10         46 return $abbreviations{$value};
191             }
192              
193             # Attempt Locale::SubCountry validation
194             # Be aware of RT77735 - some
195             # counties are missing and some towns and unitary authorities are listed as counties
196 16   66     79 my $validator = $self->{_validator} ||= Locale::SubCountry->new('GB');
197 16 50 33     193 if($validator && (my $region_code = $validator->code($value))) {
198 16 100       18753 return $value if($region_code ne 'unknown');
199             }
200              
201 10 100       107 return exists($counties{$value}) ? $value : 0;
202             }
203              
204             =head2 value
205              
206             Sets the raw data to be validated.
207             Called by the superclass,
208             you are unlikely to want to call it.
209              
210             =cut
211              
212             sub value {
213 56     56 1 440401 my ($self, $value) = @_;
214              
215 56 100       174 if(defined($value)) {
216 28         124 $self->{value} = $value;
217             }
218              
219 56         183 return $self->{value};
220             }
221              
222             BEGIN {
223 4     4   3635 my $gb = CGI::Untaint::CountyStateProvince::GB->_new();
224              
225 4         260 push @CGI::Untaint::CountyStateProvince::countries, $gb;
226             };
227              
228             =head1 AUTHOR
229              
230             Nigel Horne, C<< >>
231              
232             =head1 BUGS
233              
234             Please report any bugs or feature requests to C
235             or through the web interface at L.
236             I will be notified,
237             and then you'll
238             automatically be notified of the progress on your bug as I make changes.
239              
240             =head1 SEE ALSO
241              
242             L, L, L
243              
244             =head1 SUPPORT
245              
246             You can find documentation for this module with the perldoc command.
247              
248             perldoc CGI::Untaint::CountyStateProvince::GB
249              
250             You can also look for information at:
251              
252             =over 4
253              
254             =item * RT: CPAN's request tracker
255              
256             L
257              
258             =item * Search CPAN
259              
260             L
261              
262             =back
263              
264             =head1 ACKNOWLEDGEMENTS
265              
266             =head1 LICENSE AND COPYRIGHT
267              
268             Copyright 2012-2025 Nigel Horne.
269              
270             This program is released under the following licence: GPL2
271              
272             =cut
273              
274             1; # End of CGI::Untaint::CountyStateProvince::GB