File Coverage

blib/lib/Business/ID/SIM.pm
Criterion Covered Total %
statement 32 32 100.0
branch 7 12 58.3
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 46 51 90.2


line stmt bran cond sub pod time code
1             package Business::ID::SIM;
2              
3 2     2   387835 use 5.010001;
  2         7  
4 2     2   7 use warnings;
  2         3  
  2         99  
5 2     2   6 use strict;
  2         34  
  2         62  
6              
7 2     2   1714 use DateTime;
  2         1390502  
  2         115  
8              
9 2     2   23 use Exporter 'import';
  2         5  
  2         1283  
10              
11             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12             our $DATE = '2024-07-11'; # DATE
13             our $DIST = 'Business-ID-SIM'; # DIST
14             our $VERSION = '0.081'; # VERSION
15              
16             our @EXPORT_OK = qw(parse_sim);
17              
18             # legend: S = lack of samples
19              
20             # less organized than the convoluted code in NIK
21              
22             my %provinces = (
23             '06' => "Nanggroe Aceh Darussalam",
24             '07' => "Sumatera Utara",
25             '08' => "Sumatera Barat",
26             '09' => "Riau, Kepulauan Riau",
27             '10' => undef, # S
28             '11' => "Sumatera Selatan",
29             '12' => "DKI Jakarta", # most frequently encountered
30             '13' => "Jawa Barat",
31             '14' => "Jawa Tengah, DI Yogyakarta",
32             '15' => "Jawa Timur",
33             '16' => "Bali",
34             '17' => "Kalimantan Timur",
35             '18' => "Kalimantan Selatan",
36             '19' => "Sulawesi Selatan",
37             '20' => "Sulawesi Utara, Gorontalo",
38             '21' => undef, # S
39             '22' => 'Papua',
40             '23' => 'Kalimantan Tengah',
41             '24' => 'Sulawesi Tengah',
42             '25' => 'Lampung',
43             #'30' => "Banten", # ?, SS
44              
45             );
46              
47             our %SPEC;
48              
49             $SPEC{parse_sim} = {
50             v => 1.1,
51             summary => 'Validate Indonesian driving license number (nomor SIM)',
52             args => {
53             sim => {
54             summary => 'Input to be parsed',
55             schema => 'str*',
56             pos => 0,
57             req => 1,
58             },
59             },
60             };
61             sub parse_sim {
62 3     3 1 582291 my %args = @_;
63              
64 3 50       17 my $sim = $args{sim} or return [400, "Please specify sim"];
65 3         7 my $res = {};
66              
67 3         55 $sim =~ s/\D+//g;
68 3 50       14 return [400, "Not 12 digit"] unless length($sim) == 12;
69              
70 3         15 $res->{prov_code} = substr($sim, 4, 2);
71 3 100       28 return [400, "Unknown province code"] unless $provinces{$res->{prov_code}};
72 1         4 $res->{area_code} = substr($sim, 4, 4);
73              
74 1         7 my ($y, $m) = $sim =~ /^(..)(..)/;
75 1         11 my $today = DateTime->today;
76 1         1036 $y += int($today->year / 100) * 100;
77 1 50       13 $y -= 100 if $y > $today->year;
78 1         8 eval { $res->{dob} = DateTime->new(day=>1, month=>$m, year=>$y) };
  1         6  
79 1 50       506 return [400, "Invalid year and month of birth: $y, $m"] if $@;
80 1         7 $res->{serial} = $sim =~ /(....)$/;
81 1 50       7 return [400, "Serial starts from 1, not 0"] if $res->{serial} < 1;
82              
83 1         22 [200, "OK", $res];
84             }
85              
86             1;
87             # ABSTRACT: Validate Indonesian driving license number (nomor SIM)
88              
89             __END__
90              
91             =pod
92              
93             =encoding UTF-8
94              
95             =head1 NAME
96              
97             Business::ID::SIM - Validate Indonesian driving license number (nomor SIM)
98              
99             =head1 VERSION
100              
101             This document describes version 0.081 of Business::ID::SIM (from Perl distribution Business-ID-SIM), released on 2024-07-11.
102              
103             =head1 SYNOPSIS
104              
105             use Business::ID::SIM qw(parse_sim);
106              
107             my $res = parse_sim(sim => "0113 40 00 0001");
108              
109             =head1 DESCRIPTION
110              
111             This module can be used to validate Indonesian driving license number, Nomor
112             Surat Izin Mengemudi (SIM).
113              
114             SIM is composed of 12 digits as follow:
115              
116             yymm.pp.aa.ssss
117              
118             yy and mm are year and month of birth, pp and aa are area code
119             (province+district of some sort), ssss is 4-digit serial most probably starting
120             from 1.
121              
122             Note that there are several kinds of SIM (A, B1, B2, C) but this is not encoded
123             in the SIM number and all SIM's have the same number.
124              
125             =head1 FUNCTIONS
126              
127              
128             =head2 parse_sim
129              
130             Usage:
131              
132             parse_sim(%args) -> [$status_code, $reason, $payload, \%result_meta]
133              
134             Validate Indonesian driving license number (nomor SIM).
135              
136             This function is not exported by default, but exportable.
137              
138             Arguments ('*' denotes required arguments):
139              
140             =over 4
141              
142             =item * B<sim>* => I<str>
143              
144             Input to be parsed.
145              
146              
147             =back
148              
149             Returns an enveloped result (an array).
150              
151             First element ($status_code) is an integer containing HTTP-like status code
152             (200 means OK, 4xx caller error, 5xx function error). Second element
153             ($reason) is a string containing error message, or something like "OK" if status is
154             200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth
155             element (%result_meta) is called result metadata and is optional, a hash
156             that contains extra information, much like how HTTP response headers provide additional metadata.
157              
158             Return value: (any)
159              
160             =head1 HOMEPAGE
161              
162             Please visit the project's homepage at L<https://metacpan.org/release/Business-ID-SIM>.
163              
164             =head1 SOURCE
165              
166             Source repository is at L<https://github.com/perlancar/perl-Business-ID-SIM>.
167              
168             =head1 AUTHOR
169              
170             perlancar <perlancar@cpan.org>
171              
172             =head1 CONTRIBUTORS
173              
174             =for stopwords Steven Haryanto
175              
176             =over 4
177              
178             =item *
179              
180             Steven Haryanto <stevenharyanto@gmail.com>
181              
182             =item *
183              
184             Steven Haryanto <steven@masterweb.net>
185              
186             =back
187              
188             =head1 CONTRIBUTING
189              
190              
191             To contribute, you can send patches by email/via RT, or send pull requests on
192             GitHub.
193              
194             Most of the time, you don't need to build the distribution yourself. You can
195             simply modify the code, then test via:
196              
197             % prove -l
198              
199             If you want to build the distribution (e.g. to try to install it locally on your
200             system), you can install L<Dist::Zilla>,
201             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
202             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
203             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
204             that are considered a bug and can be reported to me.
205              
206             =head1 COPYRIGHT AND LICENSE
207              
208             This software is copyright (c) 2024, 2019, 2015, 2014, 2013 by perlancar <perlancar@cpan.org>.
209              
210             This is free software; you can redistribute it and/or modify it under
211             the same terms as the Perl 5 programming language system itself.
212              
213             =head1 BUGS
214              
215             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Business-ID-SIM>
216              
217             When submitting a bug or request, please include a test-file or a
218             patch to an existing test-file that illustrates the bug or desired
219             feature.
220              
221             =cut