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 50     50   1065219 use base qw(Exporter);
  50         163  
  50         4896  
4 50     50   390 use strict;
  50         131  
  50         1043  
5 50     50   245 use warnings;
  50         135  
  50         1632  
6              
7 50     50   1751 use Error::Pure qw(err);
  50         25206  
  50         2079  
8 50     50   513 use Readonly;
  50         139  
  50         2069  
9 50     50   2812 use URI;
  50         19923  
  50         1530  
10 50     50   22057 use Wikibase::Datatype::Value::Time;
  50         1761138  
  50         18368  
11              
12             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13              
14             our $VERSION = 0.11;
15              
16             sub obj2struct {
17 9     9 1 11558 my ($obj, $base_uri) = @_;
18              
19 9 100       33 if (! defined $obj) {
20 1         21 err "Object doesn't exist.";
21             }
22 8 100       44 if (! $obj->isa('Wikibase::Datatype::Value::Time')) {
23 1         5 err "Object isn't 'Wikibase::Datatype::Value::Time'.";
24             }
25 7 100       27 if (! defined $base_uri) {
26 1         4 err 'Base URI is required.';
27             }
28              
29 6         31 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 6         401 return $struct_hr;
42             }
43              
44             sub struct2obj {
45 8     8 1 8571 my $struct_hr = shift;
46              
47 8 100 100     72 if (! exists $struct_hr->{'type'}
48             || $struct_hr->{'type'} ne 'time') {
49              
50 2         23 err "Structure isn't for 'time' datatype.";
51             }
52              
53 6         47 my $u = URI->new($struct_hr->{'value'}->{'calendarmodel'});
54 6         36099 my @path_segments = $u->path_segments;
55 6         601 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 6         81 'value' => $struct_hr->{'value'}->{'time'},
63             );
64              
65 6         21518 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 value 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             =for comment filename=obj2struct_value_time.pl
124              
125             use strict;
126             use warnings;
127              
128             use Data::Printer;
129             use Wikibase::Datatype::Value::Time;
130             use Wikibase::Datatype::Struct::Value::Time qw(obj2struct);
131              
132             # Object.
133             my $obj = Wikibase::Datatype::Value::Time->new(
134             'precision' => 10,
135             'value' => '+2020-09-01T00:00:00Z',
136             );
137              
138             # Get structure.
139             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
140              
141             # Dump to output.
142             p $struct_hr;
143              
144             # Output:
145             # \ {
146             # type "time",
147             # value {
148             # after 0,
149             # before 0,
150             # calendarmodel "http://test.wikidata.org/entity/Q1985727",
151             # precision 10,
152             # time "+2020-09-01T00:00:00Z",
153             # timezone 0
154             # }
155             # }
156              
157             =head1 EXAMPLE2
158              
159             =for comment filename=struct2obj_value_time.pl
160              
161             use strict;
162             use warnings;
163              
164             use Wikibase::Datatype::Struct::Value::Time qw(struct2obj);
165              
166             # Time structure.
167             my $struct_hr = {
168             'type' => 'time',
169             'value' => {
170             'after' => 0,
171             'before' => 0,
172             'calendarmodel' => 'http://test.wikidata.org/entity/Q1985727',
173             'precision' => 10,
174             'time' => '+2020-09-01T00:00:00Z',
175             'timezone' => 0,
176             },
177             };
178              
179             # Get object.
180             my $obj = struct2obj($struct_hr);
181              
182             # Get calendar model.
183             my $calendarmodel = $obj->calendarmodel;
184              
185             # Get precision.
186             my $precision = $obj->precision;
187              
188             # Get type.
189             my $type = $obj->type;
190              
191             # Get value.
192             my $value = $obj->value;
193              
194             # Print out.
195             print "Calendar model: $calendarmodel\n";
196             print "Precision: $precision\n";
197             print "Type: $type\n";
198             print "Value: $value\n";
199              
200             # Output:
201             # Calendar model: Q1985727
202             # Precision: 10
203             # Type: time
204             # Value: +2020-09-01T00:00:00Z
205              
206             =head1 DEPENDENCIES
207              
208             L<Error::Pure>,
209             L<Exporter>,
210             L<Readonly>,
211             L<URL>,
212             L<Wikibase::Datatype::Value::Time>.
213              
214             =head1 SEE ALSO
215              
216             =over
217              
218             =item L<Wikibase::Datatype::Struct>
219              
220             Wikibase structure serialization.
221              
222             =item L<Wikibase::Datatype::Value::Time>
223              
224             Wikibase time value datatype.
225              
226             =back
227              
228             =head1 REPOSITORY
229              
230             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
231              
232             =head1 AUTHOR
233              
234             Michal Josef Špaček L<mailto:skim@cpan.org>
235              
236             L<http://skim.cz>
237              
238             =head1 LICENSE AND COPYRIGHT
239              
240             © 2020-2023 Michal Josef Špaček
241              
242             BSD 2-Clause License
243              
244             =head1 VERSION
245              
246             0.11
247              
248             =cut