File Coverage

blib/lib/Wikibase/Datatype/Struct/Value.pm
Criterion Covered Total %
statement 75 77 97.4
branch 38 40 95.0
condition n/a
subroutine 16 16 100.0
pod 2 2 100.0
total 131 135 97.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Value;
2              
3 46     46   1060573 use base qw(Exporter);
  46         172  
  46         4435  
4 46     46   330 use strict;
  46         144  
  46         1007  
5 46     46   284 use warnings;
  46         113  
  46         1340  
6              
7 46     46   1775 use Error::Pure qw(err);
  46         24518  
  46         1849  
8 46     46   462 use Readonly;
  46         143  
  46         1961  
9 46     46   21535 use Wikibase::Datatype::Struct::Value::Globecoordinate;
  46         156  
  46         2345  
10 46     46   21808 use Wikibase::Datatype::Struct::Value::Item;
  46         273  
  46         2228  
11 46     46   20906 use Wikibase::Datatype::Struct::Value::Monolingual;
  46         146  
  46         2042  
12 46     46   20369 use Wikibase::Datatype::Struct::Value::Property;
  46         204  
  46         2074  
13 46     46   21070 use Wikibase::Datatype::Struct::Value::Quantity;
  46         140  
  46         2246  
14 46     46   20829 use Wikibase::Datatype::Struct::Value::Sense;
  46         143  
  46         2105  
15 46     46   20409 use Wikibase::Datatype::Struct::Value::String;
  46         159  
  46         2115  
16 46     46   20843 use Wikibase::Datatype::Struct::Value::Time;
  46         136  
  46         2120  
17 46     46   346 use Wikibase::Datatype::Value;
  46         127  
  46         24361  
18              
19             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
20              
21             our $VERSION = 0.11;
22              
23             sub obj2struct {
24 53     53 1 18985 my ($obj, $base_uri) = @_;
25              
26 53 100       154 if (! defined $obj) {
27 1         4 err "Object doesn't exist.";
28             }
29 52 100       233 if (! $obj->isa('Wikibase::Datatype::Value')) {
30 1         5 err "Object isn't 'Wikibase::Datatype::Value'.";
31             }
32              
33 51         88 my $struct_hr;
34 51         275 my $type = $obj->type;
35 51 100       554 if ($type eq 'globecoordinate') {
    100          
    100          
    100          
    100          
    100          
    100          
    100          
36 1         7 $struct_hr = Wikibase::Datatype::Struct::Value::Globecoordinate::obj2struct($obj, $base_uri);
37             } elsif ($type eq 'item') {
38 19         77 $struct_hr = Wikibase::Datatype::Struct::Value::Item::obj2struct($obj);
39             } elsif ($type eq 'monolingualtext') {
40 1         5 $struct_hr = Wikibase::Datatype::Struct::Value::Monolingual::obj2struct($obj);
41             } elsif ($type eq 'property') {
42 1         7 $struct_hr = Wikibase::Datatype::Struct::Value::Property::obj2struct($obj);
43             } elsif ($type eq 'quantity') {
44 1         8 $struct_hr = Wikibase::Datatype::Struct::Value::Quantity::obj2struct($obj, $base_uri);
45             } elsif ($type eq 'sense') {
46 1         10 $struct_hr = Wikibase::Datatype::Struct::Value::Sense::obj2struct($obj, $base_uri);
47             } elsif ($type eq 'string') {
48 21         83 $struct_hr = Wikibase::Datatype::Struct::Value::String::obj2struct($obj);
49             } elsif ($type eq 'time') {
50 5         25 $struct_hr = Wikibase::Datatype::Struct::Value::Time::obj2struct($obj, $base_uri);
51             } else {
52 1         11 err "Type '$type' is unsupported.";
53             }
54              
55 50         270 return $struct_hr;
56             }
57              
58             sub struct2obj {
59 44     44 1 26543 my $struct_hr = shift;
60              
61 44 50       139 if (! exists $struct_hr->{'type'}) {
62 0         0 err "Type doesn't exist.";
63             }
64              
65 44         116 my $obj;
66 44 100       302 if ($struct_hr->{'type'} eq 'globecoordinate') {
    100          
    100          
    100          
    100          
    100          
67 1         5 $obj = Wikibase::Datatype::Struct::Value::Globecoordinate::struct2obj($struct_hr);
68             } elsif ($struct_hr->{'type'} eq 'monolingualtext') {
69 1         8 $obj = Wikibase::Datatype::Struct::Value::Monolingual::struct2obj($struct_hr);
70             } elsif ($struct_hr->{'type'} eq 'quantity') {
71 1         11 $obj = Wikibase::Datatype::Struct::Value::Quantity::struct2obj($struct_hr);
72             } elsif ($struct_hr->{'type'} eq 'string') {
73 19         73 $obj = Wikibase::Datatype::Struct::Value::String::struct2obj($struct_hr);
74             } elsif ($struct_hr->{'type'} eq 'time') {
75 5         35 $obj = Wikibase::Datatype::Struct::Value::Time::struct2obj($struct_hr);
76             } elsif ($struct_hr->{'type'} eq 'wikibase-entityid') {
77 16 100       68 if ($struct_hr->{'value'}->{'entity-type'} eq 'item') {
    100          
    50          
78 14         58 $obj = Wikibase::Datatype::Struct::Value::Item::struct2obj($struct_hr);
79             } elsif ($struct_hr->{'value'}->{'entity-type'} eq 'property') {
80 1         7 $obj = Wikibase::Datatype::Struct::Value::Property::struct2obj($struct_hr);
81             } elsif ($struct_hr->{'value'}->{'entity-type'} eq 'sense') {
82 1         6 $obj = Wikibase::Datatype::Struct::Value::Sense::struct2obj($struct_hr);
83             } else {
84 0         0 err "Entity type '$struct_hr->{'value'}->{'entity-type'}' is unsupported.";
85             }
86             } else {
87 1         7 err "Type '$struct_hr->{'type'}' is unsupported.";
88             }
89              
90 43         174 return $obj;
91             }
92              
93             1;
94              
95             __END__
96              
97             =pod
98              
99             =encoding utf8
100              
101             =head1 NAME
102              
103             Wikibase::Datatype::Struct::Value - Wikibase value structure serialization.
104              
105             =head1 SYNOPSIS
106              
107             use Wikibase::Datatype::Struct::Value qw(obj2struct struct2obj);
108              
109             my $struct_hr = obj2struct($obj, $base_uri);
110             my $obj = struct2obj($struct_hr);
111              
112             =head1 DESCRIPTION
113              
114             This conversion is between objects defined in Wikibase::Datatype and structures
115             serialized via JSON to MediaWiki.
116              
117             =head1 SUBROUTINES
118              
119             =head2 C<obj2struct>
120              
121             my $struct_hr = obj2struct($obj, $base_uri);
122              
123             Convert Wikibase::Datatype::Value instance to structure.
124             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
125              
126             Returns reference to hash with structure.
127              
128             =head2 C<struct2obj>
129              
130             my $obj = struct2obj($struct_hr);
131              
132             Convert structure of value to object.
133              
134             Returns Wikibase::Datatype::Value instance.
135              
136             =head1 ERRORS
137              
138             obj2struct():
139             Object doesn't exist.
140             Object isn't 'Wikibase::Datatype::Value'.
141             Type '%s' is unsupported.
142              
143             struct2obj():
144             Entity type '%s' is unsupported.
145             Type doesn't exist.
146             Type '%s' is unsupported.
147              
148             =head1 EXAMPLE1
149              
150             =for comment filename=obj2struct_value.pl
151              
152             use strict;
153             use warnings;
154              
155             use Data::Printer;
156             use Wikibase::Datatype::Value::Time;
157             use Wikibase::Datatype::Struct::Value qw(obj2struct);
158              
159             # Object.
160             my $obj = Wikibase::Datatype::Value::Time->new(
161             'precision' => 10,
162             'value' => '+2020-09-01T00:00:00Z',
163             );
164              
165             # Get structure.
166             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
167              
168             # Dump to output.
169             p $struct_hr;
170              
171             # Output:
172             # \ {
173             # type "time",
174             # value {
175             # after 0,
176             # before 0,
177             # calendarmodel "http://test.wikidata.org/entity/Q1985727",
178             # precision 10,
179             # time "+2020-09-01T00:00:00Z",
180             # timezone 0
181             # }
182             # }
183              
184             =head1 EXAMPLE2
185              
186             =for comment filename=struct2obj_value.pl
187              
188             use strict;
189             use warnings;
190              
191             use Wikibase::Datatype::Struct::Value qw(struct2obj);
192              
193             # Time structure.
194             my $struct_hr = {
195             'type' => 'time',
196             'value' => {
197             'after' => 0,
198             'before' => 0,
199             'calendarmodel' => 'http://test.wikidata.org/entity/Q1985727',
200             'precision' => 10,
201             'time' => '+2020-09-01T00:00:00Z',
202             'timezone' => 0,
203             },
204             };
205              
206             # Get object.
207             my $obj = struct2obj($struct_hr);
208              
209             # Get calendar model.
210             my $calendarmodel = $obj->calendarmodel;
211              
212             # Get precision.
213             my $precision = $obj->precision;
214              
215             # Get type.
216             my $type = $obj->type;
217              
218             # Get value.
219             my $value = $obj->value;
220              
221             # Print out.
222             print "Calendar model: $calendarmodel\n";
223             print "Precision: $precision\n";
224             print "Type: $type\n";
225             print "Value: $value\n";
226              
227             # Output:
228             # Calendar model: Q1985727
229             # Precision: 10
230             # Type: time
231             # Value: +2020-09-01T00:00:00Z
232              
233             =head1 DEPENDENCIES
234              
235             L<Error::Pure>,
236             L<Exporter>,
237             L<Readonly>,
238             L<Wikibase::Datatype::Value>,
239             L<Wikibase::Datatype::Struct::Value::Globecoordinate>,
240             L<Wikibase::Datatype::Struct::Value::Item>,
241             L<Wikibase::Datatype::Struct::Value::Monolingual>,
242             L<Wikibase::Datatype::Struct::Value::Property>,
243             L<Wikibase::Datatype::Struct::Value::Quantity>,
244             L<Wikibase::Datatype::Struct::Value::String>,
245             L<Wikibase::Datatype::Struct::Value::Time>.
246              
247             =head1 SEE ALSO
248              
249             =over
250              
251             =item L<Wikibase::Datatype::Struct>
252              
253             Wikibase structure serialization.
254              
255             =item L<Wikibase::Datatype::Value::Globecoordinate>
256              
257             Wikibase globe coordinate value datatype.
258              
259             =item L<Wikibase::Datatype::Value::Item>
260              
261             Wikibase item value datatype.
262              
263             =item L<Wikibase::Datatype::Value::Monolingual>
264              
265             Wikibase monolingual value datatype.
266              
267             =item L<Wikibase::Datatype::Value::Property>
268              
269             Wikibase property value datatype.
270              
271             =item L<Wikibase::Datatype::Value::Quantity>
272              
273             Wikibase quantity value datatype.
274              
275             =item L<Wikibase::Datatype::Value::String>
276              
277             Wikibase string value datatype.
278              
279             =item L<Wikibase::Datatype::Value::Time>
280              
281             Wikibase time value datatype.
282              
283             =back
284              
285             =head1 REPOSITORY
286              
287             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
288              
289             =head1 AUTHOR
290              
291             Michal Josef Špaček L<mailto:skim@cpan.org>
292              
293             L<http://skim.cz>
294              
295             =head1 LICENSE AND COPYRIGHT
296              
297             © 2020-2023 Michal Josef Špaček
298              
299             BSD 2-Clause License
300              
301             =head1 VERSION
302              
303             0.11
304              
305             =cut