« Class/Business/DK/Phonenumber.pm
↑ Summary
Data/FormValidator/Constraints/Business/DK/Phonenumber.pm »
| 1 | package Business::DK::Phonenumber; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 14 | ▶ | use vars qw($VERSION @EXPORT_OK); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 14 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 14 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 14 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 14 | $VERSION = '0.10'; | |||||||||||||||||||||||||||||||
| 11 | @EXPORT_OK | ||||||||||||||||||||||||||||||||
| 12 | 14 | = qw(validate render generate validate_template TRUE FALSE DK_PREFIX DEFAULT_TEMPLATE); | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 14 | ▶ | use constant TRUE => 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 14 | ▶ | use constant FALSE => 0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 14 | ▶ | use constant DK_PREFIX => '+45'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 14 | ▶ | use constant DIGITS => 8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 14 | ▶ | use constant DEFAULT_TEMPLATE => DK_PREFIX . ' %0' . DIGITS . 'd'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 14 | ▶ | use constant SEED => 99999999; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 81 | ▶ | sub validate { | ||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 22 | 81 | my ( $self, $phonenumber ) = @_; | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 81 | ▶ | if ( not ref $self ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | 81 | $phonenumber = $self; | |||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 81 | $phonenumber =~ s/\s//sxmg; | |||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 81 | ▶ | if ($phonenumber =~ m{ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | \A( | ||||||||||||||||||||||||||||||||
| 32 | (?:[+])(?:45)(?:\d{8})| #+4512345678 | ||||||||||||||||||||||||||||||||
| 33 | (?:45)(?:\d{8})| #4512345678 | ||||||||||||||||||||||||||||||||
| 34 | (?:\d{8}) #12345678 | ||||||||||||||||||||||||||||||||
| 35 | )\z}sgmx | ||||||||||||||||||||||||||||||||
| 36 | ) | ||||||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||||||
| 38 | 37 | return TRUE; | |||||||||||||||||||||||||||||||
| 39 | } else { | ||||||||||||||||||||||||||||||||
| 40 | 44 | return FALSE; | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 116 | ▶ | sub render { | ||||||||||||||||||||||||||||||
|
Subroutine
render: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 45 | 116 | my ( $self, $phonenumber, $template ) = @_; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 116 | ▶ | if ( $self =~ m/\A[\D]+\b/smx and not ref $self ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$self =~ /\A[\D]+\b/msx and not ref $self
MC/DC100%2/2
$self =~ /\A[\D]+\b/msx and not ref $self
$self =~ /\A[\D]+\b/msx
not ref $self
Truth table100%3/3
A and not(B)
A= $self =~ /\A[\D]+\b/msxB= not ref $self
| |||||||||||||||||||||||||||||||||
| 48 | 2 | $self = undef; | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | } elsif ( not ref $self ) { | ||||||||||||||||||||||||||||||||
| 51 | 110 | $template = $phonenumber; | |||||||||||||||||||||||||||||||
| 52 | 110 | $phonenumber = $self; | |||||||||||||||||||||||||||||||
| 53 | 110 | $self = undef; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | } else { | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 4 | ▶ | if ($template) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | if ( ref $self ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 59 | 1 | $self->validate_template($template); | |||||||||||||||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||||||||||||||
| 61 | 0 | Class::Business::DK::Phonenumber::validate_template( | |||||||||||||||||||||||||||||||
| 62 | $template); | ||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||||||
| 65 | 3 | ▶ | $template = $self->{template} || DEFAULT_TEMPLATE; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'template'} || '+45 %08d'
MC/DC0%0/1
$self->{'template'} || '+45 %08d'
$self->{'template'}
Truth table50%1/2
A
A= $self->{'template'}
| |||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | if ( not $phonenumber ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | 4 | $phonenumber = $self->{phonenumber}; | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 116 | $phonenumber =~ s/\s//sxmg; | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 116 | my @subs = $template =~ m/%(\d)+d/sxmg; | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 116 | my $sum = 0; | |||||||||||||||||||||||||||||||
| 78 | 116 | my @phonesplit = (); | |||||||||||||||||||||||||||||||
| 79 | 116 | my $phonetmp = $phonenumber; | |||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 116 | foreach my $sub (@subs) { | |||||||||||||||||||||||||||||||
| 82 | 443 | $sum += $sub; | |||||||||||||||||||||||||||||||
| 83 | 443 | push @phonesplit, substr $phonetmp, 0, $sub, q{}; | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 116 | my $output = sprintf $template, @phonesplit; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 116 | return $output; | |||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 4 | ▶ | sub generate { | ||||||||||||||||||||||||||||||
|
Subroutine
generate: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 92 | 4 | my ( $self, $amount, $template ) = @_; | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 4 | ▶ | if ( not $amount ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 1 | $amount = 1; | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | if ( not $template ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | 2 | $template = DEFAULT_TEMPLATE; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 4 | my %phonenumbers; | |||||||||||||||||||||||||||||||
| 103 | 4 | ▶ | while ( keys %phonenumbers < $amount ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 104 | 105 | ▶ | if ( ref $self ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | 0 | $phonenumbers{ $self->_generate($template) }++; | |||||||||||||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||||||||||||||
| 107 | 105 | $phonenumbers{ _generate($template) }++; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 4 | my @phonenumbers = keys %phonenumbers; | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 4 | return @phonenumbers; | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 105 | ▶ | sub _generate { | ||||||||||||||||||||||||||||||
|
Subroutine
_generate: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||
| 117 | 105 | my ( $self, $template ) = @_; | |||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 105 | my $random_phone = int rand SEED; | |||||||||||||||||||||||||||||||
| 120 | 105 | my $phonenumber = sprintf '%.08d', $random_phone; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 105 | ▶ | if ( ref $self ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 123 | 0 | return $self->render( $phonenumber, $template ); | |||||||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||||||
| 125 | 105 | $template = $self; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 105 | return render( $phonenumber, $template ); | |||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | 16 | ▶ | sub validate_template { | ||||||||||||||||||||||||||||||
|
Subroutine
validate_template: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 132 | 16 | my ( $self, $template ) = @_; | |||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 16 | my @digits = $template =~ m/%(\d)+d/sxmg; | |||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 16 | my $sum = 0; | |||||||||||||||||||||||||||||||
| 137 | 16 | foreach my $digit (@digits) { | |||||||||||||||||||||||||||||||
| 138 | 25 | $sum += $digit; | |||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 16 | ▶ | if ( $sum == DIGITS ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 142 | 8 | return TRUE; | |||||||||||||||||||||||||||||||
| 143 | } else { | ||||||||||||||||||||||||||||||||
| 144 | 8 | return FALSE; | |||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 14 | 1; | |||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||||||