File Coverage

blib/lib/Wikibase/Datatype/Struct/Reference.pm
Criterion Covered Total %
statement 33 33 100.0
branch 6 6 100.0
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Reference;
2              
3 24     24   373467 use base qw(Exporter);
  24         97  
  24         2988  
4 24     24   269 use strict;
  24         59  
  24         531  
5 24     24   138 use warnings;
  24         60  
  24         917  
6              
7 24     24   1684 use Error::Pure qw(err);
  24         35648  
  24         1003  
8 24     24   296 use Readonly;
  24         105  
  24         1028  
9 24     24   11184 use Wikibase::Datatype::Reference;
  24         24265  
  24         768  
10 24     24   12056 use Wikibase::Datatype::Struct::Utils qw(obj_array_ref2struct struct2snaks_array_ref);
  24         73  
  24         581  
11              
12             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13              
14             our $VERSION = 0.08;
15              
16             sub obj2struct {
17 8     8 1 12469 my ($obj, $base_uri) = @_;
18              
19 8 100       37 if (! defined $obj) {
20 1         6 err "Object doesn't exist.";
21             }
22 7 100       43 if (! $obj->isa('Wikibase::Datatype::Reference')) {
23 1         9 err "Object isn't 'Wikibase::Datatype::Reference'.";
24             }
25 6 100       24 if (! defined $base_uri) {
26 1         5 err 'Base URI is required.';
27             }
28              
29 5         19 my $struct_hr = obj_array_ref2struct($obj->snaks, 'snaks', $base_uri);
30              
31 5         37 return $struct_hr;
32             }
33              
34             sub struct2obj {
35 5     5 1 2460 my $struct_hr = shift;
36              
37 5         20 my $obj = Wikibase::Datatype::Reference->new(
38             'snaks' => struct2snaks_array_ref($struct_hr, 'snaks'),
39             );
40              
41 5         378 return $obj;
42             }
43              
44             1;
45              
46             __END__
47              
48             =pod
49              
50             =encoding utf8
51              
52             =head1 NAME
53              
54             Wikibase::Datatype::Struct::Reference - Wikibase reference structure serialization.
55              
56             =head1 SYNOPSIS
57              
58             use Wikibase::Datatype::Struct::Reference qw(obj2struct struct2obj);
59              
60             my $struct_hr = obj2struct($obj, $base_uri);
61             my $obj = struct2obj($struct_hr);
62              
63             =head1 DESCRIPTION
64              
65             This conversion is between objects defined in Wikibase::Datatype and structures
66             serialized via JSON to MediaWiki.
67              
68             =head1 SUBROUTINES
69              
70             =head2 C<obj2struct>
71              
72             my $struct_hr = obj2struct($obj, $base_uri);
73              
74             Convert Wikibase::Datatype::Reference instance to structure.
75             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
76              
77             Returns reference to hash with structure.
78              
79             =head2 C<struct2obj>
80              
81             my $obj = struct2obj($struct_hr);
82              
83             Convert structure of reference to object.
84              
85             Returns Wikibase::Datatype::Reference instance.
86              
87             =head1 ERRORS
88              
89             obj2struct():
90             Base URI is required.
91             Object doesn't exist.
92             Object isn't 'Wikibase::Datatype::Reference'.
93              
94             =head1 EXAMPLE1
95              
96             use strict;
97             use warnings;
98              
99             use Data::Printer;
100             use Wikibase::Datatype::Reference;
101             use Wikibase::Datatype::Snak;
102             use Wikibase::Datatype::Struct::Reference qw(obj2struct);
103             use Wikibase::Datatype::Value::Item;
104             use Wikibase::Datatype::Value::String;
105             use Wikibase::Datatype::Value::Time;
106              
107             # Object.
108             # instance of (P31) human (Q5)
109             my $obj = Wikibase::Datatype::Reference->new(
110             'snaks' => [
111             # stated in (P248) Virtual International Authority File (Q53919)
112             Wikibase::Datatype::Snak->new(
113             'datatype' => 'wikibase-item',
114             'datavalue' => Wikibase::Datatype::Value::Item->new(
115             'value' => 'Q53919',
116             ),
117             'property' => 'P248',
118             ),
119              
120             # VIAF ID (P214) 113230702
121             Wikibase::Datatype::Snak->new(
122             'datatype' => 'external-id',
123             'datavalue' => Wikibase::Datatype::Value::String->new(
124             'value' => '113230702',
125             ),
126             'property' => 'P214',
127             ),
128              
129             # retrieved (P813) 7 December 2013
130             Wikibase::Datatype::Snak->new(
131             'datatype' => 'time',
132             'datavalue' => Wikibase::Datatype::Value::Time->new(
133             'value' => '+2013-12-07T00:00:00Z',
134             ),
135             'property' => 'P813',
136             ),
137             ],
138             );
139              
140             # Get structure.
141             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
142              
143             # Dump to output.
144             p $struct_hr;
145              
146             # Output:
147             # \ {
148             # snaks {
149             # P214 [
150             # [0] {
151             # datatype "external-id",
152             # datavalue {
153             # type "string",
154             # value 113230702
155             # },
156             # property "P214",
157             # snaktype "value"
158             # }
159             # ],
160             # P248 [
161             # [0] {
162             # datatype "wikibase-item",
163             # datavalue {
164             # type "wikibase-entityid",
165             # value {
166             # entity-type "item",
167             # id "Q53919",
168             # numeric-id 53919
169             # }
170             # },
171             # property "P248",
172             # snaktype "value"
173             # }
174             # ],
175             # P813 [
176             # [0] {
177             # datatype "time",
178             # datavalue {
179             # type "time",
180             # value {
181             # after 0,
182             # before 0,
183             # calendarmodel "http://test.wikidata.org/entity/Q1985727",
184             # precision 11,
185             # time "+2013-12-07T00:00:00Z",
186             # timezone 0
187             # }
188             # },
189             # property "P813",
190             # snaktype "value"
191             # }
192             # ]
193             # },
194             # snaks-order [
195             # [0] "P248",
196             # [1] "P214",
197             # [2] "P813"
198             # ]
199             # }
200              
201             =head1 EXAMPLE2
202              
203             use strict;
204             use warnings;
205              
206             use Wikibase::Datatype::Struct::Reference qw(struct2obj);
207              
208             # Item structure.
209             my $struct_hr = {
210             'snaks' => {
211             'P214' => [{
212             'datatype' => 'external-id',
213             'datavalue' => {
214             'type' => 'string',
215             'value' => '113230702',
216             },
217             'property' => 'P214',
218             'snaktype' => 'value',
219             }],
220             'P248' => [{
221             'datatype' => 'wikibase-item',
222             'datavalue' => {
223             'type' => 'wikibase-entityid',
224             'value' => {
225             'entity-type' => 'item',
226             'id' => 'Q53919',
227             'numeric-id' => 53919,
228             },
229             },
230             'property' => 'P248',
231             'snaktype' => 'value',
232             }],
233             'P813' => [{
234             'datatype' => 'time',
235             'datavalue' => {
236             'type' => 'time',
237             'value' => {
238             'after' => 0,
239             'before' => 0,
240             'calendarmodel' => 'http://test.wikidata.org/entity/Q1985727',
241             'precision' => 11,
242             'time' => '+2013-12-07T00:00:00Z',
243             'timezone' => 0,
244             },
245             },
246             'property' => 'P813',
247             'snaktype' => 'value',
248             }],
249             },
250             'snaks-order' => [
251             'P248',
252             'P214',
253             'P813',
254             ],
255             };
256              
257             # Get object.
258             my $obj = struct2obj($struct_hr);
259              
260             # Get value.
261             my $snaks_ar = $obj->snaks;
262              
263             # Print out number of snaks.
264             print "Number of snaks: ".@{$snaks_ar}."\n";
265              
266             # Output:
267             # Number of snaks: 3
268              
269             =head1 DEPENDENCIES
270              
271             L<Error::Pure>,
272             L<Exporter>,
273             L<Readonly>,
274             L<Wikibase::Datatype::Reference>,
275             L<Wikibase::Datatype::Struct::Utils>.
276              
277             =head1 SEE ALSO
278              
279             =over
280              
281             =item L<Wikibase::Datatype::Struct>
282              
283             Wikibase structure serialization.
284              
285             =item L<Wikibase::Datatype::Reference>
286              
287             Wikibase reference datatype.
288              
289             =back
290              
291             =head1 REPOSITORY
292              
293             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
294              
295             =head1 AUTHOR
296              
297             Michal Josef Špaček L<mailto:skim@cpan.org>
298              
299             L<http://skim.cz>
300              
301             =head1 LICENSE AND COPYRIGHT
302              
303             © Michal Josef Špaček 2020-2021
304              
305             BSD 2-Clause License
306              
307             =head1 VERSION
308              
309             0.08
310              
311             =cut