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 34     34   1032396 use base qw(Exporter);
  34         147  
  34         3280  
4 34     34   346 use strict;
  34         81  
  34         686  
5 34     34   173 use warnings;
  34         77  
  34         984  
6              
7 34     34   1573 use Error::Pure qw(err);
  34         34823  
  34         1297  
8 34     34   385 use Readonly;
  34         87  
  34         1357  
9 34     34   14936 use Wikibase::Datatype::Reference;
  34         29405  
  34         1089  
10 34     34   15261 use Wikibase::Datatype::Struct::Utils qw(obj_array_ref2struct struct2snaks_array_ref);
  34         127  
  34         771  
11              
12             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13              
14             our $VERSION = 0.11;
15              
16             sub obj2struct {
17 9     9 1 9737 my ($obj, $base_uri) = @_;
18              
19 9 100       30 if (! defined $obj) {
20 1         7 err "Object doesn't exist.";
21             }
22 8 100       41 if (! $obj->isa('Wikibase::Datatype::Reference')) {
23 1         8 err "Object isn't 'Wikibase::Datatype::Reference'.";
24             }
25 7 100       25 if (! defined $base_uri) {
26 1         4 err 'Base URI is required.';
27             }
28              
29 6         30 my $struct_hr = obj_array_ref2struct($obj->snaks, 'snaks', $base_uri);
30              
31 6         39 return $struct_hr;
32             }
33              
34             sub struct2obj {
35 6     6 1 3001 my $struct_hr = shift;
36              
37 6         27 my $obj = Wikibase::Datatype::Reference->new(
38             'snaks' => struct2snaks_array_ref($struct_hr, 'snaks'),
39             );
40              
41 6         554 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             =for comment filename=obj2struct_reference.pl
97              
98             use strict;
99             use warnings;
100              
101             use Data::Printer;
102             use Wikibase::Datatype::Reference;
103             use Wikibase::Datatype::Snak;
104             use Wikibase::Datatype::Struct::Reference qw(obj2struct);
105             use Wikibase::Datatype::Value::Item;
106             use Wikibase::Datatype::Value::String;
107             use Wikibase::Datatype::Value::Time;
108              
109             # Object.
110             # instance of (P31) human (Q5)
111             my $obj = Wikibase::Datatype::Reference->new(
112             'snaks' => [
113             # stated in (P248) Virtual International Authority File (Q53919)
114             Wikibase::Datatype::Snak->new(
115             'datatype' => 'wikibase-item',
116             'datavalue' => Wikibase::Datatype::Value::Item->new(
117             'value' => 'Q53919',
118             ),
119             'property' => 'P248',
120             ),
121              
122             # VIAF ID (P214) 113230702
123             Wikibase::Datatype::Snak->new(
124             'datatype' => 'external-id',
125             'datavalue' => Wikibase::Datatype::Value::String->new(
126             'value' => '113230702',
127             ),
128             'property' => 'P214',
129             ),
130              
131             # retrieved (P813) 7 December 2013
132             Wikibase::Datatype::Snak->new(
133             'datatype' => 'time',
134             'datavalue' => Wikibase::Datatype::Value::Time->new(
135             'value' => '+2013-12-07T00:00:00Z',
136             ),
137             'property' => 'P813',
138             ),
139             ],
140             );
141              
142             # Get structure.
143             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
144              
145             # Dump to output.
146             p $struct_hr;
147              
148             # Output:
149             # \ {
150             # snaks {
151             # P214 [
152             # [0] {
153             # datatype "external-id",
154             # datavalue {
155             # type "string",
156             # value 113230702
157             # },
158             # property "P214",
159             # snaktype "value"
160             # }
161             # ],
162             # P248 [
163             # [0] {
164             # datatype "wikibase-item",
165             # datavalue {
166             # type "wikibase-entityid",
167             # value {
168             # entity-type "item",
169             # id "Q53919",
170             # numeric-id 53919
171             # }
172             # },
173             # property "P248",
174             # snaktype "value"
175             # }
176             # ],
177             # P813 [
178             # [0] {
179             # datatype "time",
180             # datavalue {
181             # type "time",
182             # value {
183             # after 0,
184             # before 0,
185             # calendarmodel "http://test.wikidata.org/entity/Q1985727",
186             # precision 11,
187             # time "+2013-12-07T00:00:00Z",
188             # timezone 0
189             # }
190             # },
191             # property "P813",
192             # snaktype "value"
193             # }
194             # ]
195             # },
196             # snaks-order [
197             # [0] "P248",
198             # [1] "P214",
199             # [2] "P813"
200             # ]
201             # }
202              
203             =head1 EXAMPLE2
204              
205             =for comment filename=struct2obj_reference.pl
206              
207             use strict;
208             use warnings;
209              
210             use Wikibase::Datatype::Struct::Reference qw(struct2obj);
211              
212             # Item structure.
213             my $struct_hr = {
214             'snaks' => {
215             'P214' => [{
216             'datatype' => 'external-id',
217             'datavalue' => {
218             'type' => 'string',
219             'value' => '113230702',
220             },
221             'property' => 'P214',
222             'snaktype' => 'value',
223             }],
224             'P248' => [{
225             'datatype' => 'wikibase-item',
226             'datavalue' => {
227             'type' => 'wikibase-entityid',
228             'value' => {
229             'entity-type' => 'item',
230             'id' => 'Q53919',
231             'numeric-id' => 53919,
232             },
233             },
234             'property' => 'P248',
235             'snaktype' => 'value',
236             }],
237             'P813' => [{
238             'datatype' => 'time',
239             'datavalue' => {
240             'type' => 'time',
241             'value' => {
242             'after' => 0,
243             'before' => 0,
244             'calendarmodel' => 'http://test.wikidata.org/entity/Q1985727',
245             'precision' => 11,
246             'time' => '+2013-12-07T00:00:00Z',
247             'timezone' => 0,
248             },
249             },
250             'property' => 'P813',
251             'snaktype' => 'value',
252             }],
253             },
254             'snaks-order' => [
255             'P248',
256             'P214',
257             'P813',
258             ],
259             };
260              
261             # Get object.
262             my $obj = struct2obj($struct_hr);
263              
264             # Get value.
265             my $snaks_ar = $obj->snaks;
266              
267             # Print out number of snaks.
268             print "Number of snaks: ".@{$snaks_ar}."\n";
269              
270             # Output:
271             # Number of snaks: 3
272              
273             =head1 DEPENDENCIES
274              
275             L<Error::Pure>,
276             L<Exporter>,
277             L<Readonly>,
278             L<Wikibase::Datatype::Reference>,
279             L<Wikibase::Datatype::Struct::Utils>.
280              
281             =head1 SEE ALSO
282              
283             =over
284              
285             =item L<Wikibase::Datatype::Struct>
286              
287             Wikibase structure serialization.
288              
289             =item L<Wikibase::Datatype::Reference>
290              
291             Wikibase reference datatype.
292              
293             =back
294              
295             =head1 REPOSITORY
296              
297             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
298              
299             =head1 AUTHOR
300              
301             Michal Josef Špaček L<mailto:skim@cpan.org>
302              
303             L<http://skim.cz>
304              
305             =head1 LICENSE AND COPYRIGHT
306              
307             © 2020-2023 Michal Josef Špaček
308              
309             BSD 2-Clause License
310              
311             =head1 VERSION
312              
313             0.11
314              
315             =cut