File Coverage

blib/lib/Wikibase/Datatype/Struct/Form.pm
Criterion Covered Total %
statement 68 68 100.0
branch 6 6 100.0
condition 2 2 100.0
subroutine 11 11 100.0
pod 2 2 100.0
total 89 89 100.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Form;
2              
3 8     8   381297 use base qw(Exporter);
  8         52  
  8         1009  
4 8     8   56 use strict;
  8         18  
  8         218  
5 8     8   45 use warnings;
  8         19  
  8         263  
6              
7 8     8   1654 use Error::Pure qw(err);
  8         38497  
  8         310  
8 8     8   264 use Readonly;
  8         21  
  8         336  
9 8     8   2479 use Wikibase::Datatype::Form;
  8         17604  
  8         327  
10 8     8   4108 use Wikibase::Datatype::Struct::Language;
  8         24  
  8         378  
11 8     8   4383 use Wikibase::Datatype::Struct::Statement;
  8         30  
  8         403  
12 8     8   63 use Wikibase::Datatype::Value::Item;
  8         18  
  8         4102  
13              
14             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
15              
16             our $VERSION = 0.08;
17              
18             sub obj2struct {
19 4     4 1 9288 my ($obj, $base_uri) = @_;
20              
21 4 100       15 if (! defined $obj) {
22 1         4 err "Object doesn't exist.";
23             }
24 3 100       21 if (! $obj->isa('Wikibase::Datatype::Form')) {
25 1         5 err "Object isn't 'Wikibase::Datatype::Form'.";
26             }
27 2 100       7 if (! defined $base_uri) {
28 1         4 err 'Base URI is required.';
29             }
30              
31 1         6 my $struct_hr = {
32             'id' => $obj->id,
33             };
34              
35             # Grammatical features.
36 1         13 foreach my $gf (@{$obj->grammatical_features}) {
  1         4  
37 2         22 push @{$struct_hr->{'grammaticalFeatures'}}, $gf->value;
  2         10  
38             }
39              
40             # Representations.
41 1         7 foreach my $rep (@{$obj->representations}) {
  1         4  
42 2         22 $struct_hr->{'representations'}->{$rep->language}
43             = Wikibase::Datatype::Struct::Language::obj2struct($rep);
44             }
45              
46             # Statements.
47 1         9 foreach my $statement (@{$obj->statements}) {
  1         4  
48 2   100     16 $struct_hr->{'claims'}->{$statement->snak->property} //= [];
49 2         30 push @{$struct_hr->{'claims'}->{$statement->snak->property}},
  2         6  
50             Wikibase::Datatype::Struct::Statement::obj2struct($statement, $base_uri);
51             }
52              
53 1         3 return $struct_hr;
54             }
55              
56             sub struct2obj {
57 1     1 1 132 my $struct_hr = shift;
58              
59             # Grammatical features.
60 1         3 my $gm_ar = [];
61 1         3 foreach my $gm (@{$struct_hr->{'grammaticalFeatures'}}) {
  1         5  
62 2         152 push @{$gm_ar}, Wikibase::Datatype::Value::Item->new(
  2         20  
63             'value' => $gm,
64             );
65             }
66              
67             # Representations.
68 1         69 my $representations_ar;
69 1         3 foreach my $lang (keys %{$struct_hr->{'representations'}}) {
  1         5  
70 2         9 push @{$representations_ar}, Wikibase::Datatype::Struct::Language::struct2obj(
71 2         6 $struct_hr->{'representations'}->{$lang});
72             }
73              
74             # Statements.
75 1         3 my $statements_ar = [];
76 1         3 foreach my $property (keys %{$struct_hr->{'claims'}}) {
  1         5  
77 1         3 foreach my $claim_hr (@{$struct_hr->{'claims'}->{$property}}) {
  1         4  
78 1         3 push @{$statements_ar}, Wikibase::Datatype::Struct::Statement::struct2obj(
  1         5  
79             $claim_hr,
80             );
81             }
82             }
83              
84             my $obj = Wikibase::Datatype::Form->new(
85             'grammatical_features' => $gm_ar,
86 1         22 'id' => $struct_hr->{'id'},
87             'representations' => $representations_ar,
88             'statements' => $statements_ar,
89             );
90              
91 1         103 return $obj;
92             }
93              
94             1;
95              
96             __END__
97              
98             =pod
99              
100             =encoding utf8
101              
102             =head1 NAME
103              
104             Wikibase::Datatype::Struct::Form - Wikibase form structure serialization.
105              
106             =head1 SYNOPSIS
107              
108             use Wikibase::Datatype::Struct::Form qw(obj2struct struct2obj);
109              
110             my $struct_hr = obj2struct($obj, $base_uri);
111             my $obj = struct2obj($struct_hr);
112              
113             =head1 DESCRIPTION
114              
115             This conversion is between objects defined in Wikibase::Datatype and structures
116             serialized via JSON to MediaWiki.
117              
118             =head1 SUBROUTINES
119              
120             =head2 C<obj2struct>
121              
122             my $struct_hr = obj2struct($obj, $base_uri);
123              
124             Convert Wikibase::Datatype::Form instance to structure.
125             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
126              
127             Returns reference to hash with structure.
128              
129             =head2 C<struct2obj>
130              
131             my $obj = struct2obj($struct_hr);
132              
133             Convert structure of form to object.
134              
135             Returns Wikibase::Datatype::Form instance.
136              
137             =head1 ERRORS
138              
139             obj2struct():
140             Base URI is required.
141             Object doesn't exist.
142             Object isn't 'Wikibase::Datatype::Form'.
143              
144             =head1 EXAMPLE1
145              
146             use strict;
147             use warnings;
148              
149             use Data::Printer;
150             use Wikibase::Datatype::Form;
151             use Wikibase::Datatype::Statement;
152             use Wikibase::Datatype::Struct::Form qw(obj2struct);
153             use Wikibase::Datatype::Value::Monolingual;
154              
155             # Statement.
156             my $statement = Wikibase::Datatype::Statement->new(
157             # instance of (P31) human (Q5)
158             'snak' => Wikibase::Datatype::Snak->new(
159             'datatype' => 'wikibase-item',
160             'datavalue' => Wikibase::Datatype::Value::Item->new(
161             'value' => 'Q5',
162             ),
163             'property' => 'P31',
164             ),
165             );
166              
167             # Object.
168             my $obj = Wikibase::Datatype::Form->new(
169             'grammatical_features' => [
170             Wikibase::Datatype::Value::Item->new(
171             'value' => 'Q163012',
172             ),
173             Wikibase::Datatype::Value::Item->new(
174             'value' => 'Q163014',
175             ),
176             ],
177             'id' => 'ID',
178             'representations' => [
179             Wikibase::Datatype::Value::Monolingual->new(
180             'language' => 'en',
181             'value' => 'Representation en',
182             ),
183             Wikibase::Datatype::Value::Monolingual->new(
184             'language' => 'cs',
185             'value' => 'Representation cs',
186             ),
187             ],
188             'statements' => [
189             $statement,
190             ],
191             );
192              
193             # Get structure.
194             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
195              
196             # Dump to output.
197             p $struct_hr;
198              
199             # Output:
200             # \ {
201             # claims {
202             # P31 [
203             # [0] {
204             # mainsnak {
205             # datatype "wikibase-item",
206             # datavalue {
207             # type "wikibase-entityid",
208             # value {
209             # entity-type "item",
210             # id "Q5",
211             # numeric-id 5
212             # }
213             # },
214             # property "P31",
215             # snaktype "value"
216             # },
217             # rank "normal",
218             # type "statement"
219             # }
220             # ]
221             # },
222             # grammaticalFeatures [
223             # [0] "Q163012",
224             # [1] "Q163014"
225             # ],
226             # id "ID",
227             # represenations {
228             # cs {
229             # language "cs",
230             # value "Representation cs"
231             # },
232             # en {
233             # language "en",
234             # value "Representation en"
235             # }
236             # }
237             # }
238              
239             =head1 EXAMPLE2
240              
241             use strict;
242             use warnings;
243              
244             use Data::Printer;
245             use Wikibase::Datatype::Struct::Form qw(struct2obj);
246              
247             # Item structure.
248             my $struct_hr = {
249             'grammaticalFeatures' => [
250             'Q163012',
251             'Q163014',
252             ],
253             'id' => 'ID',
254             'representations' => {
255             'cs' => {
256             'language' => 'cs',
257             'value' => 'Representation cs',
258             },
259             'en' => {
260             'language' => 'en',
261             'value' => 'Representation en',
262             },
263             },
264             'claims' => {
265             'P31' => [{
266             'mainsnak' => {
267             'datatype' => 'wikibase-item',
268             'datavalue' => {
269             'type' => 'wikibase-entityid',
270             'value' => {
271             'entity-type' => 'item',
272             'id' => 'Q5',
273             'numeric-id' => 5,
274             },
275             },
276             'property' => 'P31',
277             'snaktype' => 'value',
278             },
279             'rank' => 'normal',
280             'type' => 'statement',
281             }],
282             },
283             };
284              
285             # Get object.
286             my $obj = struct2obj($struct_hr);
287              
288             # Dump object.
289             p $obj;
290              
291             # Output:
292             # Wikibase::Datatype::Form {
293             # Parents Mo::Object
294             # public methods (6) : BUILD, can (UNIVERSAL), DOES (UNIVERSAL), check_array_object (Mo::utils), isa (UNIVERSAL), VERSION (UNIVERSAL)
295             # private methods (1) : __ANON__ (Mo::is)
296             # internals: {
297             # grammatical_features [
298             # [0] Wikibase::Datatype::Value::Item,
299             # [1] Wikibase::Datatype::Value::Item
300             # ],
301             # id "ID",
302             # represenations undef,
303             # statements [
304             # [0] Wikibase::Datatype::Statement
305             # ]
306             # }
307             # }
308              
309             =head1 DEPENDENCIES
310              
311             L<Error::Pure>,
312             L<Exporter>,
313             L<Readonly>,
314             L<Wikibase::Datatype::Form>,
315             L<Wikibase::Datatype::Struct::Language>,
316             L<Wikibase::Datatype::Struct::Statement>,
317             L<Wikibase::Datatype::Value::Item>.
318              
319             =head1 SEE ALSO
320              
321             =over
322              
323             =item L<Wikibase::Datatype::Struct>
324              
325             Wikibase structure serialization.
326              
327             =item L<Wikibase::Datatype::Form>
328              
329             Wikibase form datatype.
330              
331             =back
332              
333             =head1 REPOSITORY
334              
335             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
336              
337             =head1 AUTHOR
338              
339             Michal Josef Špaček L<mailto:skim@cpan.org>
340              
341             L<http://skim.cz>
342              
343             =head1 LICENSE AND COPYRIGHT
344              
345             © Michal Josef Špaček 2020-2021
346              
347             BSD 2-Clause License
348              
349             =head1 VERSION
350              
351             0.08
352              
353             =cut