File Coverage

blib/lib/Wikibase/Datatype/Struct/Value/Time.pm
Criterion Covered Total %
statement 38 38 100.0
branch 8 8 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 60 60 100.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Value::Time;
2              
3 46     46   350694 use base qw(Exporter);
  46         145  
  46         4680  
4 46     46   303 use strict;
  46         106  
  46         1320  
5 46     46   253 use warnings;
  46         117  
  46         1472  
6              
7 46     46   1799 use Error::Pure qw(err);
  46         24674  
  46         2149  
8 46     46   440 use Readonly;
  46         97  
  46         2098  
9 46     46   2581 use URI;
  46         19344  
  46         1406  
10 46     46   23120 use Wikibase::Datatype::Value::Time;
  46         65577  
  46         17567  
11              
12             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13              
14             our $VERSION = 0.08;
15              
16             sub obj2struct {
17 8     8 1 21496 my ($obj, $base_uri) = @_;
18              
19 8 100       29 if (! defined $obj) {
20 1         5 err "Object doesn't exist.";
21             }
22 7 100       38 if (! $obj->isa('Wikibase::Datatype::Value::Time')) {
23 1         6 err "Object isn't 'Wikibase::Datatype::Value::Time'.";
24             }
25 6 100       20 if (! defined $base_uri) {
26 1         5 err 'Base URI is required.';
27             }
28              
29 5         23 my $struct_hr = {
30             'value' => {
31             'after' => $obj->after,
32             'before' => $obj->before,
33             'calendarmodel' => $base_uri.$obj->calendarmodel,
34             'precision' => $obj->precision,
35             'time' => $obj->value,
36             'timezone' => $obj->timezone,
37             },
38             'type' => 'time',
39             };
40              
41 5         282 return $struct_hr;
42             }
43              
44             sub struct2obj {
45 7     7 1 22318 my $struct_hr = shift;
46              
47 7 100 100     51 if (! exists $struct_hr->{'type'}
48             || $struct_hr->{'type'} ne 'time') {
49              
50 2         9 err "Structure isn't for 'time' datatype.";
51             }
52              
53 5         35 my $u = URI->new($struct_hr->{'value'}->{'calendarmodel'});
54 5         23031 my @path_segments = $u->path_segments;
55 5         415 my $calendar_model = $path_segments[-1];
56             my $obj = Wikibase::Datatype::Value::Time->new(
57             'after' => $struct_hr->{'value'}->{'after'},
58             'before' => $struct_hr->{'value'}->{'before'},
59             'calendarmodel' => $calendar_model,
60             'precision' => $struct_hr->{'value'}->{'precision'},
61             'timezone' => $struct_hr->{'value'}->{'timezone'},
62 5         61 'value' => $struct_hr->{'value'}->{'time'},
63             );
64              
65 5         565 return $obj;
66             }
67              
68             1;
69              
70             __END__
71              
72             =pod
73              
74             =encoding utf8
75              
76             =head1 NAME
77              
78             Wikibase::Datatype::Struct::Value::Time - Wikibase time structure serialization.
79              
80             =head1 SYNOPSIS
81              
82             use Wikibase::Datatype::Struct::Value::Time qw(obj2struct struct2obj);
83              
84             my $struct_hr = obj2struct($obj, $base_uri);
85             my $obj = struct2obj($struct_hr);
86              
87             =head1 DESCRIPTION
88              
89             This conversion is between objects defined in Wikibase::Datatype and structures
90             serialized via JSON to MediaWiki.
91              
92             =head1 SUBROUTINES
93              
94             =head2 C<obj2struct>
95              
96             my $struct_hr = obj2struct($obj, $base_uri);
97              
98             Convert Wikibase::Datatype::Value::Time instance to structure.
99             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
100              
101             Returns reference to hash with structure.
102              
103             =head2 C<struct2obj>
104              
105             my $obj = struct2obj($struct_hr);
106              
107             Convert structure of time to object.
108              
109             Returns Wikibase::Datatype::Value::Time instance.
110              
111             =head1 ERRORS
112              
113             obj2struct():
114             Base URI is required.
115             Object doesn't exist.
116             Object isn't 'Wikibase::Datatype::Value::Time'.
117              
118             struct2obj():
119             Structure isn't for 'time' datatype.
120              
121             =head1 EXAMPLE1
122              
123             use strict;
124             use warnings;
125              
126             use Data::Printer;
127             use Wikibase::Datatype::Value::Time;
128             use Wikibase::Datatype::Struct::Value::Time qw(obj2struct);
129              
130             # Object.
131             my $obj = Wikibase::Datatype::Value::Time->new(
132             'precision' => 10,
133             'value' => '+2020-09-01T00:00:00Z',
134             );
135              
136             # Get structure.
137             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
138              
139             # Dump to output.
140             p $struct_hr;
141              
142             # Output:
143             # \ {
144             # type "time",
145             # value {
146             # after 0,
147             # before 0,
148             # calendarmodel "http://test.wikidata.org/entity/Q1985727",
149             # precision 10,
150             # time "+2020-09-01T00:00:00Z",
151             # timezone 0
152             # }
153             # }
154              
155             =head1 EXAMPLE2
156              
157             use strict;
158             use warnings;
159              
160             use Wikibase::Datatype::Struct::Value::Time qw(struct2obj);
161              
162             # Time structure.
163             my $struct_hr = {
164             'type' => 'time',
165             'value' => {
166             'after' => 0,
167             'before' => 0,
168             'calendarmodel' => 'http://test.wikidata.org/entity/Q1985727',
169             'precision' => 10,
170             'time' => '+2020-09-01T00:00:00Z',
171             'timezone' => 0,
172             },
173             };
174              
175             # Get object.
176             my $obj = struct2obj($struct_hr);
177              
178             # Get calendar model.
179             my $calendarmodel = $obj->calendarmodel;
180              
181             # Get precision.
182             my $precision = $obj->precision;
183              
184             # Get type.
185             my $type = $obj->type;
186              
187             # Get value.
188             my $value = $obj->value;
189              
190             # Print out.
191             print "Calendar model: $calendarmodel\n";
192             print "Precision: $precision\n";
193             print "Type: $type\n";
194             print "Value: $value\n";
195              
196             # Output:
197             # Calendar model: Q1985727
198             # Precision: 10
199             # Type: time
200             # Value: +2020-09-01T00:00:00Z
201              
202             =head1 DEPENDENCIES
203              
204             L<Error::Pure>,
205             L<Exporter>,
206             L<Readonly>,
207             L<URL>,
208             L<Wikibase::Datatype::Value::Time>.
209              
210             =head1 SEE ALSO
211              
212             =over
213              
214             =item L<Wikibase::Datatype::Struct>
215              
216             Wikibase structure serialization.
217              
218             =item L<Wikibase::Datatype::Value::Time>
219              
220             Wikibase time value datatype.
221              
222             =back
223              
224             =head1 REPOSITORY
225              
226             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
227              
228             =head1 AUTHOR
229              
230             Michal Josef Špaček L<mailto:skim@cpan.org>
231              
232             L<http://skim.cz>
233              
234             =head1 LICENSE AND COPYRIGHT
235              
236             © Michal Josef Špaček 2020-2021
237              
238             BSD 2-Clause License
239              
240             =head1 VERSION
241              
242             0.08
243              
244             =cut