From 5f6a17efd828bb731bfd69bfd694d983e428e500 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 30 May 2015 23:30:33 +0300 Subject: [PATCH] Initial commit --- Changes | 4 + MANIFEST | 8 ++ Makefile.PL | 20 ++++ README | 30 ++++++ lib/Number/Phone/RO.pm | 204 +++++++++++++++++++++++++++++++++++++++++ t/Number-Phone-RO.t | 48 ++++++++++ t/perlcritic.t | 10 ++ t/perlcriticrc | 36 ++++++++ 8 files changed, 360 insertions(+) create mode 100644 Changes create mode 100644 MANIFEST create mode 100644 Makefile.PL create mode 100644 README create mode 100644 lib/Number/Phone/RO.pm create mode 100644 t/Number-Phone-RO.t create mode 100644 t/perlcritic.t create mode 100644 t/perlcriticrc diff --git a/Changes b/Changes new file mode 100644 index 0000000..50721e7 --- /dev/null +++ b/Changes @@ -0,0 +1,4 @@ +Revision history for Perl extension Number::Phone::RO. + +0.001 2015-05-30T23:30+03:00 + - Initial release diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..2d4b445 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,8 @@ +Changes +Makefile.PL +MANIFEST +README +t/Number-Phone-RO.t +t/perlcritic.t +t/perlcriticrc +lib/Number/Phone/RO.pm diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..4f65640 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,20 @@ +use 5.014000; +use ExtUtils::MakeMaker; + +WriteMakefile( + NAME => 'Number::Phone::RO', + VERSION_FROM => 'lib/Number/Phone/RO.pm', + AUTHOR => 'Marius Gavrilescu ', + MIN_PERL_VERSION => '5.14.0', + LICENSE => 'perl', + SIGN => 1, + PREREQ_PM => { + qw/Number::Phone 0/, + }, + META_ADD => { + dynamic_config => 0, + resources => { + repository => 'https://git.ieval.ro/?p=number-phone-ro.git', + }, + } +); diff --git a/README b/README new file mode 100644 index 0000000..65c6d6d --- /dev/null +++ b/README @@ -0,0 +1,30 @@ +Number-Phone-RO version 0.001 +============================= + +Number::Phone::RO is a Number::Phone extension for Romanian phone +numbers. + +INSTALLATION + +To install this module type the following: + + perl Makefile.PL + make + make test + make install + +DEPENDENCIES + +This module requires these other modules and libraries: + +* Number::Phone + +COPYRIGHT AND LICENCE + +Copyright (C) 2015 by Marius Gavrilescu + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.20.2 or, +at your option, any later version of Perl 5 you may have available. + + diff --git a/lib/Number/Phone/RO.pm b/lib/Number/Phone/RO.pm new file mode 100644 index 0000000..d2d94d5 --- /dev/null +++ b/lib/Number/Phone/RO.pm @@ -0,0 +1,204 @@ +package Number::Phone::RO; + +use 5.014000; +use strict; +use warnings; +use parent qw/Number::Phone/; +use utf8; +use re '/s'; + +sub AREA_NAMES (); ## no critic (ProhibitSubroutinePrototypes) + +our $VERSION = '0.001'; + +our %cache; + +sub _normalized { + my ($nr) = @_; + $nr =~ y/0-9+//cd; + $nr =~ s/^[+]40//; + $nr =~ s/^0//; + $nr +} + +sub _analyze_number { + my ($nr) = @_; + my %info; + + return { valid => 0 } unless length $nr == 9; + $info{valid} = 1; + + $info{geographic} = $nr =~ /^[23][3-6]/ ? 1 : 0; + @info{qw/fixed_line mobile/} = (1, 0) if $nr =~ /^[23]/; + @info{qw/fixed_line mobile/} = (0, 1) if $nr =~ /^7/; + $info{tollfree} = $nr =~ /^800/ ? 1 : 0; + $info{specialrate} = $nr =~ /^90/ ? 1 : 0; + $info{adult} = 1 if $nr =~ /^906/; + + my $arealen = $nr =~ /^[23]1/ ? 2 : 3; + $info{areacode} = substr $nr, 0, $arealen; + $info{subscriber} = substr $nr, $arealen; + + \%info +} + +sub _info { $cache{${$_[0]}} } + +sub new { + my ($class, $nr) = @_; + $nr = _normalized $nr; + $cache{$nr} = _analyze_number $nr; + my $self = bless \$nr, $class; + $self->is_valid ? $self : undef +} + +sub is_valid { shift->_info->{valid} } +sub is_geographic { shift->_info->{geographic} } +sub is_fixed_line { shift->_info->{fixed_line} } +sub is_mobile { shift->_info->{mobile} } +sub is_tollfree { shift->_info->{tollfree} } +sub is_specialrate { shift->_info->{specialrate} } +sub is_adult { shift->_info->{adult} } + +sub country_code { 40 } +sub regulator { 'ANCOM, http://ancom.org.ro'} + +sub areacode { shift->_info->{areacode} } +sub areaname { $_[0]->is_geographic ? AREA_NAMES->{substr $_[0]->areacode, 1} : undef } +sub subscriber { shift->_info->{subscriber} } + +sub format { ## no critic (ProhibitBuiltinHomonyms) + my ($self) = @_; + join ' ', + '+40', + $self->areacode, + (substr $self->subscriber, 0, 3), + (substr $self->subscriber, 3); +} + +sub intra_country_dial_to { "0${$_[0]}" } + +use constant AREA_NAMES => { + 1 => 'București', + 30 => 'Suceava', + 31 => 'Botoșani', + 32 => 'Iași', + 33 => 'Neamț', + 34 => 'Bacău', + 35 => 'Vaslui', + 36 => 'Galați', + 37 => 'Vrancea', + 38 => 'Buzău', + 39 => 'Brăila', + 40 => 'Tulcea', + 41 => 'Constanța', + 42 => 'Călărași', + 43 => 'Ialomița', + 44 => 'Prahova', + 45 => 'Dâmbovița', + 46 => 'Giurgiu', + 47 => 'Teleorman', + 48 => 'Argeș', + 49 => 'Olt', + 50 => 'Vâlcea', + 51 => 'Dolj', + 52 => 'Mehedinți', + 53 => 'Gorj', + 54 => 'Hunedoara', + 55 => 'Caraș-Severin', + 56 => 'Timiș', + 57 => 'Arad', + 58 => 'Alba', + 59 => 'Bihor', + 60 => 'Sălaj', + 61 => 'Satu Mare', + 62 => 'Maramureș', + 63 => 'Bistrița-Năsăud', + 64 => 'Cluj', + 65 => 'Mureș', + 66 => 'Harghita', + 67 => 'Covasna', + 68 => 'Brașov', + 69 => 'Sibiu', +}; + +1; +__END__ + +=encoding utf-8 + +=head1 NAME + +Number::Phone::RO - Phone number information for Romania (+40) + +=head1 SYNOPSIS + + use Number::Phone::RO; + my $nr = Number::Phone::RO->new('+40250123456'); + say $nr->is_geographic; # 1 + say $nr->is_fixed_line; # 1 + say $nr->is_mobile; # 0 + say $nr->is_tollfree; # 0 + say $nr->is_specialrate; # 0 + say $nr->areacode; # 250 + say $nr->areaname; # Vâlcea + say $nr->subscriber; # 123456 + say $nr->format; # +40 250 123 456 + +=head1 DESCRIPTION + +This module is a work in progress. See the L section below. + +See the L documentation for usage information. The +following methods from L are overridden: + +=over + +=item B + +=item B + +=item B + +=item B + +=item B + +=item B + +Always returns 40. + +=item B + +Returns the name and URL of the regulator, ANCOM. + +=item B + +=item B + +=item B + +=item B + +=back + +=head1 TODO + +Only long (10 digits) numbers are supported. + +Should query L to implement B and B. + +=head1 AUTHOR + +Marius Gavrilescu, Emarius@ieval.roE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2015 by Marius Gavrilescu + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.20.2 or, +at your option, any later version of Perl 5 you may have available. + + +=cut diff --git a/t/Number-Phone-RO.t b/t/Number-Phone-RO.t new file mode 100644 index 0000000..8bf5a97 --- /dev/null +++ b/t/Number-Phone-RO.t @@ -0,0 +1,48 @@ +#!/usr/bin/perl +use strict; +use warnings; +use utf8; + +use Test::More tests => 22; +BEGIN { use_ok('Number::Phone::RO') }; + +my $nr; + +sub num { + my ($num) = @_; + note "Now working with the number $num"; + $nr = Number::Phone::RO->new($num); +} + +num '0'; +ok !defined $nr, 'Constructor returns undef'; + +num '0350123456'; +ok $nr->is_geographic, 'is_geographic'; +ok $nr->is_fixed_line, 'is_fixed_line'; +ok !$nr->is_mobile, '!is_mobile'; +ok !$nr->is_tollfree, 'is_tollfree'; +ok !$nr->is_specialrate, 'is_specialrate'; +is $nr->areacode, '350', 'areacode'; +is $nr->areaname, 'Vâlcea', 'areaname'; +is $nr->subscriber, '123456', 'subscriber'; +is $nr->format, '+40 350 123 456', 'format'; + +num '0211234567'; +is $nr->areacode, '21', 'areacode'; +is $nr->subscriber, '1234567', 'subscriber'; +is $nr->format, '+40 21 123 4567', 'format'; + +num '0800123123'; +is $nr->intra_country_dial_to, '0800123123', 'intra_country_dial_to'; +ok $nr->is_tollfree, 'is_tollfree'; +is $nr->country_code, 40, 'country_code'; +like $nr->regulator, qr/ANCOM/, 'regulator'; +ok !defined $nr->areaname, 'areaname is undef'; + +num '0906123456'; +ok $nr->is_adult, 'is_adult'; + +num '0731123456'; +ok $nr->is_mobile, 'is_mobile'; +ok !$nr->is_fixed_line, 'is_fixed_line'; diff --git a/t/perlcritic.t b/t/perlcritic.t new file mode 100644 index 0000000..51bad9d --- /dev/null +++ b/t/perlcritic.t @@ -0,0 +1,10 @@ +#!/usr/bin/perl +use v5.14; +use warnings; + +use Test::More; + +BEGIN { plan skip_all => '$ENV{RELEASE_TESTING} is false' unless $ENV{RELEASE_TESTING} } +use Test::Perl::Critic -profile => 't/perlcriticrc'; + +all_critic_ok diff --git a/t/perlcriticrc b/t/perlcriticrc new file mode 100644 index 0000000..5815f07 --- /dev/null +++ b/t/perlcriticrc @@ -0,0 +1,36 @@ +severity = 1 + +[-BuiltinFunctions::ProhibitComplexMappings] +[-CodeLayout::RequireTidyCode] +[-ControlStructures::ProhibitPostfixControls] +[-ControlStructures::ProhibitUnlessBlocks] +[-Documentation::PodSpelling] +[-Documentation::RequirePodLinksIncludeText] +[-InputOutput::RequireBracedFileHandleWithPrint] +[-Modules::ProhibitAutomaticExportation] +[-References::ProhibitDoubleSigils] +[-RegularExpressions::ProhibitEnumeratedClasses] +[-RegularExpressions::RequireLineBoundaryMatching] +[-Subroutines::RequireFinalReturn] +[-ValuesAndExpressions::ProhibitConstantPragma] +[-ValuesAndExpressions::ProhibitEmptyQuotes] +[-ValuesAndExpressions::ProhibitLeadingZeros] +[-ValuesAndExpressions::ProhibitMagicNumbers] +[-ValuesAndExpressions::ProhibitNoisyQuotes] +[-Variables::ProhibitLocalVars] +[-Variables::ProhibitPackageVars] +[-Variables::ProhibitPunctuationVars] + +[BuiltinFunctions::ProhibitStringyEval] +allow_includes = 1 + +[RegularExpressions::RequireExtendedFormatting] +minimum_regex_length_to_complain_about = 20 + +[Documentation::RequirePodSections] +lib_sections = NAME | SYNOPSIS | DESCRIPTION | AUTHOR | COPYRIGHT AND LICENSE +script_sections = NAME | SYNOPSIS | DESCRIPTION | AUTHOR | COPYRIGHT AND LICENSE + +[Subroutines::RequireArgUnpacking] +short_subroutine_statements = 5 +allow_subscripts = 1 -- 2.30.2