File Coverage

blib/lib/User/Identity.pm
Criterion Covered Total %
statement 77 101 76.2
branch 29 58 50.0
condition 8 18 44.4
subroutine 22 25 88.0
pod 18 19 94.7
total 154 221 69.6


line stmt bran cond sub pod time code
1             # Copyrights 2003-2023 by [Mark Overmeer ].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.03.
5             # This code is part of distribution Math-Polygon. Meta-POD processed with
6             # OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package User::Identity;
10 3     3   67528 use vars '$VERSION';
  3         7  
  3         155  
11             $VERSION = '1.02';
12              
13 3     3   17 use base 'User::Identity::Item';
  3         5  
  3         609  
14              
15 3     3   18 use strict;
  3         6  
  3         89  
16 3     3   25 use warnings;
  3         5  
  3         69  
17              
18 3     3   23 use Carp;
  3         6  
  3         182  
19              
20              
21 3     3   3548 use overload '""' => 'fullName';
  3         2976  
  3         18  
22              
23             #-----------------------------------------
24              
25              
26             my @attributes = qw/charset courtesy birth full_name formal_name
27             firstname gender initials language nickname prefix surname titles /;
28              
29             sub init($)
30 7     7 0 17 { my ($self, $args) = @_;
31              
32             exists $args->{$_} && ($self->{'UI_'.$_} = delete $args->{$_})
33 7   100     193 foreach @attributes;
34              
35 7         33 $self->SUPER::init($args);
36             }
37              
38             sub type() { 'user' }
39              
40 4     4 1 18 sub user() { shift }
41              
42              
43 1 50   1 1 8 sub charset() { shift->{UI_charset} || $ENV{LC_CTYPE} }
44              
45              
46             sub nickname()
47 5     5 1 889 { my $self = shift;
48 5 50       21 $self->{UI_nickname} || $self->name;
49             # TBI: If OS-specific info exists, then username
50             }
51              
52              
53             sub firstname()
54 9     9 1 15 { my $self = shift;
55 9 100       41 $self->{UI_firstname} || ucfirst $self->nickname;
56             }
57              
58              
59             sub initials()
60 6     6 1 290 { my $self = shift;
61             return $self->{UI_initials}
62 6 100       20 if defined $self->{UI_initials};
63              
64 4 50       9 if(my $firstname = $self->firstname)
65 4         7 { my $i = '';
66 4         27 while( $firstname =~ m/(\w+)(\-)?/g )
67 12         32 { my ($part, $connect) = ($1,$2);
68 12   100     38 $connect ||= '.';
69 12         31 $part =~ m/^(chr|th|\w)/i;
70 12         46 $i .= ucfirst(lc $1).$connect;
71             }
72 4         19 return $i;
73             }
74             }
75              
76              
77 1     1 1 4 sub prefix() { shift->{UI_prefix} }
78              
79              
80 1     1 1 5 sub surname() { shift->{UI_surname} }
81              
82              
83             sub fullName()
84 10     10 1 28 { my $self = shift;
85              
86             return $self->{UI_full_name}
87 10 50       28 if defined $self->{UI_full_name};
88              
89             my ($first, $prefix, $surname)
90 10         28 = @$self{ qw/UI_firstname UI_prefix UI_surname/};
91              
92 10 50 66     38 $surname = ucfirst $self->nickname if defined $first && ! defined $surname;
93 10 50 66     32 $first = $self->firstname if !defined $first && defined $surname;
94            
95 10         19 my $full = join ' ', grep {defined $_} ($first,$prefix,$surname);
  30         65  
96              
97 10 100       25 $full = $self->firstname unless length $full;
98              
99             # TBI: if OS-specific knowledge, then unix GCOS?
100              
101 10         38 $full;
102             }
103              
104              
105             sub formalName()
106 3     3 1 7 { my $self = shift;
107             return $self->{UI_formal_name}
108 3 50       11 if defined $self->{UI_formal_name};
109              
110 3         12 my $initials = $self->initials;
111              
112 3         6 my $firstname = $self->{UI_firstname};
113 3 50       9 $firstname = "($firstname)" if defined $firstname;
114              
115 15         41 my $full = join ' ', grep {defined $_}
116             $self->courtesy, $initials
117 3         8 , @$self{ qw/UI_prefix UI_surname UI_titles/ };
118             }
119              
120              
121             my %male_courtesy
122             = ( mister => 'en'
123             , mr => 'en'
124             , sir => 'en'
125             , 'de heer' => 'nl'
126             , mijnheer => 'nl'
127             , dhr => 'nl'
128             , herr => 'de'
129             );
130              
131             my %male_courtesy_default
132             = ( en => 'Mr.'
133             , nl => 'De heer'
134             , de => 'Herr'
135             );
136              
137             my %female_courtesy
138             = ( miss => 'en'
139             , ms => 'en'
140             , mrs => 'en'
141             , madam => 'en'
142             , mevr => 'nl'
143             , mevrouw => 'nl'
144             , frau => 'de'
145             );
146              
147             my %female_courtesy_default
148             = ( en => 'Madam'
149             , nl => 'Mevrouw'
150             , de => 'Frau'
151             );
152              
153             sub courtesy()
154 3     3 1 5 { my $self = shift;
155              
156             return $self->{UI_courtesy}
157 3 50       7 if defined $self->{UI_courtesy};
158              
159 3 50       7 my $table
    100          
160             = $self->isMale ? \%male_courtesy_default
161             : $self->isFemale ? \%female_courtesy_default
162             : return undef;
163              
164 3         8 my $lang = lc $self->language;
165 3 100       15 return $table->{$lang} if exists $table->{$lang};
166              
167 1         3 $lang =~ s/\..*//; # "en_GB.utf8" --> "en-GB" and retry
168 1 50       5 return $table->{$lang} if exists $table->{$lang};
169              
170 1         5 $lang =~ s/[-_].*//; # "en_GB.utf8" --> "en" and retry
171 1         4 $table->{$lang};
172             }
173              
174              
175             # TBI: if we have a courtesy, we may detect the language.
176             # TBI: when we have a postal address, we may derive the language from
177             # the country.
178             # TBI: if we have an e-mail addres, we may derive the language from
179             # that.
180              
181 3 100   3 1 14 sub language() { shift->{UI_language} || 'en' }
182              
183              
184 3     3 1 289 sub gender() { shift->{UI_gender} }
185              
186              
187             sub isMale()
188 6     6 1 11 { my $self = shift;
189              
190 6 100       17 if(my $gender = $self->{UI_gender})
191 5         29 { return $gender =~ m/^[mh]/i;
192             }
193              
194 1 50       3 if(my $courtesy = $self->{UI_courtesy})
195 0         0 { $courtesy = lc $courtesy;
196 0         0 $courtesy =~ s/[^\s\w]//g;
197 0 0       0 return 1 if exists $male_courtesy{$courtesy};
198             }
199              
200 1         4 undef;
201             }
202              
203              
204             sub isFemale()
205 4     4 1 9 { my $self = shift;
206              
207 4 100       12 if(my $gender = $self->{UI_gender})
208 3         18 { return $gender =~ m/^[vf]/i;
209             }
210              
211 1 50       3 if(my $courtesy = $self->{UI_courtesy})
212 0         0 { $courtesy = lc $courtesy;
213 0         0 $courtesy =~ s/[^\s\w]//g;
214 0 0       0 return 1 if exists $female_courtesy{$courtesy};
215             }
216              
217 1         4 undef;
218             }
219              
220              
221 1     1 1 5 sub dateOfBirth() { shift->{UI_birth} }
222              
223              
224             sub birth()
225 0     0 1   { my $birth = shift->dateOfBirth;
226 0           my $time;
227              
228 0 0         if($birth =~ m/^\s*(\d{4})[-\s]*(\d{2})[-\s]*(\d{2})\s*$/)
229             { # Pre-formatted.
230 0           return sprintf "%04d%02d%02d", $1, $2, $3;
231             }
232              
233 0           eval "require Date::Parse";
234 0 0         unless($@)
235 0           { my ($day,$month,$year) = (Date::Parse::strptime($birth))[3,4,5];
236 0 0         if(defined $year)
237 0 0 0       { return sprintf "%04d%02d%02d"
238             , ($year + 1900)
239             , (defined $month ? $month+1 : 0)
240             , ($day || 0);
241             }
242             }
243              
244             # TBI: Other date parsers
245              
246 0           undef;
247             }
248              
249              
250             sub age()
251 0 0   0 1   { my $birth = shift->birth or return;
252              
253 0           my ($year, $month, $day) = $birth =~ m/^(\d{4})(\d\d)(\d\d)$/;
254 0           my ($today, $tomonth, $toyear) = (localtime)[3,4,5];
255 0           $tomonth++;
256              
257 0           my $age = $toyear+1900 - $year;
258 0 0 0       $age-- if $month > $tomonth || ($month == $tomonth && $day >= $today);
      0        
259 0           $age;
260             }
261              
262              
263 0     0 1   sub titles() { shift->{UI_titles} }
264              
265             1;
266